diff --git a/perl/lib/Nix/Store.xs b/perl/lib/Nix/Store.xs index 64899e341..094612897 100644 --- a/perl/lib/Nix/Store.xs +++ b/perl/lib/Nix/Store.xs @@ -34,11 +34,15 @@ static AsyncIoRoot & aio() static ref store() { - static std::optional> _store; + auto & aioRoot = aio(); + + // SAFETY: this store will be destructed after its associated aio + // root because the root is initialized first (due to call above) + static thread_local std::optional> _store; if (!_store) { try { initLibStore(); - _store = aio().blockOn(openStore()); + _store = aioRoot.blockOn(openStore()); } catch (Error & e) { croak("%s", e.what()); }