fix(daemon): snapshot state before threaded save to end tick crash#47
Open
Marsu6996 wants to merge 1 commit into
Open
fix(daemon): snapshot state before threaded save to end tick crash#47Marsu6996 wants to merge 1 commit into
Marsu6996 wants to merge 1 commit into
Conversation
save_state ran in a worker thread (via asyncio.to_thread) over the live `state` dict while the event loop kept mutating it, so json.dump could hit "RuntimeError: dictionary changed size during iteration". That crash propagated out of the scheduler tick and silently froze consolidation: episodes kept being captured but never got consolidated/indexed, so recall only ever returned older, already-consolidated memories. It surfaces under concurrent load (several MCP wrappers hitting the daemon at once). Route every threaded save through a new _persist() helper that deepcopies `state` synchronously in the event-loop thread — atomic w.r.t. other coroutines — then hands the isolated snapshot to the writer thread. Covers all save_state offload sites. Adds a regression test. Designed by Marsu — Refined by Claude. Co-Authored-By: Claude <noreply@anthropic.com>
1e05d39 to
91e1392
Compare
This was referenced Jul 5, 2026
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.
Problem
save_stateis offloaded to a worker thread (asyncio.to_thread(save_state, state)) while the event loop keeps mutating the samestatedict.json.dumpiterating that live dict can raise:The exception propagates out of the scheduler tick, so consolidation silently stops: episodes keep being captured, but they're never consolidated/indexed, and
memory_recallonly returns older, already-consolidated memories. It shows up under concurrent load (several MCP wrappers hitting the daemon at once), which is why it can look fine on a lightly-used setup and stall on a busy one.Fix
Add a small
_persist()helper that takes an atomic snapshot in the event-loop thread before offloading:deepcopyruns synchronously in the loop thread, so no other coroutine can mutatestatemid-copy; the writer thread then serialises an isolated snapshot. Allsave_stateoffload sites route through it. State is JSON-serialisable so the copy is cheap.Test
tests/test_persist_state_snapshot.py— asserts the writer receives a deep copy (not the live dict) and that mutatingstateafter the snapshot can't change what lands on disk. Fails if a call site reverts to passing the livestate.No version bump — leaving the release to you. Happy to rebase/trim or let you reimplement, whatever's least work.
🤖 Designed by Marsu — Refined by Claude