Skip to content

fix(adk): add nil checks for execContext in ChatModelAgent Run and Resume#758

Merged
shentongmartin merged 1 commit intoalpha/08from
fix/ec_panic
Feb 4, 2026
Merged

fix(adk): add nil checks for execContext in ChatModelAgent Run and Resume#758
shentongmartin merged 1 commit intoalpha/08from
fix/ec_panic

Conversation

@shentongmartin
Copy link
Contributor

Summary

Problem Solution
Potential nil pointer dereference when prepareExecContext fails Add nil checks for execContext in getRunFunc, Run, and Resume methods

Key Insight

When prepareExecContext() fails (e.g., when a tool's Info() method returns an error), a.exeCtx remains nil while a.run is set to errFunc(err). The original code assumed bc (execContext) would always be non-nil after getRunFunc() returned, leading to potential nil pointer dereferences when accessing bc.toolInfos, bc.instruction, etc.

The fix adds defensive nil checks at three locations:

  1. getRunFunc(): Early return when bc is nil
  2. Run(): Guard bc field access with nil check
  3. Resume(): Guard bc field access with nil check

This ensures the error is properly propagated through the event iterator instead of causing a panic.

Testing

Added TestChatModelAgent_PrepareExecContextError with two sub-tests:

  • Run_WithToolInfoError_ReturnsError: Verifies Run() properly returns error
  • Resume_WithToolInfoError_ReturnsError: Verifies Resume() properly returns error

摘要

问题 解决方案
prepareExecContext 失败时可能出现空指针解引用 getRunFuncRunResume 方法中添加 nil 检查

关键洞察

prepareExecContext() 失败时(例如,当工具的 Info() 方法返回错误时),a.exeCtx 保持为 nil,而 a.run 被设置为 errFunc(err)。原始代码假设 getRunFunc() 返回后 bc(execContext)始终非空,导致访问 bc.toolInfosbc.instruction 等字段时可能出现空指针解引用。

修复在三个位置添加了防御性 nil 检查:

  1. getRunFunc():当 bc 为 nil 时提前返回
  2. Run():使用 nil 检查保护 bc 字段访问
  3. Resume():使用 nil 检查保护 bc 字段访问

这确保错误通过事件迭代器正确传播,而不是导致 panic。

测试

添加了 TestChatModelAgent_PrepareExecContextError 测试,包含两个子测试:

  • Run_WithToolInfoError_ReturnsError:验证 Run() 正确返回错误
  • Resume_WithToolInfoError_ReturnsError:验证 Resume() 正确返回错误

…sume

- Add early return in getRunFunc when execContext is nil
- Guard execContext field access with nil checks in Run method
- Guard execContext field access with nil checks in Resume method
- Add tests for prepareExecContext error handling edge case

This prevents potential nil pointer dereferences when prepareExecContext
fails (e.g., when a tool's Info method returns an error).~

Change-Id: I36ef545ff44db57343ceb14c8883380a7c38a243
@shentongmartin shentongmartin merged commit 9f2d714 into alpha/08 Feb 4, 2026
30 of 32 checks passed
@shentongmartin shentongmartin deleted the fix/ec_panic branch February 4, 2026 13:33
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.

2 participants