Skip to content

Make the ParallelGuard depth thread-local - #276

Open
JoeyBF wants to merge 1 commit into
SpectralSequences:masterfrom
JoeyBF:claude/parallelguard-thread-local-ne4935
Open

Make the ParallelGuard depth thread-local#276
JoeyBF wants to merge 1 commit into
SpectralSequences:masterfrom
JoeyBF:claude/parallelguard-thread-local-ne4935

Conversation

@JoeyBF

@JoeyBF JoeyBF commented Jul 30, 2026

Copy link
Copy Markdown
Collaborator

PARALLEL_DEPTH was a global static AtomicUsize, so any thread entering a guarded region made every pending step_resolution job in the pool observe is_in_parallel() == true and send_retry itself back to the scheduler — including jobs that were about to run on threads with no guard active and plenty of capacity for long work.

The inversion the guard actually protects against is narrower than that. It happens only when a thread blocked inside a guarded par_iter_mut steals a long job and therefore cannot resume its parent. Stolen jobs run on the OS thread that stole them, so a thread-local depth is exactly the right condition to test. With this change, other threads keep picking up long jobs while a guarded region is active.

Changes

ext/src/utils.rsPARALLEL_DEPTH becomes a thread_local! Cell<usize>. ParallelGuard::new/Drop bump the calling thread's own counter and is_in_parallel() reads only that thread's value.

No changes were needed at the three call sites (ext/src/resolution.rs:764, ext/src/resolution.rs:836, ext/src/nassau.rs:960) — the predicate is unchanged, just correctly scoped.

The guard holds a tracing::span::EnteredSpan, which is !Send, so ParallelGuard is !Send too and always drops on the thread that incremented the counter. The counter therefore cannot underflow. This reasoning is recorded in a doc comment on the struct.

This is a scheduling optimization only — it has no effect on computed results.

Verification

  • cargo build, cargo clippy --all-targets (clean), cargo fmt --check (clean)
  • Full test suite passes, with one exception: test_tempdir_lock in tests/save_load_resolution.rs fails identically on unmodified master (verified by stashing and re-running). It is a should_panic test expecting a file-lock conflict that does not materialize when the suite runs as root in a container, and is unrelated to this change.

Related

The hpc branch approaches the same symptom differently: 71a21b6 removes utils::parallel outright, on the grounds that under the relaxed dependency graph many more step jobs are ready at once and the global counter turned that into a retry storm, while the heavy nassau matrix builds are now GPU-offloaded. That retry storm is the same problem this PR fixes at its root, so it may be worth revisiting whether the removal is still preferable to keeping the thread-local version if hpc merges back.

🤖 Generated with Claude Code

https://claude.ai/code/session_01B8hUmfq86DwNurgutaiL8e


Generated by Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved parallel execution tracking so status is isolated correctly between threads.
    • Enhanced tracing information for nested parallel operations.

`PARALLEL_DEPTH` was a global `AtomicUsize`, so any thread entering a guarded
region made every `step_resolution` job in the pool bounce off `is_in_parallel`
and re-queue itself, even though those jobs would have run on threads that were
free to take long work.

The inversion the guard protects against is narrower than that: it happens only
when a thread blocked inside a guarded `par_iter_mut` steals a long job and so
cannot resume its parent. Stolen jobs run on the thread that stole them, so a
thread-local depth is exactly the condition to test. Other threads keep picking
up long jobs while a guarded region is active.

Replaces the atomic with a `thread_local!` `Cell<usize>`; the guard holds an
`EnteredSpan` and is therefore `!Send`, so it always drops on the thread that
incremented the counter.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B8hUmfq86DwNurgutaiL8e
@coderabbitai

coderabbitai Bot commented Jul 30, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: ad283930-ac84-4ee9-b523-5616b23290f4

📥 Commits

Reviewing files that changed from the base of the PR and between 4867b30 and d5d9ee2.

📒 Files selected for processing (1)
  • ext/src/utils.rs

📝 Walkthrough

Walkthrough

Parallel depth tracking in ext/src/utils.rs now uses a thread-local counter. Guard creation, teardown, tracing values, and is_in_parallel() were updated accordingly.

Changes

Parallel depth tracking

Layer / File(s) Summary
Thread-local guard lifecycle
ext/src/utils.rs
Replaces the global atomic depth with a thread-local Cell<usize>, updates guard increment/decrement tracing, and checks the current thread’s depth in is_in_parallel().

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: hoodmane

Poem

A bunny guards the parallel gate,
With thread-local hops that coordinate.
Up goes the depth when tasks unite,
Down when they finish, neat and light.
No global carrot to synchronize—
Each thread keeps watch with bright eyes.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: ParallelGuard depth tracking is now thread-local.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

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