{"record":{"id":"4264245dd1b1c0e2","repo":"zed-industries/zed","slug":"session-was-closed-before-load-completed","errorCode":null,"errorMessage":"session was closed before load completed","messagePattern":"session was closed before load completed","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/agent_servers/src/acp.rs","lineNumber":1277,"sourceCode":"                    if let Some(config_opts) = config_options.as_ref() {\n                        this.apply_default_config_options(&session_id, config_opts, cx);\n                    }\n\n                    let ref_count = this\n                        .pending_sessions\n                        .borrow_mut()\n                        .remove(&session_id)\n                        .map_or(1, |pending| pending.ref_count);\n\n                    // If `close_session` ran to completion while the load RPC was in\n                    // flight, it will have removed both the pending entry and the\n                    // sessions entry (and dispatched the ACP close RPC). In that case\n                    // the thread has no live session to attach to, so fail the load\n                    // instead of handing back an orphaned thread.\n                    {\n                        let mut sessions = this.sessions.borrow_mut();\n                        let Some(session) = sessions.get_mut(&session_id) else {\n                            return Err(Arc::new(anyhow!(\n                                \"session was closed before load completed\"\n                            )));\n                        };\n                        session.session_modes = modes;\n                        session.config_options = config_options.map(ConfigOptions::new);\n                        session.ref_count = ref_count;\n                    }\n\n                    Ok(thread)\n                }\n            })\n            .shared();\n\n        self.pending_sessions.borrow_mut().insert(\n            session_id,\n            PendingAcpSession {\n                task: shared_task.clone(),\n                ref_count: 1,","sourceCodeStart":1259,"sourceCodeEnd":1295,"githubUrl":"https://github.com/zed-industries/zed/blob/bc538def4545534201bbfcac4e95ac34ea6501b6/crates/agent_servers/src/acp.rs#L1259-L1295","documentation":"When an ACP session load RPC completes, Zed re-attaches the thread to the entry in its sessions map. If close_session for the same session id ran to completion while the load was in flight, the entry (and pending entry) are gone, so the load fails with this error instead of handing back a thread with no live session to attach to.","triggerScenarios":"close_session (refcount dropping to zero, thread tab closed) racing load_session for the same session id — the load finishes after the close already removed the session entry.","commonSituations":"Quickly closing a thread while it (or a duplicate view of it) is still loading; two components loading the same session concurrently and one path closing it.","solutions":["Open the session again — open_or_create_session will create a fresh session","Avoid closing a thread while its load is still in flight (disable close actions until loaded)","Audit callers for duplicate concurrent loads of the same session id"],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"match load_result {\n    Err(e) if e.to_string().contains(\"session was closed before load completed\") => {\n        // close won the race; open the session again instead of reusing the thread handle\n        open_or_create_session(project, work_dirs, cx).await\n    }\n    result => result,\n}","preventionTips":["Do not close a thread while its load RPC is still in flight; disable close actions until load settles","Avoid issuing duplicate concurrent loads of the same session id from multiple components"],"tags":["acp","agent-server","race-condition","session"],"backgroundTag":null,"analyzedSha":"bc538def4545534201bbfcac4e95ac34ea6501b6","analyzedAt":"2026-08-16T07:30:46.435Z","schemaVersion":2},"datasetVersion":"2026-08-16T08:17:34.114Z"}