Skip to content

Fix logging segfault by leaking logger#15884

Merged
edolstra merged 1 commit into
NixOS:masterfrom
lisanna-dettwyler:logging-segfault-leak
May 22, 2026
Merged

Fix logging segfault by leaking logger#15884
edolstra merged 1 commit into
NixOS:masterfrom
lisanna-dettwyler:logging-segfault-leak

Conversation

@lisanna-dettwyler
Copy link
Copy Markdown
Contributor

@lisanna-dettwyler lisanna-dettwyler commented May 19, 2026

There is a race between activity and logger teardown, so we switch from a unique_ptr back to a raw pointer and leak the logger so it persists across all activity teardowns.

Motivation

The functional tests are currently randomly segfaulting when trying to use a logger reference in the activity destructor.

Context


Add 👍 to pull requests you find important.

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

@lisanna-dettwyler
Copy link
Copy Markdown
Contributor Author

I am exploring an alternative patch wherein I change all the logger references to shared_ptrs, so marking as draft for now.

@lisanna-dettwyler
Copy link
Copy Markdown
Contributor Author

lisanna-dettwyler commented May 20, 2026

I couldn't get the shared_ptr version to work without crashing because we can't release it on fork, so switching back to this version.

@lisanna-dettwyler lisanna-dettwyler marked this pull request as ready for review May 20, 2026 13:23
Comment thread src/libutil/include/nix/util/logging.hh Outdated
Comment on lines +267 to +271
std::unique_ptr<Logger>
makeTeeLogger(std::unique_ptr<Logger> mainLogger, std::vector<std::unique_ptr<Logger>> && extraLoggers);
Logger * makeTeeLogger(Logger * mainLogger, std::vector<Logger *> && extraLoggers);

std::unique_ptr<Logger> makeJSONLogger(Descriptor fd, bool includeNixPrefix = true);
Logger * makeJSONLogger(Descriptor fd, bool includeNixPrefix = true);

std::unique_ptr<Logger> makeJSONLogger(const std::filesystem::path & path, bool includeNixPrefix = true);
Logger * makeJSONLogger(const std::filesystem::path & path, bool includeNixPrefix = true);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This code doesn't need to change though, right?

The only things that needs to change is that the top-level logger is just a raw pointer that gets leaked?

All this plumbing/composition code can stay with unique_ptr, but it's the places where we assign to the global that change?

Is there something I'm missing? That seems like a much smaller change.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think there would be no point to having these be unique_ptrs because we'd have to call release() on their outputs anyways, kinda defeating the point of using a unique_ptr in the first place, right?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Well, for one that's a smaller diff and the it's more about how the logger object itself is global (but the classes themselves don't know about it, and don't have to be used as globals).

Maybe at some point we could improve upon this situation and then churn would hurt more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Latest commit is the most minimal change I can do ^-^

There is a race between activity and logger teardown, so we
switch from a unique_ptr to a raw pointer and leak the logger so it
persists across all activity teardowns.

Signed-off-by: Lisanna Dettwyler <lisanna.dettwyler@gmail.com>
@github-actions github-actions Bot added the new-cli Relating to the "nix" command label May 20, 2026
@edolstra edolstra added this pull request to the merge queue May 22, 2026
Merged via the queue into NixOS:master with commit 02e5998 May 22, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

new-cli Relating to the "nix" command

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants