Repository navigation
Fix race condition in Value::isTrivial() - #192
Conversation
WalkthroughRefactors triviality detection from Value to a Changes
Sequence Diagram(s)sequenceDiagram
autonumber
actor Caller
participant Flake as Flake logic
participant V as ValueStorage
participant Eval as Eval internals
Caller->>Flake: forceTrivialValue(value)
Flake->>V: isTrivial()
Note over V,Eval: atomic load p0 (acquire), read p1, derive PrimaryDiscriminator
alt Thunk/Pending/Awaited
alt Application tagged
V-->>Flake: false
else Not application
V->>Eval: untagPointer(p1) → Expr*
Eval-->>V: check Expr: Attrs(empty dyn)/Lambda/List?
V-->>Flake: true/false
end
else Non-thunk-like
V-->>Flake: true
end
alt isTrivial == true
Flake->>Eval: force(value)
Eval-->>Flake: forced result
Flake-->>Caller: return
else
Flake-->>Caller: no-op
end
Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 💡 Knowledge Base configuration:
You can enable these sources in your CodeRabbit configuration. 📒 Files selected for processing (3)
🚧 Files skipped from review as they are similar to previous changes (3)
⏰ 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). (1)
✨ Finishing Touches
🧪 Generate unit tests
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
src/libexpr/include/nix/expr/value.hh (1)
780-789: API surface note: isTrivial() is only declared on the bit-packed specialization.On 32-bit or non-bitpacked builds,
ValueStorage<ptrSize, void>does not declareisTrivial(), which will break call sites likevalue.isTrivial(). If 32-bit is unsupported, that’s fine; otherwise, add a conservative fallback on the genericValueStorage(e.g., returnisFinished(getInternalType())).Apply this minimal fallback in the non-bitpacked variant:
class ValueStorage : public detail::ValueBase { ... +public: + // Fallback for non-bitpacked builds. + bool isTrivial() const noexcept { return nix::isFinished(getInternalType()); } };Please confirm whether non-64-bit builds are still supported in this repo’s CI matrix.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (3)
src/libexpr/eval.cc(1 hunks)src/libexpr/include/nix/expr/value.hh(3 hunks)src/libflake/flake.cc(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (3)
src/libflake/flake.cc (2)
src/libexpr/eval.cc (6)
state(1143-1166)state(1143-1143)showType(107-137)showType(107-107)showType(139-161)showType(139-139)src/libexpr/include/nix/expr/value.hh (9)
state(159-160)state(172-172)state(177-184)state(802-802)state(811-811)state(1038-1038)pos(1253-1253)t(1060-1064)showType(1013-1013)
src/libexpr/eval.cc (1)
src/libexpr/include/nix/expr/value.hh (8)
p1(790-794)p0_(527-543)p0_(527-527)p0(797-800)pd(599-622)static_cast(517-520)untagPointer(512-515)untagPointer(512-512)
src/libexpr/include/nix/expr/value.hh (1)
src/libexpr/eval.cc (2)
isTrivial(182-200)isTrivial(182-182)
⏰ 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 (4)
src/libflake/flake.cc (2)
31-33: Good: force trivial values unconditionally (thread-safe and simpler).Using
value.isTrivial()alone is correct with the new storage-aware implementation; forcing finished values is a no-op and avoids the prior racy thunk check.
38-41: Minor clarity/perf win in expectType.Caching
value.type()and usingshowType(t)in the message is cleaner and avoids double computation.src/libexpr/include/nix/expr/value.hh (2)
22-24: Forward declarations look right.These match the uses in the new triviality check logic and avoid header include churn.
825-827: Explicit specialization declaration is appropriate.Matches the out-of-line definition in eval.cc and keeps ODR tidy.
| template<> | ||
| bool ValueStorage<sizeof(void *)>::isTrivial() const | ||
| { | ||
| return isFinished() | ||
| || (isa<tThunk>() | ||
| && ((dynamic_cast<ExprAttrs *>(thunk().expr) && ((ExprAttrs *) thunk().expr)->dynamicAttrs.empty()) | ||
| || dynamic_cast<ExprLambda *>(thunk().expr) || dynamic_cast<ExprList *>(thunk().expr))); | ||
| auto p1_ = p1; // must acquire before reading p0, since thunks can change | ||
| auto p0_ = p0.load(std::memory_order_acquire); | ||
|
|
||
| auto pd = static_cast<PrimaryDiscriminator>(p0_ & discriminatorMask); | ||
|
|
||
| if (pd == pdThunk || pd == pdPending || pd == pdAwaited) { | ||
| bool isApp = p1_ & discriminatorMask; | ||
| if (isApp) | ||
| return false; | ||
| auto expr = untagPointer<Expr *>(p1_); | ||
| return (dynamic_cast<ExprAttrs *>(expr) && ((ExprAttrs *) expr)->dynamicAttrs.empty()) | ||
| || dynamic_cast<ExprLambda *>(expr) || dynamic_cast<ExprList *>(expr); | ||
| } | ||
|
|
||
| else | ||
| return true; | ||
| } |
There was a problem hiding this comment.
Fix acquire/load ordering bug when reading p0/p1 (data race risk).
You read p1 before the acquire load of p0. To pair with finish()’s release store (which writes p1 then p0), readers must load p0 with acquire first, then read p1. Otherwise p1_ can be observed stale or mismatched to p0_.
Apply:
- auto p1_ = p1; // must acquire before reading p0, since thunks can change
- auto p0_ = p0.load(std::memory_order_acquire);
+ auto p0_ = p0.load(std::memory_order_acquire); // acquire before reading p1, since thunks can change
+ auto p1_ = p1;Optional readability nit: avoid the C-style cast when checking ExprAttrs:
- return (dynamic_cast<ExprAttrs *>(expr) && ((ExprAttrs *) expr)->dynamicAttrs.empty())
+ if (auto attrs = dynamic_cast<ExprAttrs *>(expr); attrs && attrs->dynamicAttrs.empty())
+ return true;
+ return dynamic_cast<ExprLambda *>(expr) || dynamic_cast<ExprList *>(expr);📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| template<> | |
| bool ValueStorage<sizeof(void *)>::isTrivial() const | |
| { | |
| return isFinished() | |
| || (isa<tThunk>() | |
| && ((dynamic_cast<ExprAttrs *>(thunk().expr) && ((ExprAttrs *) thunk().expr)->dynamicAttrs.empty()) | |
| || dynamic_cast<ExprLambda *>(thunk().expr) || dynamic_cast<ExprList *>(thunk().expr))); | |
| auto p1_ = p1; // must acquire before reading p0, since thunks can change | |
| auto p0_ = p0.load(std::memory_order_acquire); | |
| auto pd = static_cast<PrimaryDiscriminator>(p0_ & discriminatorMask); | |
| if (pd == pdThunk || pd == pdPending || pd == pdAwaited) { | |
| bool isApp = p1_ & discriminatorMask; | |
| if (isApp) | |
| return false; | |
| auto expr = untagPointer<Expr *>(p1_); | |
| return (dynamic_cast<ExprAttrs *>(expr) && ((ExprAttrs *) expr)->dynamicAttrs.empty()) | |
| || dynamic_cast<ExprLambda *>(expr) || dynamic_cast<ExprList *>(expr); | |
| } | |
| else | |
| return true; | |
| } | |
| template<> | |
| bool ValueStorage<sizeof(void *)>::isTrivial() const | |
| { | |
| auto p0_ = p0.load(std::memory_order_acquire); // acquire before reading p1, since thunks can change | |
| auto p1_ = p1; | |
| auto pd = static_cast<PrimaryDiscriminator>(p0_ & discriminatorMask); | |
| if (pd == pdThunk || pd == pdPending || pd == pdAwaited) { | |
| bool isApp = p1_ & discriminatorMask; | |
| if (isApp) | |
| return false; | |
| auto expr = untagPointer<Expr *>(p1_); | |
| if (auto attrs = dynamic_cast<ExprAttrs *>(expr); attrs && attrs->dynamicAttrs.empty()) | |
| return true; | |
| return dynamic_cast<ExprLambda *>(expr) || dynamic_cast<ExprList *>(expr); | |
| } else { | |
| return true; | |
| } | |
| } |
🤖 Prompt for AI Agents
In src/libexpr/eval.cc around lines 181 to 200, the reader currently reads p1
before performing the acquire load of p0 which can observe a stale/mismatched p1
vs p0; to fix, perform the acquire load of p0 first, compute the primary
discriminator from that p0_, then read p1 (non-atomic) into p1_ so the
load-order pairs with finish()’s release store (which writes p1 then p0); also
replace the C-style cast when checking ExprAttrs with a safer dynamic_cast usage
for clarity.
The use of thunk() is racy since the value might stop being a thunk while we're accessing it.
31f255e to
7ccfc62
Compare
Motivation
The use of
thunk()is racy since the value might stop being a thunk while we're accessing it.This is unlikely to happen in practice, but can occur with test cases like
where we're evaluating the same flake multiple times in parallel, leading to segfaults or errors like
Context
Summary by CodeRabbit
Improvements
Refactor
Chores