Skip to content

Fix data race initializing AttrCursor::_value#557

Merged
edolstra merged 1 commit into
mainfrom
attr-cursor-value-lock
Jul 13, 2026
Merged

Fix data race initializing AttrCursor::_value#557
edolstra merged 1 commit into
mainfrom
attr-cursor-value-lock

Conversation

@edolstra

@edolstra edolstra commented Jul 13, 2026

Copy link
Copy Markdown
Collaborator

Motivation

This field could be lazily initialized by multiple threads, clobbering each other's RootValue. Since #546 this has been much more likely to lead to a crash. (Previously RootValue was an std::shared_ptr, which was not safe either but unlikely to crash.)

Context

Summary by CodeRabbit

  • Bug Fixes
    • Improved evaluation cache synchronization and value handling.
    • Prevented potential inconsistencies when cached expressions and attribute values are evaluated concurrently.
    • Improved reliability when retrieving cached root and attribute values.

AttrCursor::_value and EvalCache::value were lazily initialized
without any synchronization, while `nix flake check/show` call into
cursors shared between evaluator threads (e.g. the flake outputs
cursor used by every Leaf::derivation() call). Two threads could
both see an empty RootValue and move-assign it concurrently,
double-freeing a root value slot. Since the root value pool uses an
intrusive freelist, a double free corrupts the freelist and makes a
subsequent allocRootValueSlot() chase a garbage next pointer,
crashing with EXC_BAD_ACCESS (e.g. at address 0x28, a Value
discriminator word read as a Slot pointer).

Wrap both members in Sync so initialization happens exactly once and
concurrent callers block until the value is available. The lock is
held while the value is computed; lock ordering is strictly
child -> parent (and thunk waiting in parallel eval blocks on a
condition variable, without work stealing), so this cannot deadlock.

Note: AttrCursor::cachedValue has a similar unsynchronized lazy
initialization pattern that still needs fixing.

Assisted-by: Claude Fable 5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jul 13, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 0ae66a9f-c904-4183-be72-5ba5b0836b81

📥 Commits

Reviewing files that changed from the base of the PR and between a2daf9e and 50d27f9.

📒 Files selected for processing (2)
  • src/libexpr/eval-cache.cc
  • src/libexpr/include/nix/expr/eval-cache.hh

📝 Walkthrough

Walkthrough

EvalCache and AttrCursor now store cached RootValue instances in Sync wrappers. Cache initialization, attribute evaluation, and value retrieval acquire locks and update the wrapped values through locked references.

Changes

Eval-cache synchronization

Layer / File(s) Summary
Synchronized root-value storage
src/libexpr/include/nix/expr/eval-cache.hh
EvalCache and AttrCursor store their cached root values as Sync<RootValue>.
Locked cache access and evaluation
src/libexpr/eval-cache.cc
Root-value initialization and attribute evaluation lock the cached slots while checking, computing, storing, and returning values.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

Suggested reviewers: cole-h

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: synchronizing AttrCursor::_value initialization to fix a data race.
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 docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch attr-cursor-value-lock

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

@github-actions

Copy link
Copy Markdown

@github-actions github-actions Bot temporarily deployed to pull request July 13, 2026 13:58 Inactive
@edolstra edolstra added this pull request to the merge queue Jul 13, 2026
Merged via the queue into main with commit 776fac6 Jul 13, 2026
33 checks passed
@edolstra edolstra deleted the attr-cursor-value-lock branch July 13, 2026 16:10
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