Skip to content

builtins.toFile leads to crashing nix repl #16174

Closed

Description

@3timeslazy

Describe the bug

Certain chain of builtins.toFile calls lead to the repl crash

Steps To Reproduce

I managed to reproduce the issue consistently with these three repl commands. Inside the repl, do:

  1. builtins.toFile "name" "${./not-found.txt}". It should result in path '/home/threetimeslazy/.../not-found.txt' does not exist
  2. builtins.toFile "hi" "hi" or any successful call to the builtin. It usually returns end of string reached.
  3. Run the command from step 2. again. The repl should crash at this point

Here's a full output sample of running these steps:

nix-repl> builtins.toFile "name" "${./not-found.txt}"
error:
       … while calling the 'toFile' builtin
         at «string»:1:1:
            1| builtins.toFile "name" "${./not-found.txt}"
             | ^

       … while evaluating the second argument passed to builtins.toFile

       error: path '/home/threetimeslazy/not-found.txt' does not exist

nix-repl> builtins.toFile "hi" "hi"
error:
       … while calling the 'toFile' builtin
         at «string»:1:1:
            1| builtins.toFile "hi" "hi"
             | ^

       error: end of string reached

nix-repl> builtins.toFile "hi" "hi"


terminating due to unexpected unrecoverable internal error: Assertion 'block' failed in std::__exception_ptr::exception_ptr nix::WorkerProto::BasicClientConnection::processStderrReturn(nix::Sink*, nix::Source*, bool, bool) at ../worker-protocol-connection.cc:101
Nix crashed. This is a bug. Please report this at https://github.com/NixOS/nix/issues with the following information included:

std::terminate() called without exception
Stack trace:
 0# 0x00000000001124AE in nix
 1# 0x00000000000C539A in /nix/store/hngmi01i8wgi25a0byrxcn4ysz5j79mw-gcc-15.2.0-lib/lib/libstdc++.so.6
 2# std::unexpected() in /nix/store/hngmi01i8wgi25a0byrxcn4ysz5j79mw-gcc-15.2.0-lib/lib/libstdc++.so.6
 3# 0x00000000000E92EF in /nix/store/l5p52saf0rh1fspzwfx32813pb1ap597-nix-util-2.34.7/lib/libnixutil.so.2.34.7
 4# __wrap___assert_fail in /nix/store/0hin4abpq9yk7zrl1x0v0yaw1x8yjpy6-nix-store-2.34.7/lib/libnixstore.so.2.34.7
 5# 0x000000000012DFCF in /nix/store/0hin4abpq9yk7zrl1x0v0yaw1x8yjpy6-nix-store-2.34.7/lib/libnixstore.so.2.34.7
 6# 0x0000000000378266 in /nix/store/0hin4abpq9yk7zrl1x0v0yaw1x8yjpy6-nix-store-2.34.7/lib/libnixstore.so.2.34.7
 7# 0x0000000000318787 in /nix/store/0hin4abpq9yk7zrl1x0v0yaw1x8yjpy6-nix-store-2.34.7/lib/libnixstore.so.2.34.7
 8# nix::RemoteStore::ConnectionHandle::withFramedSink(nix::fun<void (nix::Sink&)>) in /nix/store/0hin4abpq9yk7zrl1x0v0yaw1x8yjpy6-nix-store-2.34.7/lib/libnixstore.so.2.34.7
 9# nix::RemoteStore::addCAToStore(nix::Source&, std::basic_string_view<char, std::char_traits<char> >, nix::ContentAddressMethod, nix::HashAlgorithm, std::set<nix::StorePath, std::less<nix::StorePath>, std::allocator<nix::StorePath> > const&, nix::RepairFlag) in /nix/store/0hin4abpq9yk7zrl1x0v0yaw1x8yjpy6-nix-store-2.34.7/lib/libnixstore.so.2.34.7
10# nix::RemoteStore::addToStoreFromDump(nix::Source&, std::basic_string_view<char, std::char_traits<char> >, nix::FileSerialisationMethod, nix::ContentAddressMethod, nix::HashAlgorithm, std::set<nix::StorePath, std::less<nix::StorePath>, std::allocator<nix::StorePath> > const&, nix::RepairFlag) in /nix/store/0hin4abpq9yk7zrl1x0v0yaw1x8yjpy6-nix-store-2.34.7/lib/libnixstore.so.2.34.7
11# 0x0000000000137F4B in /nix/store/prs3vvry5dw9gqy7frwg1ny119swakjs-nix-expr-2.34.7/lib/libnixexpr.so.2.34.7
12# nix::EvalState::callFunction(nix::Value&, std::span<nix::Value*, 18446744073709551615ul>, nix::Value&, nix::PosIdx) in /nix/store/prs3vvry5dw9gqy7frwg1ny119swakjs-nix-expr-2.34.7/lib/libnixexpr.so.2.34.7
13# nix::ExprCall::eval(nix::EvalState&, nix::Env&, nix::Value&) in /nix/store/prs3vvry5dw9gqy7frwg1ny119swakjs-nix-expr-2.34.7/lib/libnixexpr.so.2.34.7
14# nix::NixRepl::evalString(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, nix::Value&) in /nix/store/z8sp8bniwl5mxqnqprrjzsvhyhx94waz-nix-cmd-2.34.7/lib/libnixcmd.so.2.34.7
15# nix::NixRepl::processLine(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) in /nix/store/z8sp8bniwl5mxqnqprrjzsvhyhx94waz-nix-cmd-2.34.7/lib/libnixcmd.so.2.34.7
16# nix::NixRepl::mainLoop() in /nix/store/z8sp8bniwl5mxqnqprrjzsvhyhx94waz-nix-cmd-2.34.7/lib/libnixcmd.so.2.34.7
17# 0x00000000001D61B5 in nix
18# nix::RawInstallablesCommand::run(nix::ref<nix::Store>) in /nix/store/z8sp8bniwl5mxqnqprrjzsvhyhx94waz-nix-cmd-2.34.7/lib/libnixcmd.so.2.34.7
19# nix::StoreCommand::run(nix::ref<nix::StoreConfig>) in /nix/store/z8sp8bniwl5mxqnqprrjzsvhyhx94waz-nix-cmd-2.34.7/lib/libnixcmd.so.2.34.7
20# virtual thunk to nix::StoreConfigCommand::run() in /nix/store/z8sp8bniwl5mxqnqprrjzsvhyhx94waz-nix-cmd-2.34.7/lib/libnixcmd.so.2.34.7
21# 0x000000000019FADB in nix
22# nix::handleExceptions(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, nix::fun<void ()>) in /nix/store/l5p52saf0rh1fspzwfx32813pb1ap597-nix-util-2.34.7/lib/libnixutil.so.2.34.7
23# 0x00000000000E1A0B in nix
24# 0x000000000002B285 in /nix/store/8kvxvr3pmsypxiypq4g8zy13glnfr7nx-glibc-2.42-67/lib/libc.so.6
25# __libc_start_main in /nix/store/8kvxvr3pmsypxiypq4g8zy13glnfr7nx-glibc-2.42-67/lib/libc.so.6
26# 0x00000000000E6675 in nix

zsh: abort (core dumped)  nix repl

Expected behavior

I'd expect the first call to fail, but subsequent calls to return a valid value instead. In that case, i'd be

nix-repl> builtins.toFile "hi" "hi"
"/nix/store/fd6846zv66nhsbwjaijjqbykvk60smps-hi"

Metadata

nix-env (Nix) 2.34.7

Additional context

Checklist


Add 👍 to issues you find important.

Activity

  1. xokdvium commented on Jul 20, 2026

    @xokdvium
    Contributor

    Oh huh, this is quite a convoluted crash. This is quite certainly (at least me looking in the debugger and staring at the code) due to our usage of stackful coroutines and some weirdness around marking connections bad? This doesn't really affect just toFile (or has anything to do with it actually). The initial error when trying to add a path to the store via path interpolation in the "${./not-found.txt}" expression. Needs a bit more debugging though.

  2. xokdvium commented on Jul 20, 2026

    @xokdvium
    Contributor

    Generally, boost's forced_unwind should lead us to properly unwind the stack and mark a connection as bad, but that doesn't seem to be working hmmmm.

  3. added
    protocol
    on Jul 20, 2026
  4. xokdvium commented on Jul 21, 2026

    @xokdvium
    Contributor

    Indeed this is a boost "nuance" (hopefully upstream treats it as a bug). Applying this patch to the nix tree (along with a patch to boost.context) fixes the issue:

    diff --git a/packaging/dependencies.nix b/packaging/dependencies.nix
    index db7948566f..c82f31f823 100644
    --- a/packaging/dependencies.nix
    +++ b/packaging/dependencies.nix
    @@ -126,6 +126,9 @@ scope: {
             "--with-iostreams"
             "--with-url"
           ];
    +      patches = [
    +        ./patches/boost-context-uncaught-exceptions.patch
    +      ];
           enableIcu = false;
         }).overrideAttrs
           (old: {
    diff --git a/packaging/patches/boost-context-uncaught-exceptions.patch b/packaging/patches/boost-context-uncaught-exceptions.patch
    new file mode 100644
    index 0000000000..0869c0ec60
    --- /dev/null
    +++ b/packaging/patches/boost-context-uncaught-exceptions.patch
    @@ -0,0 +1,76 @@
    +diff --git a/include/boost/context/fiber_fcontext.hpp b/include/boost/context/fiber_fcontext.hpp
    +index 543ba6c..38476c9 100644
    +--- a/boost/context/fiber_fcontext.hpp
    ++++ b/boost/context/fiber_fcontext.hpp
    +@@ -70,7 +70,9 @@ namespace context {
    + namespace detail {
    + 
    + // manage_exception_state is a dummy struct unless we have specific support
    +-struct manage_exception_state {};
    ++struct manage_exception_state {
    ++    void from_forced_unwind() noexcept {}
    ++};
    + 
    + } // namespace detail
    + } // namespace context
    +@@ -90,6 +92,11 @@ public:
    +     manage_exception_state() {
    +         exception_state_ = *__cxa_get_globals();
    +     }
    ++    // Hack to account for the forced_unwind exception thrown in fiber_unwind
    ++    // that's run ontop before the destructor.
    ++    void from_forced_unwind() noexcept {
    ++        exception_state_.uncaughtExceptions += 1;
    ++    }
    +     ~manage_exception_state() {
    +         *__cxa_get_globals() = exception_state_;
    +     }
    +@@ -376,13 +383,18 @@ public:
    +         BOOST_ASSERT( nullptr != fctx_);
    +         detail::manage_exception_state exstate;
    +         boost::ignore_unused(exstate);
    +-        return { detail::jump_fcontext(
    ++        try {
    ++            return { detail::jump_fcontext(
    + #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
    +-                    detail::exchange( fctx_, nullptr),
    ++                        detail::exchange( fctx_, nullptr),
    + #else
    +-                    std::exchange( fctx_, nullptr),
    ++                        std::exchange( fctx_, nullptr),
    + #endif
    +-                    nullptr).fctx };
    ++                        nullptr).fctx };
    ++        } catch ( detail::forced_unwind const& ) {
    ++            exstate.from_forced_unwind();
    ++            throw;
    ++        }
    +     }
    + 
    +     template< typename Fn >
    +@@ -391,14 +403,19 @@ public:
    +         detail::manage_exception_state exstate;
    +         boost::ignore_unused(exstate);
    +         auto p = std::forward< Fn >( fn);
    +-        return { detail::ontop_fcontext(
    ++        try {
    ++            return { detail::ontop_fcontext(
    + #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
    +-                    detail::exchange( fctx_, nullptr),
    ++                        detail::exchange( fctx_, nullptr),
    + #else
    +-                    std::exchange( fctx_, nullptr),
    ++                        std::exchange( fctx_, nullptr),
    + #endif
    +-                    & p,
    +-                    detail::fiber_ontop< fiber, decltype(p) >).fctx };
    ++                        & p,
    ++                        detail::fiber_ontop< fiber, decltype(p) >).fctx };
    ++        } catch ( detail::forced_unwind const& ) {
    ++            exstate.from_forced_unwind();
    ++            throw;
    ++        }
    +     }
    + 
    +     explicit operator bool() const noexcept {
    +
  5. xokdvium commented on Jul 21, 2026

    @xokdvium
    Contributor

    I have submitted boostorg/context#337, let's see how that pans out. Though we'd probably still need some workaround even if that was merged. Probably depending on the review on the patch it'd be clearer what the best shape of the workaround would be.

  6. xokdvium commented on Jul 22, 2026

    @xokdvium
    Contributor

    Ok we seem to have agreed to go with applying my patch to our dependencies. If this goes well on master for a bit we'll also backport to our packaging and also apply it in nixpkgs to the nix's boost dependency.

  7. xokdvium commented on Jul 27, 2026

    @xokdvium
    Contributor

    NixOS/nixpkgs#546405 is now up for boost.context patches backport in nixpkgs. That should address the underlying issue here.

  8. xokdvium commented on Aug 13, 2026

    @xokdvium
    Contributor

    This is now fixed in nixpkgs and in our binary builds in 2.35.2. Boost patches are in unstable and 26.05 staging now.

  9. 3timeslazy commented on Aug 16, 2026

    @3timeslazy
    Author

    Thank you for detailed overview! I checked again from the main branch, the issue is gone

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugprotocol

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions