Repository navigation
fix(server): detach the attached runtime when a --listen TUI tab closes #4930
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
19b0529
1830ad7
b59a330
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -341,6 +341,19 @@ func (sm *SessionManager) dropEventLog(sessionID string) { | |
| } | ||
| } | ||
|
|
||
| // releaseEventLog tears down sessionID's event log, if any, without | ||
| // tombstoning the ID: unlike dropEventLog it is for a session that merely | ||
| // stops being attached (see DetachRuntime), so the same ID can be attached | ||
| // again and register a fresh event source. | ||
| func (sm *SessionManager) releaseEventLog(sessionID string) { | ||
| sm.eventLogsMu.Lock() | ||
| defer sm.eventLogsMu.Unlock() | ||
| if pe, ok := sm.eventLogs.Load(sessionID); ok { | ||
| pe.cancel() | ||
| sm.eventLogs.Delete(sessionID) | ||
| } | ||
| } | ||
|
|
||
| // appendSessionEvent appends event to sessionID's event log, creating the | ||
| // log on demand (see ensureEventLog) if this is the first out-of-band event | ||
| // the session has ever produced. Events for a deleted session are dropped. | ||
|
|
@@ -462,6 +475,37 @@ func (sm *SessionManager) AttachRuntime(ctx context.Context, sessionID string, r | |
| return &rs.streaming | ||
| } | ||
|
|
||
| // DetachRuntime is the inverse of [SessionManager.AttachRuntime]: it releases | ||
| // the manager's in-memory state for an attached session (runtime registration, | ||
| // cancellation signal, event log, follow-up injector) without touching the | ||
| // persisted session. An embedder calls it when it stops owning the session, | ||
| // e.g. when a TUI tab is closed under --listen; otherwise the runtime and its | ||
| // session would stay reachable (and listed as active) until the process exits. | ||
| // Unlike [SessionManager.DeleteSession], the stored session is kept and the | ||
| // session ID is not tombstoned, so it can be attached again (e.g. a reopened tab). It is a no-op for unknown sessions. | ||
| func (sm *SessionManager) DetachRuntime(sessionID string) { | ||
| sm.mux.Lock() | ||
| defer sm.mux.Unlock() | ||
| rs, ok := sm.runtimeSessions.Load(sessionID) | ||
| if !ok { | ||
| return | ||
| } | ||
| if rs.cancel != nil { | ||
| rs.cancel() | ||
| } | ||
| sm.runtimeSessions.Delete(sessionID) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [blocking] Removing the runtime from the manager does not undo the recall handler installed by |
||
| // Undo the recall handler installed by AttachRuntime so a runtime kept by | ||
| // the embedder falls back to its own steer queue instead of routing idle | ||
| // recalls to a session this manager no longer owns. | ||
| if setter, ok := rs.runtime.(recallHandlerSetter); ok { | ||
| setter.SetRecallHandler(nil) | ||
| } | ||
| sm.releaseEventLog(sessionID) | ||
| sm.followUpInjectors.Delete(sessionID) | ||
| sm.followUpKeys.Delete(sessionID) | ||
| sm.pendingSafetyDefaults.Delete(sessionID) | ||
| } | ||
|
|
||
| // GetSession retrieves a session by ID. | ||
| func (sm *SessionManager) GetSession(ctx context.Context, id string) (*session.Session, error) { | ||
| if rs, ok := sm.runtimeSessions.Load(id); ok && rs.session != nil { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[blocking] This only detaches tabs created by
createSessionSpawner. The initial tab is attached separately bystartSessionCoordinator(run_listen.go:75–80), butrunOrExecstill passes the backend's cleanup directly to the TUI (run.go:559–616). Open a second tab under--listenand close the initial one: the supervisor removes the tab, but its runtime and follow-up injector remain registered and/api/sessionsstill lists it as active. Wire detachment into the initial tab's cleanup too, and cover both paths with a tab-close regression test.