Skip to content

Avoid stack overflow in nested asyncio task chains - #704

Merged
davidhewitt merged 2 commits into
pydantic:mainfrom
jay3cx:codex/fix-async-exception-stack-overflow
Aug 17, 2026
Merged

Avoid stack overflow in nested asyncio task chains#704
davidhewitt merged 2 commits into
pydantic:mainfrom
jay3cx:codex/fix-async-exception-stack-overflow

Conversation

@jay3cx

@jay3cx jay3cx commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

The waiter-error path re-entered VM::handle_exception recursively, adding one native Rust stack frame for each nested asyncio.gather waiter.

This changes waiter error propagation to an iterative loop while preserving the existing exception handling and ownership behavior.

A regression test covers a 4,000-level nested asyncio.gather chain and verifies that the original ValueError remains catchable.

Tests

  • cargo test -p monty --test asyncio
  • cargo test -p monty

Fixes #649


Summary by cubic

Prevents stack overflow when exceptions propagate through deep asyncio.gather waiter chains by switching from recursive to iterative handling. Preserves exception behavior; adds a regression test.

  • Bug Fixes
    • Made handle_exception_with_value iterative via a loop over handle_exception_step.
    • Added ExceptionHandlingResult with Caught, Unhandled, and PropagateToWaiter to control propagation and handle waiter handoff without growing the stack.
    • Added a regression test for a 4,000-level nested asyncio.gather; verifies the original ValueError is catchable.

Written for commit 4f92024. Summary will update on new commits.

Review in cubic

@codecov

codecov Bot commented Aug 10, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.85714% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/monty/src/bytecode/vm/exceptions.rs 92.85% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@cubic-dev-ai cubic-dev-ai Bot 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.

No issues found across 2 files

Re-trigger cubic

@codspeed-hq

codspeed-hq Bot commented Aug 10, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 34 untouched benchmarks
⏩ 16 skipped benchmarks1


Comparing jay3cx:codex/fix-async-exception-stack-overflow (4f92024) with main (ca9cca0)

Open in CodSpeed

Footnotes

  1. 16 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@rewitt94 rewitt94 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.

Thanks for contributing! This looks good to me!

Comment thread crates/monty/src/bytecode/vm/exceptions.rs
Comment thread crates/monty/tests/asyncio.rs Outdated

@jay3cx jay3cx left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

@jay3cx

jay3cx commented Aug 14, 2026

Copy link
Copy Markdown
Contributor Author

@rewitt94 Review comments are in 4f92024. Mind taking another look when you have a minute?

@samuelcolvin samuelcolvin mentioned this pull request Aug 14, 2026
@rewitt94

Copy link
Copy Markdown
Contributor

Thanks @jay3cx! I will forward this to @davidhewitt

@davidhewitt
davidhewitt merged commit b1cda23 into pydantic:main Aug 17, 2026
42 checks passed
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.

CREATED BY CLAUDE: stack overflow (SIGABRT) from unbounded handle_exception recursion on deep async task chains

3 participants