Skip to content

Wait for nested agent tool cleanup on cancellation - #6130

Open
markstuart-oai wants to merge 2 commits into
feat/simple-codexfrom
fix/join-streamed-agent-tools
Open

markstuart-oai wants to merge 2 commits into
feat/simple-codexfrom
fix/join-streamed-agent-tools

Conversation

@markstuart-oai

@markstuart-oai markstuart-oai commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Cancelling a manager could leave a streamed Agent.as_tool() child running after the caller received CancelledError. Repeated cancellation could also interrupt cleanup, and an event callback that raised during cleanup could receive more queued events.

The manager now gives active delegated calls and their nested tools the SDK's existing 250 ms cleanup grace period. Repeated cancellation leaves the deadline intact. Stream teardown stops new callback deliveries and closes the iterator after its producer stops. Independent runs can keep using the same tool and call ID.

The caller still receives cancellation. If cleanup exceeds the deadline, the SDK stops waiting and observes late errors. An in-flight tool or callback can still run and have side effects. No public API or configuration changes.

Test plan

  • Reproduced early return, stalled cleanup and queued callbacks after cancellation. The regressions cover cooperative and stalled tools, repeated cancellation, callback failure, queue overflow and independent managers.
  • Checked approval and resume, child turn limits, context and usage isolation, and return of the original result to the manager.
  • Passed 972 focused tests on Python 3.12 and 114 agent-tool and task-helper tests on Python 3.10. The cleanup tests check cancellation before task forwarding because Python 3.10 can drop its message.
  • Full repository verification passed with 13,226 tests passing and 37 skipped.

Checks

  • Added regression coverage
  • Ran .agents/skills/code-change-verification/scripts/run.sh
  • Confirmed all repository verification steps pass
  • Completed two independent reviews of the full diff

@markstuart-oai
markstuart-oai requested review from a team, rm-openai and seratch as code owners November 21, 2026 06:03
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-21T14:18:31.889577Z 1393814 New commits
🔒 Security Review ✅ Completed 2026-11-21T14:19:28.354144Z 1393814 New commits

Security findings

Advisory findings (1)

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛡️ Codex Security Review · Automatically triggered

Here are some automated security review suggestions for this pull request.

Reviewed commit: dd7f90bcaf

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

Comment thread src/agents/util/_asyncio_tasks.py Resolved

@jbeckwith-oai jbeckwith-oai left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved after comprehensive static review and an independent second review. Reviewed attached task ownership, repeated cancellation, producer/consumer teardown, iterator closure, nested stream settlement, provider/sandbox cleanup, and concurrent manager isolation. No confirmed actionable defect in the reviewed changes.

Rechecked the cleanup availability finding. The mechanism is real: a nested application tool that never finishes cleanup can delay cancellation indefinitely, including repeated cancellation. The PR explicitly documents this tradeoff in order to preserve ownership of delegated work. The finding does not identify a concrete SDK-owned cleanup path stalled by untrusted input or an applicable bounded-cancellation guarantee; an unsafe application-defined cleanup alone does not establish an SDK security boundary bypass. This approval does not claim bounded cancellation latency. A concrete built-in cleanup operation lacking an I/O timeout would merit separate review.

Hosted CI is complete: 33 passed and 4 skipped; no failures. Static review only; no candidate code or local tests executed.

@jbeckwith-oai jbeckwith-oai left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved after static re-review and an independent second review of the update. The cleanup wait now uses a fixed 250 ms grace period, repeated cancellation does not extend that deadline, and late task errors are observed. The stopped flag prevents queued callbacks from being delivered after failure even when an in-flight callback suppresses cancellation or raises during cleanup. Iterator closure remains sequenced after its producer stops; callers owning closure externally retain a full join. No actionable findings.

This addresses the unbounded-wait mechanism in the earlier cleanup finding. As documented, timing out the wait does not stop an uncooperative application tool or callback from continuing or causing side effects. Reviewed cooperative/stalled cleanup, repeated cancellation, overflow, late callback failure, and independent-manager regression coverage.

Hosted CI is complete: 33 passed and 4 skipped; no failures. Static review only; no candidate code or local tests executed.

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.

2 participants