diff --git a/src/libutil/logging.cc b/src/libutil/logging.cc index c7b859bd5..de8df24b0 100644 --- a/src/libutil/logging.cc +++ b/src/libutil/logging.cc @@ -196,6 +196,7 @@ struct JSONLogger : Logger { struct State { + bool enabled = true; }; Sync _state; @@ -208,8 +209,18 @@ struct JSONLogger : Logger { /* Acquire a lock to prevent log messages from clobbering each other. */ - auto state(_state.lock()); - writeLine(fd, line); + try { + auto state(_state.lock()); + if (state->enabled) + writeLine(fd, line); + } catch (...) { + bool enabled = false; + std::swap(_state.lock()->enabled, enabled); + if (enabled) { + ignoreExceptionExceptInterrupt(); + logger->warn("disabling JSON logger due to write errors"); + } + } } void log(Verbosity lvl, std::string_view s) override diff --git a/src/nix/main.cc b/src/nix/main.cc index 68137a216..644c65cf0 100644 --- a/src/nix/main.cc +++ b/src/nix/main.cc @@ -502,7 +502,11 @@ void mainWrapped(int argc, char * * argv) } if (!extLoggerSettings.jsonLogPath.get().empty()) { - logger = makeTeeLogger({logger, makeJSONLogger(std::filesystem::path(extLoggerSettings.jsonLogPath.get()), false)}); + try { + logger = makeTeeLogger({logger, makeJSONLogger(std::filesystem::path(extLoggerSettings.jsonLogPath.get()), false)}); + } catch (...) { + ignoreExceptionExceptInterrupt(); + } } if (args.helpRequested) {