Route open_thread through load_agent_thread to prevent duplicate sessions#53859
Merged
Route open_thread through load_agent_thread to prevent duplicate sessions#53859
Conversation
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.
16c3d7c to
a12684d
Compare
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.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
AgentPanel::open_thread(called when clicking@threadmention links, pasting thread links, or opening threads via the CLI) was callingexternal_threaddirectly, bypassing the de-duplication guards inload_agent_thread. This meant clicking a thread link to the currently-active thread would create a redundantConversationViewsharing the sameNativeAgentsession. When the old CV was later evicted frombackground_threads, itson_releasecalledclose_all_sessions, removing the session and breaking the surviving CV with "Session not found".Fix this by routing
open_threadthroughload_agent_thread, which checks the active view, retained threads, and background threads before creating a newConversationView. This prevents the duplicate from being created in the first place.Release Notes: