zed-industries/zed · error

session was closed before load completed

Error message

session was closed before load completed

What it means

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.

Source

Thrown at crates/agent_servers/src/acp.rs:1277

                    if let Some(config_opts) = config_options.as_ref() {
                        this.apply_default_config_options(&session_id, config_opts, cx);
                    }

                    let ref_count = this
                        .pending_sessions
                        .borrow_mut()
                        .remove(&session_id)
                        .map_or(1, |pending| pending.ref_count);

                    // If `close_session` ran to completion while the load RPC was in
                    // flight, it will have removed both the pending entry and the
                    // sessions entry (and dispatched the ACP close RPC). In that case
                    // the thread has no live session to attach to, so fail the load
                    // instead of handing back an orphaned thread.
                    {
                        let mut sessions = this.sessions.borrow_mut();
                        let Some(session) = sessions.get_mut(&session_id) else {
                            return Err(Arc::new(anyhow!(
                                "session was closed before load completed"
                            )));
                        };
                        session.session_modes = modes;
                        session.config_options = config_options.map(ConfigOptions::new);
                        session.ref_count = ref_count;
                    }

                    Ok(thread)
                }
            })
            .shared();

        self.pending_sessions.borrow_mut().insert(
            session_id,
            PendingAcpSession {
                task: shared_task.clone(),
                ref_count: 1,

View on GitHub (pinned to bc538def45)

Solutions

  1. Open the session again — open_or_create_session will create a fresh session
  2. Avoid closing a thread while its load is still in flight (disable close actions until loaded)
  3. Audit callers for duplicate concurrent loads of the same session id
Defensive patterns

Strategy: retry

Try / catch

match load_result {
    Err(e) if e.to_string().contains("session was closed before load completed") => {
        // close won the race; open the session again instead of reusing the thread handle
        open_or_create_session(project, work_dirs, cx).await
    }
    result => result,
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of zed-industries/zed@bc538def45 (2026-08-16). Data as JSON: /api/errors/4264245dd1b1c0e2. Report an issue: GitHub.