Skip to content

Bound stack depth from nested parallelism - #9378

Open
abadams wants to merge 2 commits into
mainfrom
abadams/nested_parallel_unbounded_recursion
Open

Bound stack depth from nested parallelism#9378
abadams wants to merge 2 commits into
mainfrom
abadams/nested_parallel_unbounded_recursion

Conversation

@abadams

@abadams abadams commented Aug 21, 2026

Copy link
Copy Markdown
Member

A thread that stalls waiting on a parallel loop it owns may run tasks from other parallel regions rather than idle. That is worth keeping: with a small inner loop, starting another instance of the outer loop is the only work available. But each one nests a new owned job inside the one already being waited on, and that stack frame cannot unwind until the new job completes, so a single thread could accumulate one frame per outer loop iteration.

Track how many of the jobs each thread owns have stalled, in an array indexed by a hash of the thread id, and let a stalled job start work from another region only while its thread is below the limit of two. A thread can still take one more instance of a loop it is already inside, but not an unbounded number of them. Descending without stalling is already bounded, since it can only go one level deeper into the loop nest, so per-thread stack depth is now bounded by the pipeline's static loop nest depth rather than by loop extents.

The thread id is only fetched when a job first looks outside its own region, which on Linux costs a syscall. A pipeline whose parallel loops aren't nested never gets that far.

Fixes #9377

A thread that stalls waiting on a parallel loop it owns may run tasks from
other parallel regions rather than idle. That is worth keeping: with a small
inner loop, starting another instance of the outer loop is the only work
available. But each one nests a new owned job inside the one already being
waited on, and that stack frame cannot unwind until the new job completes, so
a single thread could accumulate one frame per outer loop iteration.

Track how many of the jobs each thread owns have stalled, in an array indexed
by a hash of the thread id, and let a stalled job start work from another
region only while its thread is below the limit of two. A thread can still
take one more instance of a loop it is already inside, but not an unbounded
number of them. Descending without stalling is already bounded, since it can
only go one level deeper into the loop nest, so per-thread stack depth is now
bounded by the pipeline's static loop nest depth rather than by loop extents.

The thread id is only fetched when a job first looks outside its own region,
which on Linux costs a syscall. A pipeline whose parallel loops aren't nested
never gets that far.

Fixes #9377

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
@codecov

codecov Bot commented Aug 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 70.02%. Comparing base (5c21c82) to head (7979ad7).
⚠️ Report is 9 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #9378      +/-   ##
==========================================
- Coverage   70.08%   70.02%   -0.07%     
==========================================
  Files         260      260              
  Lines       79287    79287              
  Branches    19327    19327              
==========================================
- Hits        55569    55521      -48     
- Misses      17923    17932       +9     
- Partials     5795     5834      +39     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

The test defines an extern that the pipeline calls, so the JIT has to find
it in the running process.

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
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.

Nested parallelism risks stack overflow

2 participants