Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review (claude): cancel probe timers + listeners when the shell exits before round-trip

Adam Eivy8f633072375c
diff --git a/server/services/shell.js b/server/services/shell.js
index c1d88f85fc..2cf57b03b6 100644
--- a/server/services/shell.js
+++ b/server/services/shell.js
@@ -187,15 +187,24 @@ export function createShellSession(socket, options = {}) {
// platform-aware probe.
let sent = false;
let sub = null;
+ let exitSub = null;
const nonce = uuidv4().replace(/-/g, '').slice(0, 12);
const marker = `PORTOSRDY${nonce}`;
const probe = `printf '%s\\n' 'PORTOSRDY''${nonce}'\n`;
let seen = '';
+ // Tear down every pending timer + listener. Called both on success
+ // (fire) and when the PTY exits before the probe round-trips, so no
+ // timer survives to fire into a dead session.
+ const stop = () => {
+ clearTimeout(probeTimer);
+ clearTimeout(fallback);
+ sub?.dispose?.();
+ exitSub?.dispose?.();
+ };
const fire = () => {
if (sent) return;
sent = true;
- clearTimeout(fallback);
- sub?.dispose?.();
+ stop();
sendInitial();
};
sub = ptyProcess.onData((chunk) => {
@@ -203,11 +212,15 @@ export function createShellSession(socket, options = {}) {
if (seen.length > 8192) seen = seen.slice(-8192);
if (seen.includes(marker)) fire();
});
+ // If the shell dies before the probe round-trips, cancel the pending
+ // writes/timers — sent stays true so the fallback can't resurrect a
+ // send into the gone session.
+ exitSub = ptyProcess.onExit(() => { sent = true; stop(); });
// Give the freshly-spawned PTY a tick to come up, then send the probe.
// Writing earlier is harmless (zsh's line editor / p10k instant-prompt
// buffer holds it until the prompt is live and replays it), but a small
// delay avoids racing node-pty's own spawn handshake.
- setTimeout(() => { if (!sent) writeToSession(sessionId, probe); }, 50);
+ const probeTimer = setTimeout(() => { if (!sent) writeToSession(sessionId, probe); }, 50);
const fallback = setTimeout(fire, options.initialCommandDelayMs ?? 8000);
} else {
setTimeout(sendInitial, options.initialCommandDelayMs ?? 200);
diff --git a/server/services/shell.test.js b/server/services/shell.test.js
index d92c2362b1..97172fe41c 100644
--- a/server/services/shell.test.js
+++ b/server/services/shell.test.js
@@ -215,6 +215,18 @@ describe('createShellSession', () => {
expect(pty.write).toHaveBeenCalledWith('claude\n');
vi.useRealTimers();
});
+
+ it('waitForPromptReady: if the shell exits before the probe round-trips, the fallback never injects the command', () => {
+ vi.useFakeTimers();
+ shell.createShellSession(makeSocket(), { initialCommand: 'claude', waitForPromptReady: true, initialCommandDelayMs: 8000 });
+ const pty = ptyInstances[0];
+ vi.advanceTimersByTime(50); // probe written
+ // The shell dies before the marker round-trips → cancels pending timers.
+ pty.emitExit({ exitCode: 1 });
+ vi.advanceTimersByTime(8000); // past the (now-cleared) fallback window
+ expect(pty.write).not.toHaveBeenCalledWith('claude\n');
+ vi.useRealTimers();
+ });
});
describe('attachSession', () => {