# agent_servers: Fix abandoned ACP session loads \(\#64717\) · gitcafe/zed

[View on GitCafe](https://git.cafe/gitcafe/zed/commit/3ab0b5df18031e19b0267b189969ef1a991cd352)

Repository: [gitcafe/zed](https://git.cafe/gitcafe/zed)

Visibility: public

Requested revision: 3ab0b5df18031e19b0267b189969ef1a991cd352

Requested commit: 3ab0b5df18031e19b0267b189969ef1a991cd352

Commit: 3ab0b5df18031e19b0267b189969ef1a991cd352

Tree: ba113fe96e8529dc886c4c80fa95eb74af1252ef

Author: Ben Brandt

Committer: GitHub

## Message

```
agent_servers: Fix abandoned ACP session loads (#64717)

Fix an ownership cycle in ACP session loading: a connection-owned
pending task retained the thread, which in turn retained the connection.
Closing the loading view could therefore keep both alive indefinitely
while the agent's response was outstanding.

- Keep strong thread ownership with active callers; shared load/resume
operations retain only weak application references while awaiting the
agent.
- Preserve concurrent-load deduplication. Track session close alongside
pending loads so reopening the same session waits for the previous
operation's cleanup before registering a replacement.
- Drain queued foreground replay at cleanup boundaries, without adding a
barrier to successful live loads or changing their mode/config
initialization ordering.
- Ignore replay targeting a released model while preserving session-list
metadata updates.

## Lifecycle boundaries

Successful abandoned loads settle, attempt a best-effort close, and
drain queued replay before replacement. Failed loads drain and clean up
locally before retry, preserving the existing no-close-on-load-error
policy. The tracked close also covers releasing an already-loaded
thread.

This does not add timeouts or cancel detached filesystem/terminal work
already launched by a handler. Same-session reopening waits for the
earlier load/close to settle; pending wire operations no longer keep the
application connection alive by themselves.

Release Notes:

- Fixed external-agent session cleanup when closing and reopening
conversations.
```

## Parents

- [382ff7a64b334ab59a3dc0e3f3185705f79cd0ab](https://git.cafe/gitcafe/zed/commit/382ff7a64b334ab59a3dc0e3f3185705f79cd0ab?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zed/tree/3ab0b5df18031e19b0267b189969ef1a991cd352?format=markdown)
