Repository navigation
fix(server): detach the attached runtime when a --listen TUI tab closes - #4930
pcbeingused333 wants to merge 3 commits into
Conversation
Sessions spawned from TUI tabs under --listen are attached to the serving SessionManager, but nothing released them when the tab was closed, so the runtime, session, event log and follow-up injector stayed retained and the session kept showing up as active until the process exited. Add SessionManager.DetachRuntime (the inverse of AttachRuntime, keeping the persisted session) and call it from the spawner's cleanup. Signed-off-by: Alex Castillo <alex.castillog33@gmail.com>
aheritier
left a comment
There was a problem hiding this comment.
Spawned tabs now leave the active-session list without deleting the stored session, but the fix does not yet cover the full lifecycle in #4909. #4931 addresses the same issue and is a useful comparison for the detach wiring.
Validation: On this commit, the server, TUI, supervisor, and root command package tests passed. Server and supervisor race tests and targeted go vet also passed. Two focused regression tests reproduced the server findings below. The initial-tab finding was verified by tracing the cleanup callbacks, not by an interactive TUI run.
Prior threads: No reviews or review threads; there is no prior human-reviewed commit to compare against.
| // (see recallCoordinatorOpt); release it when the tab goes away so | ||
| // the runtime, event log and injector are not retained. | ||
| if f.listenSM != nil { | ||
| f.listenSM.DetachRuntime(newSess.ID) |
There was a problem hiding this comment.
[blocking] This only detaches tabs created by createSessionSpawner. The initial tab is attached separately by startSessionCoordinator (run_listen.go:75–80), but runOrExec still passes the backend's cleanup directly to the TUI (run.go:559–616). Open a second tab under --listen and close the initial one: the supervisor removes the tab, but its runtime and follow-up injector remain registered and /api/sessions still lists it as active. Wire detachment into the initial tab's cleanup too, and cover both paths with a tab-close regression test.
| if rs.cancel != nil { | ||
| rs.cancel() | ||
| } | ||
| sm.runtimeSessions.Delete(sessionID) |
There was a problem hiding this comment.
[blocking] Removing the runtime from the manager does not undo the recall handler installed by AttachRuntime (session_manager.go:459–460). An embedder that keeps the detached runtime still holds a callback into this manager; an idle recall is routed back to the detached session and rejected instead of falling back to the runtime's steer queue (runtime.go:1492–1514). A focused test with a runtime that implements SetRecallHandler confirmed the handler remains set after detach. Clear the handler installed for that attachment.
| rs.cancel() | ||
| } | ||
| sm.runtimeSessions.Delete(sessionID) | ||
| sm.dropEventLog(sessionID) |
There was a problem hiding this comment.
[blocking] dropEventLog permanently tombstones the session ID (session_manager.go:329–341). The stored session survives, but reattaching that same ID succeeds while RegisterEventSource silently refuses to register its new source (session_manager.go:256–261), so /api/sessions/:id/events returns 404. A focused attach → detach → reload → reattach test reproduced the rejected registration. Tear down the attachment's event log without marking the session deleted, and add a same-ID reattachment test.
Clear the recall handler AttachRuntime installed, tear down the event log without tombstoning the session ID so it can be reattached, and detach the initial --listen tab when it is closed, not only spawned tabs. Signed-off-by: Alex Castillo <alex.castillog33@gmail.com>
|
Thanks for the detailed review. Pushed b59a330 (on top of a merge of current main), addressing the three blocking points:
This comment was drafted with the help of an AI assistant (Claude Code) and the change was tested locally. |
Fixes #4909
Root cause. Under
--listen, every TUI tab's session is attached to the servingSessionManagerviaAttachRuntime, but onlyfinishSessionDeletionever removed that state. Closing a tab ran the spawner'scleanup(stop toolsets) and nothing else, so the runtime, session, event log and follow-up injector stayed retained and the session kept being listed as active for the life of the process.Fix. Add
SessionManager.DetachRuntime, the inverse ofAttachRuntime: it cancels the attach context and drops the runtime registration, event log, follow-up injector/keys and pending safety defaults, without deleting the persisted session (so it can still be resumed). The spawner'scleanupcalls it whenf.listenSM != nil.Test.
TestSessionManager_DetachRuntimeReleasesAttachedSession: Attach → Detach →GetActiveSessionsis empty, injector released, stored session intact, second detach is a no-op. Fails before the change, passes after;go test ./pkg/server,go vetandgofmtare clean.This change was prepared with the help of an AI coding assistant (Claude) and submitted by a human contributor.