Repository navigation
fix(samples): serialize chat state writes in the FastAPI sample - #8436
Open
电大老baby (HhH666-ux) wants to merge 1 commit into
Open
电大老baby (HhH666-ux) wants to merge 1 commit into
电大老baby (HhH666-ux) wants to merge 1 commit into
Conversation
电大老baby (HhH666-ux)
force-pushed
the
fix/fastapi-sample-state-race
branch
from
November 6, 2026 04:50
ce7bc95 to
3ec1bbb
Compare
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.
Summary
Every
/chatrequest reads and rewrites the sharedagent_state.json/agent_history.jsonwithout synchronization. When two requests overlap, their writes can leave a mixture of two JSON documents, and the next request fails withJSONDecodeError: Extra data; the history can also lose updates.This serializes the read-modify-write cycle behind an
asyncio.Lockand writes each document through a temporary file plusos.replace, so a killed or concurrent writer cannot leave a truncated file. The history entries now usemodel_dump(mode="json"), because the previousmodel_dump()kept datetimes andjson.dumpsraised before the write happened.Tests
pytest python/samples/agentchat_fastapi/test_app_agent.py -q— 1 passed/chatrequests and asserts every response is 200 and all 20 history entries are present. Without the lock the same test returned 500s and kept only 2 of 20 entries.ruff checkandruff format --check(0.4.8, the repo pin) pass on the changed files.Fixes #8435