Repository navigation
fix: serialize state and history writes in the agentchat FastAPI sample - #8437
Open
inchang-ing wants to merge 1 commit into
Open
inchang-ing wants to merge 1 commit into
inchang-ing wants to merge 1 commit into
Conversation
Concurrent /chat requests wrote agent_state.json and agent_history.json with a plain "w" open, so overlapping requests could interleave their bytes and leave a file that no longer parses. The history was also read-modify-written without any mutual exclusion, so every request but the last dropped its entries. Both writes now go through a single asyncio lock and land via a temp file plus os.replace, so readers never observe a partial document.
This branch has not been deployed
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.
What this fixes
python/samples/agentchat_fastapi/app_agent.pypersists the agent state and the chat history with a plain"w"open, from a handler FastAPI runs concurrently:await agent.save_state()thenopen(state_path, "w")get_history()->appendtwo messages ->open(history_path, "w")Two overlapping
/chatrequests therefore interleave their bytes, and one of them canjson.loads()a file that another request is still truncating. The history write is additionally a read-modify-write over a shared file with no mutual exclusion, so a request that started from a stale snapshot overwrites the entries a concurrent request just appended.What changed
_write_json_atomic()writes the payload to atempfile.mkstemp()file in the same directory andos.replace()s it into place, so a reader sees either the previous document or the new one, never a half-written file.save_state()andappend_history()take one module-levelasyncio.Lockaround the read-modify-write, so concurrent requests no longer drop each other's history entries.chat()now calls those two helpers instead of opening the files itself; the rest of the sample is untouched.How I verified it
I ran the
chat()handler from this exact file underasyncio.gather()with only the model call stubbed out (on_messages()returns immediately), so all the file writes are the real ones. 12 concurrent requests:before (file as it is on
main), three runs:JSONDecodeError: Expecting value: line 1 column 1when reading a file mid-write (andPermission deniedwhen two writers opened the same path at once on Windows)agent_history.jsonended up with 4, 4 and 6 of the expected 24 entriesafter, three runs:
ruff checkpasses with the version pinned inpython/pyproject.toml(0.4.8), andpyrightin strict mode reports no errors for the file.The sibling
app_team.pysample writes both files the same way; I left it out to keep this change on the sample the report is about, and can extend it here or in a separate PR.The change and the reproduction above were prepared with AI assistance.