Skip to content

Route open_thread through load_agent_thread to prevent duplicate sessions#53859

Merged
rtfeldman merged 4 commits intomainfrom
fix-session-not-found-race
Apr 14, 2026
Merged

Route open_thread through load_agent_thread to prevent duplicate sessions#53859
rtfeldman merged 4 commits intomainfrom
fix-session-not-found-race

Conversation

@rtfeldman
Copy link
Copy Markdown
Contributor

@rtfeldman rtfeldman commented Apr 14, 2026

AgentPanel::open_thread (called when clicking @thread mention links, pasting thread links, or opening threads via the CLI) was calling external_thread directly, bypassing the de-duplication guards in load_agent_thread. This meant clicking a thread link to the currently-active thread would create a redundant ConversationView sharing the same NativeAgent session. When the old CV was later evicted from background_threads, its on_release called close_all_sessions, removing the session and breaking the surviving CV with "Session not found".

Fix this by routing open_thread through load_agent_thread, which checks the active view, retained threads, and background threads before creating a new ConversationView. This prevents the duplicate from being created in the first place.

Release Notes:

  • Fixed a "Session not found" error that could occur after clicking a thread mention link.

@rtfeldman rtfeldman self-assigned this Apr 14, 2026
@cla-bot cla-bot Bot added the cla-signed The user has signed the Contributor License Agreement label Apr 14, 2026
@zed-community-bot zed-community-bot Bot added the staff Pull requests authored by a current member of Zed staff label Apr 14, 2026
When two ConversationViews share the same session (because open_thread
found the session still in the HashMap and reused it), closing the
session from the first view removes the entry entirely, breaking the
second view's ability to prompt.

This test reproduces the race deterministically:
1. CV1 creates a session
2. CV2 calls open_thread, reuses the existing session
3. CV1 calls close_session, removing the entry
4. CV2 tries to prompt — gets "Session not found"
When multiple ConversationViews share the same NativeAgent session
(because open_thread found it still in the HashMap), a close_session
call from one view would remove the entry, breaking all other views
with "Session not found".

Add a ref_count to Session: register_session starts at 1, open_thread
increments when reusing an existing session, and close_session only
removes the entry when the count reaches zero.
@rtfeldman rtfeldman force-pushed the fix-session-not-found-race branch from 16c3d7c to a12684d Compare April 14, 2026 03:57
Add a comment to test_close_session_does_not_remove_reused_session
explaining how clicking a @thread mention link (or pasting a thread
link, or opening one via the CLI) to the currently-active thread
can produce two ConversationViews sharing the same NativeAgent
session, leading to the race that the ref-counting fix prevents.
…ions

AgentPanel::open_thread (called when clicking @thread mention links,
pasting thread links, or opening threads via the CLI) was calling
external_thread directly, bypassing the de-duplication guards in
load_agent_thread. This meant clicking a thread link to the
currently-active thread would create a redundant ConversationView
sharing the same NativeAgent session. When the old CV was later
evicted from background_threads, its on_release called
close_all_sessions, removing the session and breaking the surviving
CV with "Session not found".

Fix this by routing open_thread through load_agent_thread, which
checks the active view, retained threads, and background threads
before creating a new ConversationView. This prevents the duplicate
from being created in the first place.

Also removes the Session ref_count that was added as a workaround
for this bug, since it is no longer needed.
@rtfeldman rtfeldman changed the title Fix session-not-found race when multiple views share a session Route open_thread through load_agent_thread to prevent duplicate sessions Apr 14, 2026
@rtfeldman rtfeldman marked this pull request as ready for review April 14, 2026 19:05
@rtfeldman rtfeldman merged commit 5d15e01 into main Apr 14, 2026
33 checks passed
@rtfeldman rtfeldman deleted the fix-session-not-found-race branch April 14, 2026 19:06
@rtfeldman rtfeldman requested a review from benbrandt April 14, 2026 19:06
@mikayla-maki
Copy link
Copy Markdown
Member

@zed-industries/approved

piper-of-dawn pushed a commit to piper-of-dawn/zed that referenced this pull request Apr 25, 2026
…ions (zed-industries#53859)

`AgentPanel::open_thread` (called when clicking `@thread` mention links,
pasting thread links, or opening threads via the CLI) was calling
`external_thread` directly, bypassing the de-duplication guards in
`load_agent_thread`. This meant clicking a thread link to the
currently-active thread would create a redundant `ConversationView`
sharing the same `NativeAgent` session. When the old CV was later
evicted from `background_threads`, its `on_release` called
`close_all_sessions`, removing the session and breaking the surviving CV
with "Session not found".

Fix this by routing `open_thread` through `load_agent_thread`, which
checks the active view, retained threads, and background threads before
creating a new `ConversationView`. This prevents the duplicate from
being created in the first place.

Release Notes:

- Fixed a "Session not found" error that could occur after clicking a
thread mention link.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla-signed The user has signed the Contributor License Agreement staff Pull requests authored by a current member of Zed staff

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants