fix(adk): fix concurrent compile race in ChatModelAgent#775
Merged
Conversation
Move chain and inner graph creation inside closures to avoid data races when multiple requests call Compile() concurrently on shared instances. Changes: - buildNoToolsRunFunc: create chain per request instead of sharing - buildReactRunFunc: create both inner graph (g) and chain per request Change-Id: I9a44697675c892ac7658909010b72d3a2718c25a
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## alpha/08 #775 +/- ##
===========================================
Coverage ? 79.79%
===========================================
Files ? 146
Lines ? 15844
Branches ? 0
===========================================
Hits ? 12642
Misses ? 2218
Partials ? 984 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
N3kox
approved these changes
Feb 6, 2026
hi-pender
pushed a commit
that referenced
this pull request
Feb 14, 2026
Move chain and inner graph creation inside closures to avoid data races when multiple requests call Compile() concurrently on shared instances. Changes: - buildNoToolsRunFunc: create chain per request instead of sharing - buildReactRunFunc: create both inner graph (g) and chain per request Change-Id: I9a44697675c892ac7658909010b72d3a2718c25a
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
chain.Compile()concurrently on shared chain/graph instances inChatModelAgentProblem
In
ChatModelAgent, bothbuildNoToolsRunFuncandbuildReactRunFunccreate a sharedchain(and inner graphgfor react) outside the closure, then callchain.Compile()inside. When multiple requests execute concurrently, they race on:chain.addEndIfNeeded()- modifieshasEndflaggraph.compile()- modifies internal state likecompiled,handlerPreNodeinnerGraph.compile()- nested graph also gets compiled with racesSolution
Create chain and inner graph per request inside the closure:
Key Insight
Neither
ChainnorGraphin the compose package supports concurrentCompile()calls on the same instance. The compile operation modifies internal state without synchronization. Creating instances per request is the safe pattern when each request may have different compile options (likeWithCheckPointStore(store)).概述
chain.Compile()时存在数据竞争问题
在
ChatModelAgent中,buildNoToolsRunFunc和buildReactRunFunc都在闭包外创建共享的chain(react 模式还有内部 graphg),然后在闭包内调用chain.Compile()。当多个请求并发执行时,会在以下位置产生竞争:chain.addEndIfNeeded()- 修改hasEnd标志graph.compile()- 修改内部状态如compiled、handlerPreNodeinnerGraph.compile()- 嵌套的 graph 也会被编译并产生竞争解决方案
在闭包内为每个请求创建 chain 和内部 graph。
关键洞察
compose 包中的
Chain和Graph都不支持在同一实例上并发调用Compile()。编译操作会修改内部状态而没有同步机制。当每个请求可能有不同的编译选项时(如WithCheckPointStore(store)),为每个请求创建新实例是安全的模式。