{"record":{"id":"77546d353efb27b8","repo":"stablyai/orca","slug":"terminal-host-is-shutting-down","errorCode":null,"errorMessage":"Terminal host is shutting down","messagePattern":"Terminal host is shutting down","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/main/daemon/terminal-host-session-create.ts","lineNumber":31,"sourceCode":"import { resolveWslSessionContext } from './wsl-session-context'\n\ntype TerminalHostSessionCreateDependencies = {\n  sessions: Map<string, Session>\n  sessionTeardown: TerminalSessionTeardown\n  killedTombstones: TerminalHostTombstones\n  spawnSubprocess: TerminalHostOptions['spawnSubprocess']\n  creationFenced: boolean\n  onDeadSessionRemoved: (sessionId: string) => void\n  onSessionCreated: (sessionId: string, generation: string | undefined, isAlive: boolean) => void\n  onSessionExit: (sessionId: string, generation: string | undefined) => void\n}\n\nexport async function createOrAttachTerminalSession(\n  opts: InternalCreateOrAttachOptions,\n  deps: TerminalHostSessionCreateDependencies\n): Promise<CreateOrAttachResult> {\n  if (deps.creationFenced) {\n    throw new Error('Terminal host is shutting down')\n  }\n  opts.onSessionResolved?.(opts.sessionId)\n  const existing = deps.sessions.get(opts.sessionId)\n\n  // Why: descendant capture must finish before attach or recreation, or the\n  // caller could receive a doomed session while teardown owns its process.\n  if (deps.sessionTeardown.get(opts.sessionId) || existing?.isTerminating) {\n    throw new SessionNotFoundError(opts.sessionId)\n  }\n\n  if (existing && existing.isAlive && !existing.isTerminating) {\n    const snapshot = existing.getSnapshot()\n    existing.detachAllClients()\n    const token = existing.attachClient(opts.streamClient)\n    return {\n      isNew: false,\n      snapshot,\n      pid: existing.pid,","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/stablyai/orca/blob/1136503c6a231a16dce8f921f6fadb63d181e8db/src/main/daemon/terminal-host-session-create.ts#L13-L49","documentation":"createOrAttachTerminalSession refuses a new session when deps.creationFenced is true. The terminal host sets the fence during shutdown/teardown so no new sessions can be spawned while it is draining. This prevents a session from being created that would immediately need to be torn down, and keeps the shutdown path deterministic.","triggerScenarios":"createOrAttachTerminalSession called after the host began shutting down and set creationFenced = true. Any create (non-attach) during host disposal hits this.","commonSituations":"A createOrAttach racing the app quit / daemon shutdown; the host fencing during an error-driven teardown while a queued create is still in flight; retries of createOrAttach after shutdown has begun.","solutions":["Do not issue create requests to a host that is shutting down; check host liveness before creating.","On the client, treat this as a transient/reconnect condition: wait for a fresh host or reconnect to a new daemon, then retry.","Ensure shutdown drains in-flight creates before fencing if orderly completion matters.","Cancel pending create requests when shutdown is initiated rather than letting them hit the fence."],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":"function isHostShuttingDown(e: unknown): boolean {\n  return e instanceof Error && e.message === 'Terminal host is shutting down'\n}","tryCatchPattern":"try {\n  return await createOrAttachTerminalSession(opts, deps)\n} catch (e) {\n  if (e instanceof Error && e.message === 'Terminal host is shutting down') {\n    // reconnect to a fresh host/daemon, then retry\n    await reconnectHost()\n    return await createOrAttachTerminalSession(opts, freshDeps)\n  }\n  throw e\n}","preventionTips":["Check host liveness before issuing create requests.","Cancel pending creates when shutdown is initiated.","Treat this as a reconnect condition rather than a fatal error."],"tags":["terminal-host","shutdown","lifecycle"],"backgroundTag":null,"analyzedSha":"1136503c6a231a16dce8f921f6fadb63d181e8db","analyzedAt":"2026-08-12T23:15:58.167Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}