Skip to content

Fix close_session final save being a guaranteed no-op#52030

Merged
rtfeldman merged 1 commit intomainfrom
AI-95/close-session-save-data-loss
Mar 23, 2026
Merged

Fix close_session final save being a guaranteed no-op#52030
rtfeldman merged 1 commit intomainfrom
AI-95/close-session-save-data-loss

Conversation

@rtfeldman
Copy link
Copy Markdown
Contributor

close_session removed the session from self.sessions before calling save_thread, but save_thread immediately looks up the session in self.sessions and returns early if it's not found. This meant the explicit final save was always a no-op.

Additionally, when the local session variable was dropped at the end of the closure, it cancelled any in-flight save from a prior observe callback by dropping session.pending_save.

So on session close:

  • The explicit final save was a guaranteed no-op
  • Any in-flight save from an earlier observation was cancelled

Fix: Call save_thread while the session is still in the map, then extract the pending_save task and return it so the caller can actually await the save completing.

Test: Added test_close_session_saves_thread which sets a draft prompt without a run_until_parked() before close_session, so the only way the data gets persisted is if close_session itself performs the save. Also strengthened the existing test_save_load_thread similarly.

Closes AI-95

Release Notes:

  • Fixed a bug where closing an agent thread could lose unsaved changes (e.g. draft prompts) made in the same frame.

@rtfeldman rtfeldman self-assigned this Mar 20, 2026
@cla-bot cla-bot bot added the cla-signed The user has signed the Contributor License Agreement label Mar 20, 2026
@zed-community-bot zed-community-bot bot added the staff Pull requests authored by a current member of Zed staff label Mar 20, 2026
close_session removed the session from the map before calling
save_thread, which immediately tried to look it up via get_mut.
The lookup always failed, making the final save a no-op.

Fix by calling save_thread before removing the session, then
returning the pending_save task so the caller awaits the DB write.

Also changes pending_save from Task<()> to Task<Result<()>> to
avoid an unnecessary cx.spawn wrapper in close_session.
@rtfeldman rtfeldman force-pushed the AI-95/close-session-save-data-loss branch from 7bf9442 to 8f9202e Compare March 23, 2026 14:56
@rtfeldman rtfeldman marked this pull request as ready for review March 23, 2026 15:47
@rtfeldman rtfeldman merged commit 2938ab9 into main Mar 23, 2026
31 checks passed
@rtfeldman rtfeldman deleted the AI-95/close-session-save-data-loss branch March 23, 2026 15:48
@zed-codeowner-coordinator zed-codeowner-coordinator bot requested review from a team, danilo-leal and probably-neb and removed request for a team March 23, 2026 15:48
AmaanBilwar pushed a commit to AmaanBilwar/zed that referenced this pull request Mar 23, 2026
…#52030)

`close_session` removed the session from `self.sessions` before calling
`save_thread`, but `save_thread` immediately looks up the session in
`self.sessions` and returns early if it's not found. This meant the
explicit final save was always a no-op.

Additionally, when the local `session` variable was dropped at the end
of the closure, it cancelled any in-flight save from a prior observe
callback by dropping `session.pending_save`.

So on session close:
- The explicit final save was a guaranteed no-op
- Any in-flight save from an earlier observation was cancelled

**Fix:** Call `save_thread` while the session is still in the map, then
extract the `pending_save` task and return it so the caller can actually
await the save completing.

**Test:** Added `test_close_session_saves_thread` which sets a draft
prompt without a `run_until_parked()` before `close_session`, so the
only way the data gets persisted is if `close_session` itself performs
the save. Also strengthened the existing `test_save_load_thread`
similarly.

Closes AI-95

Release Notes:

- Fixed a bug where closing an agent thread could lose unsaved changes
(e.g. draft prompts) made in the same frame.
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.

3 participants