From 67769e6fc146d9b1b2db777c56e2bba9f3ddf698 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Tue, 15 Jul 2025 20:16:39 +0200 Subject: [PATCH] Make EvalState::callDepth thread-local This is needed to make it thread-safe. --- src/libexpr/eval.cc | 2 ++ src/libexpr/include/nix/expr/eval.hh | 20 +++++++++++--------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc index 47cc35daa..e93ca5593 100644 --- a/src/libexpr/eval.cc +++ b/src/libexpr/eval.cc @@ -1533,6 +1533,8 @@ void ExprLambda::eval(EvalState & state, Env & env, Value & v) v.mkLambda(&env, this); } +thread_local size_t EvalState::callDepth = 0; + void EvalState::callFunction(Value & fun, std::span args, Value & vRes, const PosIdx pos) { auto _level = addCallDepth(pos); diff --git a/src/libexpr/include/nix/expr/eval.hh b/src/libexpr/include/nix/expr/eval.hh index 27294d114..5d9847182 100644 --- a/src/libexpr/include/nix/expr/eval.hh +++ b/src/libexpr/include/nix/expr/eval.hh @@ -52,15 +52,15 @@ namespace eval_cache { * Increments a count on construction and decrements on destruction. */ class CallDepth { - size_t & count; + size_t & count; public: - CallDepth(size_t & count) : count(count) { - ++count; - } - ~CallDepth() { - --count; - } + CallDepth(size_t & count) : count(count) { + ++count; + } + ~CallDepth() { + --count; + } }; /** @@ -710,9 +710,11 @@ private: std::shared_ptr & staticEnv); /** - * Current Nix call stack depth, used with `max-call-depth` setting to throw stack overflow hopefully before we run out of system stack. + * Current Nix call stack depth, used with `max-call-depth` + * setting to throw stack overflow hopefully before we run out of + * system stack. */ - size_t callDepth = 0; + thread_local static size_t callDepth; public: