Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

fix(pty): keep a client's per-connection socket pause when one view leaves

enesd8a69ccf5967
diff --git a/src/core/pty-coattach.test.ts b/src/core/pty-coattach.test.ts
index 8cc97f785..5f4c3cad6 100644
--- a/src/core/pty-coattach.test.ts
+++ b/src/core/pty-coattach.test.ts
@@ -1299,4 +1299,52 @@ describe('viewer identity: multiple views per connection', () => {
killV(ALICE, sessionId, 'modal') // the last view closes
expect(spawned[0].killed).toBe(true) // …only now is the pty released
})
+
+ // ── The Server Edition's per-CONNECTION socket pause outlives a single view's departure ──────
+ // A browser's WS send-buffer backpressure (`setFlow(uiId, …, 'socket')`) is owned by the whole
+ // CONNECTION, not by one xterm — it rides the PRIMARY view by convention (the socket forwards no
+ // viewerId). So when the canvas node (PRIMARY) unmounts while the kanban modal is still open on the
+ // SAME connection, that one shared socket may still be jammed: a `kill` that returned EVERY owner's
+ // ticket for the departing view — as a full departure does — would hand back the socket's pause too
+ // and permanently un-pause a drowning connection, because the renderer's flow control is
+ // edge-latched and will never re-pause (invariant (b) on `pausedBy`). The socket ticket must
+ // survive until the client's LAST view leaves; only THIS view's own renderer pause is unreturnable.
+ it("a PRIMARY-view kill keeps the client's per-connection SOCKET pause while a modal view stays", async () => {
+ const m = await manager()
+ vi.spyOn(m, 'captureForResync').mockResolvedValue('x')
+ const { sessionId } = await createV(ALICE, undefined) // the canvas node (PRIMARY)
+ await createV(ALICE, 'modal') // the kanban card modal, SAME connection
+
+ m.setFlow(ALICE, sessionId, false, 'socket') // ALICE's WS socket is backed up (per-connection)
+ expect(spawned[0].paused).toBe(true)
+
+ killV(ALICE, sessionId) // the canvas node unmounts (PRIMARY) — the modal stays open
+ expect(spawned[0].paused).toBe(true) // the connection is still jammed; the pty must STAY paused
+ expect(spawned[0].killed).toBe(false) // …and the modal keeps it alive
+
+ m.setFlow(ALICE, sessionId, true, 'socket') // the socket finally drains into the browser
+ expect(spawned[0].paused).toBe(false) // only now does the pty resume
+
+ killV(ALICE, sessionId, 'modal') // the last view closes → the pty is released
+ expect(spawned[0].killed).toBe(true)
+ })
+
+ // Belt-and-braces: if the socket is STILL jammed when the client's last view leaves, the departure
+ // sweep must return the socket ticket (which rides the PRIMARY view, not the departing modal's
+ // key), or a released pty would be left holding an un-returnable pause on a session that is gone.
+ it("the client's last view leaving sweeps a still-held socket pause (no leaked ticket)", async () => {
+ const m = await manager()
+ vi.spyOn(m, 'captureForResync').mockResolvedValue('x')
+ const { sessionId } = await createV(ALICE, undefined)
+ await createV(ALICE, 'modal')
+ await createV(BOB, undefined) // a second CONNECTION keeps the pty alive after ALICE fully leaves
+
+ m.setFlow(ALICE, sessionId, false, 'socket')
+ killV(ALICE, sessionId) // PRIMARY view goes; ALICE's socket pause stays (modal still open)
+ expect(spawned[0].paused).toBe(true)
+
+ killV(ALICE, sessionId, 'modal') // ALICE's LAST view leaves without ever resuming the socket
+ expect(spawned[0].paused).toBe(false) // the departure sweep returns the leftover socket ticket
+ expect(spawned[0].killed).toBe(false) // BOB still watches, so the pty is not released
+ })
})
diff --git a/src/core/pty-manager.ts b/src/core/pty-manager.ts
index ecf8ba2ba..027550600 100644
--- a/src/core/pty-manager.ts
+++ b/src/core/pty-manager.ts
@@ -1692,11 +1692,23 @@ export class PtyManager {
}
// The departing view (or sink) may have been one of the ones that paused us, and it will
// never send the matching resume now — leaving that pause in place would freeze the terminal
- // for everyone who stayed. EVERY owner it owed goes back (no `owner` argument): it stops
- // receiving this session's output entirely, so neither its renderer nor its socket can ever
- // resume us again. A pause owed by a view that is STILL here is untouched: the pty stays
- // paused until IT drains (its renderer cannot re-pause — see `Session.pausedBy`).
- this.releaseFlow(session, sub)
+ // for everyone who stayed. But WHICH tickets are unreturnable depends on whether the CLIENT is
+ // wholly gone or just this one VIEW:
+ // - the relay sink (clientId null) departs entirely → return every owner it owed.
+ // - the client still has ANOTHER view (e.g. the canvas node closed but the kanban modal is
+ // still open on the SAME connection): only THIS view's RENDERER pause is unreturnable — its
+ // own xterm is gone. The SOCKET pause is per-CONNECTION (it rides the PRIMARY view and is
+ // shared by every view of this client), so it must survive; handing it back here would
+ // permanently un-pause a still-jammed connection whose renderer flow control is edge-latched.
+ // - this was the client's LAST view → the whole connection is gone. Sweep every ticket it owes
+ // across all its views and owners (its socket ticket rides the PRIMARY view, so it is not
+ // necessarily keyed on `sub`) — the same departure sweep `dropClient` uses.
+ // A pause owed by a DIFFERENT client that is still here is untouched either way (the pty stays
+ // paused until IT drains — its renderer cannot re-pause; see `Session.pausedBy`).
+ if (clientId === null) this.releaseFlow(session, sub)
+ else if ([...session.subscribers].some((s) => subClient(s) === clientId))
+ this.releaseFlow(session, sub, 'renderer')
+ else this.releaseFlowForClient(session, clientId)
if (session.subscribers.size > 0 || session.onData) {
// Somebody is still watching: the departing client's size no longer constrains the pty.
this.applySize(sessionId, session)