fix(terminal): make SSE cleanup idempotent (#1234)
Co-authored-by: Isaac Sanchez <isanchez-hawkins@arize.com>
This commit is contained in:
committed by
GitHub
co-authored by
Isaac Sanchez
parent
060a5311ef
commit
df12c3a2b5
@@ -572,6 +572,34 @@ export function createTerminalRuntime({
|
|||||||
}
|
}
|
||||||
}, 15000);
|
}, 15000);
|
||||||
|
|
||||||
|
let cleanedUp = false;
|
||||||
|
let dataDisposable = null;
|
||||||
|
let exitDisposable = null;
|
||||||
|
const cleanup = () => {
|
||||||
|
if (cleanedUp) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
cleanedUp = true;
|
||||||
|
clearInterval(heartbeatInterval);
|
||||||
|
session.clients.delete(clientId);
|
||||||
|
|
||||||
|
if (dataDisposable && typeof dataDisposable.dispose === 'function') {
|
||||||
|
dataDisposable.dispose();
|
||||||
|
}
|
||||||
|
if (exitDisposable && typeof exitDisposable.dispose === 'function') {
|
||||||
|
exitDisposable.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
res.end();
|
||||||
|
} catch (error) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`Client ${clientId} disconnected from terminal session ${sessionId}`);
|
||||||
|
};
|
||||||
|
|
||||||
const dataHandler = (data) => {
|
const dataHandler = (data) => {
|
||||||
try {
|
try {
|
||||||
session.lastActivity = Date.now();
|
session.lastActivity = Date.now();
|
||||||
@@ -600,28 +628,15 @@ export function createTerminalRuntime({
|
|||||||
cleanup();
|
cleanup();
|
||||||
};
|
};
|
||||||
|
|
||||||
const dataDisposable = session.ptyProcess.onData(dataHandler);
|
dataDisposable = session.ptyProcess.onData(dataHandler);
|
||||||
const exitDisposable = session.ptyProcess.onExit(exitHandler);
|
if (cleanedUp && dataDisposable && typeof dataDisposable.dispose === 'function') {
|
||||||
|
dataDisposable.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
const cleanup = () => {
|
exitDisposable = session.ptyProcess.onExit(exitHandler);
|
||||||
clearInterval(heartbeatInterval);
|
if (cleanedUp && exitDisposable && typeof exitDisposable.dispose === 'function') {
|
||||||
session.clients.delete(clientId);
|
exitDisposable.dispose();
|
||||||
|
}
|
||||||
if (dataDisposable && typeof dataDisposable.dispose === 'function') {
|
|
||||||
dataDisposable.dispose();
|
|
||||||
}
|
|
||||||
if (exitDisposable && typeof exitDisposable.dispose === 'function') {
|
|
||||||
exitDisposable.dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
res.end();
|
|
||||||
} catch (error) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log(`Client ${clientId} disconnected from terminal session ${sessionId}`);
|
|
||||||
};
|
|
||||||
|
|
||||||
req.on('close', cleanup);
|
req.on('close', cleanup);
|
||||||
req.on('error', cleanup);
|
req.on('error', cleanup);
|
||||||
|
|||||||
Reference in New Issue
Block a user