Skip to content

fix(adk): avoid panic on closed agent tool event stream - #1241

Open
Slimzeo wants to merge 1 commit into
cloudwego:mainfrom
Slimzeo:fix-agent-tool-send
Open

fix(adk): avoid panic on closed agent tool event stream#1241
Slimzeo wants to merge 1 commit into
cloudwego:mainfrom
Slimzeo:fix-agent-tool-send

Conversation

@Slimzeo

@Slimzeo Slimzeo commented Sep 2, 2026

Copy link
Copy Markdown

What type of PR is this?

fix

Check the PR title.

  • This PR title match the format: (optional scope):
  • The description of this PR title is user-oriented and clear enough for others to understand.
  • User documentation is not required because this is an internal bug fix with no API or configuration changes.

(Optional) Translate the PR title into Chinese.

fix(adk): 避免 agent tool 向已关闭的事件流发送事件时触发 panic

(Optional) More detailed description for this PR(en: English/zh: Chinese).

en:

Agent-tool events can arrive after the parent event consumer closes its generator during cancellation. InvokableRun forwarded these events through Send, which panics when the generator is already closed.

Use the existing non-panicking trySend path for this best-effort event forwarding, consistent with the existing chat-model event sender. A closed parent event stream does not stop the wrapped agent from completing and returning its final tool result.

The regression test controls the relevant ordering without timing sleeps:

  1. the wrapped agent starts;
  2. the parent event generator closes;
  3. the inner agent emits its event;
  4. InvokableRun completes without panic and returns the final result.

zh(optional):

取消过程中,agent tool 的事件可能在父级消费者关闭 generator 后到达。原先 InvokableRun 使用 Send 转发事件,会在 generator 已关闭时触发 panic。

本修改复用已有的非 panic trySend 路径,与 chat-model 事件发送逻辑保持一致。父级事件流关闭不会阻止被包装的 agent 完成执行并返回最终工具结果。

Validation:

  • go test ./adk -run 'TestAgentTool_InvokableRun_(ParentGeneratorClosesWhileAgentRuns|FinalOnly)$' -count=100
  • go test -race ./adk -run '^TestAgentTool_InvokableRun_ParentGeneratorClosesWhileAgentRuns$' -count=100
  • go test ./adk
  • gofmt
  • git diff --check

(Optional) Which issue(s) this PR fixes:

Fixes #1236

(optional) The PR that updates user documentation:

Not required; this fix does not change public APIs, configuration, or user workflows.

@CLAassistant

CLAassistant commented Sep 2, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@Slimzeo
Slimzeo force-pushed the fix-agent-tool-send branch from 2c1410c to 2ba0157 Compare September 2, 2026 17:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

panic: send on closed channel in typedAgentTool.InvokableRun (adk/agent_tool.go:243) after cancellation — path missed by #929

2 participants