Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/libstore/binary-cache-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -418,10 +418,20 @@ void BinaryCacheStore::narFromPath(const StorePath & storePath, Sink & sink)
{
auto info = queryPathInfo(storePath).cast<const NarInfo>();

LengthSink narSize;
TeeSink tee{sink, narSize};
uint64_t narSize = 0;

auto decompressor = makeDecompressionSink(info->compression, tee);
LambdaSink uncompressedSink{
[&](std::string_view data) {
narSize += data.size();
sink(data);
},
[&]() {
stats.narRead++;
// stats.narReadCompressedBytes += nar->size(); // FIXME
stats.narReadBytes += narSize;
}};

auto decompressor = makeDecompressionSink(info->compression, uncompressedSink);

try {
getFile(info->url, *decompressor);
Expand All @@ -431,9 +441,7 @@ void BinaryCacheStore::narFromPath(const StorePath & storePath, Sink & sink)

decompressor->finish();

stats.narRead++;
// stats.narReadCompressedBytes += nar->size(); // FIXME
stats.narReadBytes += narSize.length;
// Note: don't do anything here because it's never reached if we're called as a coroutine.
Copy link
Contributor

@xokdvium xokdvium Nov 25, 2025

Choose a reason for hiding this comment

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

But if this code doesn't run then how is the destructor (for LambdaSink) being run? Would be nice to add a unit test for the issue so it's more clear what's being fixed

Copy link
Member Author

Choose a reason for hiding this comment

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

Boost unwinds the coroutine stack by throwing an exception of type boost::context::detail::forced_unwind. So destructors of stack variables do get executed.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks!

}

void BinaryCacheStore::queryPathInfoUncached(
Expand Down
19 changes: 14 additions & 5 deletions src/libutil/include/nix/util/serialise.hh
Original file line number Diff line number Diff line change
Expand Up @@ -447,18 +447,27 @@ struct LengthSource : Source
*/
struct LambdaSink : Sink
{
typedef std::function<void(std::string_view data)> lambda_t;
typedef std::function<void(std::string_view data)> data_t;
typedef std::function<void()> cleanup_t;

lambda_t lambda;
data_t dataFun;
cleanup_t cleanupFun;

LambdaSink(const lambda_t & lambda)
: lambda(lambda)
LambdaSink(
const data_t & dataFun, const cleanup_t & cleanupFun = []() {})
: dataFun(dataFun)
, cleanupFun(cleanupFun)
{
}

~LambdaSink()
{
cleanupFun();
}

void operator()(std::string_view data) override
{
lambda(data);
dataFun(data);
}
};

Expand Down
Loading