Repository navigation
Fix infinite recursion detection - #213
Conversation
This detects infinite recursion within the same thread (i.e. when a
thread waits on a thunk that it's already evaluating). This is done by
storing a thread ID in pending/awaited values.
It does not detect cycles between threads, e.g. in `rec { x = y; y =
x; }` if `x` and `y` are marked as pending by different threads. But
those are much less likely in practice.
WalkthroughIntroduces per-eval-thread identifiers and propagates them through thunk waiting. Updates waitOnThunk’s signature to take a PackedPointer instead of a boolean, revises atomic pending/awaited transitions to include thread tags, and adjusts call sites and control flow accordingly across header and implementation. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
actor T as Eval Thread
participant VS as ValueStorage (Value)
participant WT as waitOnThunk(...)
participant D as Domain/CondVar
T->>VS: Force value / CAS to pending
note right of VS: Set pdPending | (myEvalThreadId << discriminatorBits)
T->>WT: waitOnThunk(state, expectedP0 = p0)
WT->>WT: Decode PrimaryDiscriminator from expectedP0
alt Already advanced/finalized
WT-->>T: Return current p0
else Pending/awaited
WT->>WT: Compare threadId in p0 vs myEvalThreadId
opt Detect recursion / self-wait
WT-->>T: Early return / avoid infinite recursion
end
WT->>D: Wait on condvar for completion
D-->>WT: Notified (completed or state changed)
WT-->>T: Return updated p0
end
Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes Possibly related PRs
Suggested reviewers
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests
Tip 👮 Agentic pre-merge checks are now available in preview!Pro plan users can now enable pre-merge checks in their settings to enforce checklists before merging PRs.
Please see the documentation for more information. Example: reviews:
pre_merge_checks:
custom_checks:
- name: "Undocumented Breaking Changes"
mode: "warning"
instructions: |
Pass/fail criteria: All breaking changes to public APIs, CLI flags, environment variables, configuration keys, database schemas, or HTTP/GraphQL endpoints must be documented in the "Breaking Change" section of the PR description and in CHANGELOG.md. Exclude purely internal or private changes (e.g., code not exported from package entry points or explicitly marked as internal).Please share your feedback with us on this Discord post. Comment |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (4)
src/libexpr/include/nix/expr/value.hh (2)
808-809: Rename param to avoid shadowing class member and to match definition.The parameter name ‘p0’ shadows the class member ‘p0’ and doesn’t match the implementation (‘expectedP0’). Rename for clarity.
Apply this diff:
- PackedPointer waitOnThunk(EvalState & state, PackedPointer p0); + PackedPointer waitOnThunk(EvalState & state, PackedPointer expectedP0);
820-822: Keep parameter name consistent with implementation.Mirror the implementation name to reduce confusion.
Apply this diff:
-ValueStorage<sizeof(void *)>::waitOnThunk(EvalState & state, PackedPointer p0); +ValueStorage<sizeof(void *)>::waitOnThunk(EvalState & state, PackedPointer expectedP0);src/libexpr/include/nix/expr/eval-inline.hh (1)
113-117: Use acq_rel on successful CAS when transitioning to pending.Use release semantics on success to follow the “p1 before p0” publishing rule and match other transitions.
Apply this diff:
- if (!p0.compare_exchange_strong( + if (!p0.compare_exchange_strong( p0_, pdPending | (myEvalThreadId << discriminatorBits), - std::memory_order_acquire, + std::memory_order_acq_rel, std::memory_order_acquire)) {src/libexpr/parallel-eval.cc (1)
201-233: Use acq_rel on successful CAS when marking awaited.Release semantics on success better document intent and align with other state transitions.
Apply this diff:
- PackedPointer p0_ = expectedP0; - if (!p0.compare_exchange_strong( - p0_, - pdAwaited | (threadId << discriminatorBits), - std::memory_order_acquire, - std::memory_order_acquire)) { + PackedPointer p0_ = expectedP0; + if (!p0.compare_exchange_strong( + p0_, + pdAwaited | (threadId << discriminatorBits), + std::memory_order_acq_rel, + std::memory_order_acquire)) {
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
src/libexpr/include/nix/expr/eval-inline.hh(3 hunks)src/libexpr/include/nix/expr/value.hh(2 hunks)src/libexpr/parallel-eval.cc(3 hunks)
🧰 Additional context used
🧬 Code graph analysis (3)
src/libexpr/parallel-eval.cc (1)
src/libexpr/include/nix/expr/value.hh (11)
waitOnThunk(821-821)state(156-157)state(169-169)state(174-181)state(799-799)state(808-808)state(1036-1036)static_cast(514-517)p0_(524-540)p0_(524-524)p0(794-797)
src/libexpr/include/nix/expr/eval-inline.hh (2)
src/libexpr/include/nix/expr/value.hh (12)
p0(794-797)p0_(524-540)p0_(524-524)pd(596-619)static_cast(514-517)waitOnThunk(821-821)state(156-157)state(169-169)state(174-181)state(799-799)state(808-808)state(1036-1036)src/libexpr/parallel-eval.cc (2)
waitOnThunk(194-261)waitOnThunk(195-195)
src/libexpr/include/nix/expr/value.hh (1)
src/libexpr/parallel-eval.cc (2)
waitOnThunk(194-261)waitOnThunk(195-195)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: build_aarch64-darwin / build
- GitHub Check: build_x86_64-linux / build
🔇 Additional comments (6)
src/libexpr/include/nix/expr/eval-inline.hh (3)
122-123: Call-site update looks correct.Passing the observed p0_ to waitOnThunk preserves the owner thread tag.
Consider edge-cases where p0_ is stale; the implementation handles this via CAS/early-exit under the domain lock. No action needed.
148-149: Call-site update LGTM.Consistent with the new waitOnThunk contract.
91-97: Single definition ofmyEvalThreadIdconfirmed
Definition only inparallel-eval.cc; extern declaration ineval-inline.hhis correct.src/libexpr/parallel-eval.cc (3)
194-196: Signature specialization update matches header and call sites.Good alignment across declaration, definition, and usage.
236-239: Infinite recursion detection within the same thread: LGTM.Correctly compares owner thread tag with current thread.
190-192: Thread-local ID initialization verified
Search showsmyEvalThreadIdis only referenced inparallel-eval.ccandeval-inline.hh; every evaluation path (including the main thread) odr-uses it, triggering its initialization. No changes needed.
cole-h
left a comment
There was a problem hiding this comment.
Adequately detected infrec in a few cases I've run into recently. Nice!
Motivation
This detects infinite recursion within the same thread (i.e. when a thread waits on a thunk that it's already evaluating). This is done by storing a thread ID in pending/awaited values.
It does not detect cycles between threads, e.g. in
rec { x = y; y = x; }ifxandyare marked as pending by different threads. But those are much less likely in practice.Context
Summary by CodeRabbit