Skip to content

fix(samples): serialize chat state writes in the FastAPI sample - #8436

Open
电大老baby (HhH666-ux) wants to merge 1 commit into
microsoft:mainfrom
HhH666-ux:fix/fastapi-sample-state-race
Open

电大老baby (HhH666-ux) wants to merge 1 commit into
microsoft:mainfrom
HhH666-ux:fix/fastapi-sample-state-race

Conversation

@HhH666-ux

Copy link
Copy Markdown

Summary

Every /chat request reads and rewrites the shared agent_state.json / agent_history.json without synchronization. When two requests overlap, their writes can leave a mixture of two JSON documents, and the next request fails with JSONDecodeError: Extra data; the history can also lose updates.

This serializes the read-modify-write cycle behind an asyncio.Lock and writes each document through a temporary file plus os.replace, so a killed or concurrent writer cannot leave a truncated file. The history entries now use model_dump(mode="json"), because the previous model_dump() kept datetimes and json.dumps raised before the write happened.

Tests

  • pytest python/samples/agentchat_fastapi/test_app_agent.py -q — 1 passed
  • The test posts 10 concurrent /chat requests 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 check and ruff format --check (0.4.8, the repo pin) pass on the changed files.

Fixes #8435

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Concurrent requests can corrupt state files in the AutoGen FastAPI sample

1 participant