Skip to content

nar-accessor: Fix thread safety of seekableGetNarBytes, use Sink, reduce memory usage of nix store cat#15013

Merged
Ericson2314 merged 4 commits intomasterfrom
seekable-nar-bytes-thread-safe-pread
Jan 19, 2026
Merged

nar-accessor: Fix thread safety of seekableGetNarBytes, use Sink, reduce memory usage of nix store cat#15013
Ericson2314 merged 4 commits intomasterfrom
seekable-nar-bytes-thread-safe-pread

Conversation

@xokdvium
Copy link
Contributor

Motivation

Instead of mutating the file pointer we can instead safely do
preads. That makes the local-nar-info cache once again thread safe
without the overhead of reopening the file that we used to have prior
to b9b6def which broke the thread safety
by persisting the file descriptor.

The second and third commit reduces the memory usage of the source accessor with a local-nar-cache,
which pairs nicely with the recent refactor to support substituting from it.

Context


Add 👍 to pull requests you find important.

The Nix maintainer team uses a GitHub project board to schedule and track reviews.

@xokdvium xokdvium requested a review from edolstra as a code owner January 17, 2026 23:57
@github-actions github-actions bot added the new-cli Relating to the "nix" command label Jan 17, 2026
@xokdvium xokdvium force-pushed the seekable-nar-bytes-thread-safe-pread branch 2 times, most recently from a4992fe to f9213a4 Compare January 18, 2026 00:34
while (left) {
checkInterrupt();
auto limit = std::min<decltype(buf)::size_type>(left, buf.size());
#ifdef _WIN32
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that this code is a bit more complex, can a pread/ReadFile wrapper can factored out into file-descriptor.hh? I think we already have the Windows/Unix implementation files for that too.

Copy link
Member

@Ericson2314 Ericson2314 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise looks good!

Copy link
Member

@Ericson2314 Ericson2314 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to merge once factor out is done

Instead of mutating the file pointer we can instead safely do
preads. That makes the local-nar-info cache once again thread safe
without the overhead of reopening the file that we used to have prior
to b9b6def which broke the thread safety
by persisting the file descriptor.
This reduces the memory overhead of nix store cat down to constant memory
with a local-nar-cache.
@xokdvium xokdvium force-pushed the seekable-nar-bytes-thread-safe-pread branch from f9213a4 to 253aec8 Compare January 18, 2026 21:55
@github-actions github-actions bot added store Issues and pull requests concerning the Nix store fetching Networking with the outside (non-Nix) world, input locking labels Jan 18, 2026
@xokdvium
Copy link
Contributor Author

Fixed, also did some more optimizations and cleanups. Turns out we were unnecessarily buffering files in memory in the evaluator.

@xokdvium xokdvium force-pushed the seekable-nar-bytes-thread-safe-pread branch from 253aec8 to 3a00ce2 Compare January 18, 2026 22:10
This factors out the helper function from seekableGetNarBytes into copyFdRange
and adds some more sanity checks for offset/length truncation/wrapping at that
API boundary where we work with NAR-style offsets and convert to native off_t.
…non-virtual

This makes all addToStore operations that use these source accessors
constant memory regardless of file sizes. Also make the other overload
altogether and relegate it to the base class as a non-virtual method to
avoid such mistakes.
@xokdvium xokdvium force-pushed the seekable-nar-bytes-thread-safe-pread branch from 3a00ce2 to 4db68c2 Compare January 18, 2026 22:15
@Ericson2314 Ericson2314 added this pull request to the merge queue Jan 18, 2026
Merged via the queue into master with commit 63344d3 Jan 19, 2026
18 checks passed
@Ericson2314 Ericson2314 deleted the seekable-nar-bytes-thread-safe-pread branch January 19, 2026 00:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fetching Networking with the outside (non-Nix) world, input locking new-cli Relating to the "nix" command store Issues and pull requests concerning the Nix store

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants