fix: acquire session lock in background and cron wake paths - #9671
Open
T52T52 wants to merge 3 commits into
Open
fix: acquire session lock in background and cron wake paths#9671T52T52 wants to merge 3 commits into
T52T52 wants to merge 3 commits into
Conversation
The background wake path (_wake_main_agent_for_background_result) bypassed session_lock_manager, so it could run concurrently with user message processing on the same conversation, causing stale LLM context and lost conversation history (persist race). Wrap the read-build-run-persist flow in the per-session lock, matching the user message path in internal.py. - test_background_wake_lock: asserts acquire_lock is called - test_persist_race: verifies the lock prevents concurrent overwrites
The cron wake path (_woke_main_agent) also bypassed session_lock_manager, running concurrently with user message processing on the same conversation. Wrap the read-build-run-persist flow in the per-session lock, matching the user message path in internal.py. - test_cron_wake_lock: asserts acquire_lock is called
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.
问题
后台任务唤醒路径(wakemainagentforbackgroundresult)和定时任务唤醒路径(wokemain_agent)都绕过了 sessionlockmanager,导致同一会话的 agent 工作流与用户消息处理并发执行,造成:
复现步骤
1.让llm同时调用两个或以上子agent
2. 多个子 agent 同时返回结果,主 agent 的唤醒路径并发触发
3.历史被覆盖,llm只有最后一个子agent回复的上下文
根因
用户消息处理路径(agentsubstages/internal.py)将整个 agent 流程包在 sessionlockmanager.acquirelock(unifiedmsg_origin) 中,但两条唤醒路径直接调用 buildmainagent,没有获取会话锁。
修复
将两条唤醒路径的「读取-构建-运行-持久化」流程包进 per-session 锁中,与用户消息路径保持一致。
测试
全部 4 个测试通过。修复前 testbackgroundwake_lock 和 testcronwake_lock 失败(acquirelock 调用 0 次),testpersistrace 丢失 3/4 并发结果。
Summary by Sourcery
Guard background and cron agent wake workflows with per-session locking to prevent concurrent access to conversation history.
Bug Fixes:
Tests: