Skip to content

Commit

Permalink
remove 'using namespace nix;'
Browse files Browse the repository at this point in the history
  • Loading branch information
Mic92 committed Nov 10, 2024
1 parent f80f5ed commit 0bf233c
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 62 deletions.
2 changes: 0 additions & 2 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ Checks:
- concurrency-*
- google-*
- -google-readability-todo
# TODO
- -google-build-using-namespace

# don't find them too problematic
- -readability-identifier-length
Expand Down
131 changes: 71 additions & 60 deletions src/nix-eval-jobs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <nix/flake/flake.hh>
#include <nix/signals.hh>
#include <nix/signals-impl.hh>
#include <nix/fmt.hh>
#include <condition_variable>
#include <array>
#include <filesystem>
Expand All @@ -55,28 +56,27 @@
#include "buffered-io.hh"
#include "worker.hh"

using namespace nix;

namespace {
MyArgs myArgs; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
}

using Processor = std::function<void(ref<EvalState> state, Bindings &autoArgs,
const Channel &channel, MyArgs &args)>;
using Processor =
std::function<void(nix::ref<nix::EvalState> state, nix::Bindings &autoArgs,
const Channel &channel, MyArgs &args)>;

/* Auto-cleanup of fork's process and fds. */
struct Proc {
AutoCloseFD to, from;
Pid pid;
nix::AutoCloseFD to, from;
nix::Pid pid;

Proc(const Proc &) = delete;
Proc(Proc &&) = delete;
auto operator=(const Proc &) -> Proc & = delete;
auto operator=(Proc &&) -> Proc & = delete;

explicit Proc(const Processor &proc) {
Pipe toPipe;
Pipe fromPipe;
nix::Pipe toPipe;
nix::Pipe fromPipe;
toPipe.create();
fromPipe.create();

Expand All @@ -85,28 +85,32 @@ struct Proc {

auto p = startProcess(
[&,
to{std::make_shared<AutoCloseFD>(std::move(fromPipe.writeSide))},
from{
std::make_shared<AutoCloseFD>(std::move(toPipe.readSide))}]() {
debug("created worker process %d", getpid());
to{std::make_shared<nix::AutoCloseFD>(
std::move(fromPipe.writeSide))},
from{std::make_shared<nix::AutoCloseFD>(
std::move(toPipe.readSide))}]() {
nix::logger->log(
nix::lvlDebug,
nix::fmt("created worker process %d", getpid()));
try {
auto evalStore = myArgs.evalStoreUrl
? openStore(*myArgs.evalStoreUrl)
: openStore();
auto state = std::make_shared<EvalState>(
myArgs.lookupPath, evalStore, fetchSettings,
evalSettings);
Bindings &autoArgs = *myArgs.getAutoArgs(*state);
? nix::openStore(*myArgs.evalStoreUrl)
: nix::openStore();
auto state = std::make_shared<nix::EvalState>(
myArgs.lookupPath, evalStore, nix::fetchSettings,
nix::evalSettings);
nix::Bindings &autoArgs = *myArgs.getAutoArgs(*state);
const Channel channel{
.from = from,
.to = to,
};
proc(ref<EvalState>(state), autoArgs, channel, myArgs);
} catch (Error &e) {
proc(nix::ref<nix::EvalState>(state), autoArgs, channel,
myArgs);
} catch (nix::Error &e) {
nlohmann::json err;
const auto &msg = e.msg();
err["error"] = nix::filterANSIEscapes(msg, true);
printError(msg);
nix::logger->log(nix::lvlError, msg);
if (tryWriteLine(to->get(), err.dump()) < 0) {
return; // main process died
};
Expand All @@ -117,7 +121,7 @@ struct Proc {
}
}
},
ProcessOptions{.allowVfork = false});
nix::ProcessOptions{.allowVfork = false});

pid = p;
}
Expand Down Expand Up @@ -147,27 +151,27 @@ struct Thread {

int s = pthread_attr_init(&attr);
if (s != 0) {
throw SysError(s, "calling pthread_attr_init");
throw nix::SysError(s, "calling pthread_attr_init");
}
s = pthread_attr_setstacksize(&attr,
static_cast<size_t>(64) * 1024 * 1024);
if (s != 0) {
throw SysError(s, "calling pthread_attr_setstacksize");
throw nix::SysError(s, "calling pthread_attr_setstacksize");
}
s = pthread_create(&thread, &attr, Thread::init, func.release());
if (s != 0) {
throw SysError(s, "calling pthread_launch");
throw nix::SysError(s, "calling pthread_launch");
}
s = pthread_attr_destroy(&attr);
if (s != 0) {
throw SysError(s, "calling pthread_attr_destroy");
throw nix::SysError(s, "calling pthread_attr_destroy");
}
}

void join() const {
const int s = pthread_join(thread, nullptr);
if (s != 0) {
throw SysError(s, "calling pthread_join");
throw nix::SysError(s, "calling pthread_join");
}
}

Expand All @@ -182,7 +186,8 @@ struct Thread {
};

struct State {
std::set<nlohmann::json> todo = nlohmann::json::array({nlohmann::json::array()});
std::set<nlohmann::json> todo =
nlohmann::json::array({nlohmann::json::array()});
std::set<nlohmann::json> active;
std::exception_ptr exc;
};
Expand All @@ -196,32 +201,35 @@ void handleBrokenWorkerPipe(Proc &proc, std::string_view msg) {
const int rc = waitpid(pid, &status, WNOHANG);
if (rc == 0) {
kill(pid, SIGKILL);
throw Error("BUG: while %s, worker pipe got closed but evaluation "
"worker still running?",
msg);
throw nix::Error(
"BUG: while %s, worker pipe got closed but evaluation "
"worker still running?",
msg);
}

if (rc == -1) {
kill(pid, SIGKILL);
std::array<char, 1024> buf = {};
char* errMsg = strerror_r(errno, buf.data(), sizeof(buf));
throw Error("BUG: while %s, waitpid for evaluation worker failed: %s", msg, errMsg);
char *errMsg = strerror_r(errno, buf.data(), sizeof(buf));
throw nix::Error(
"BUG: while %s, waitpid for evaluation worker failed: %s", msg,
errMsg);
}
if (WIFEXITED(status)) {
if (WEXITSTATUS(status) == 1) {
throw Error(
throw nix::Error(
"while %s, evaluation worker exited with exit code 1, "
"(possible infinite recursion)",
msg);
}
throw Error("while %s, evaluation worker exited with %d", msg,
WEXITSTATUS(status));
throw nix::Error("while %s, evaluation worker exited with %d", msg,
WEXITSTATUS(status));
}

if (WIFSIGNALED(status)) {
switch (WTERMSIG(status)) {
case SIGKILL:
throw Error(
throw nix::Error(
"while %s, evaluation worker got killed by SIGKILL, "
"maybe "
"memory limit reached?",
Expand All @@ -235,16 +243,16 @@ void handleBrokenWorkerPipe(Proc &proc, std::string_view msg) {
break;
#else
case SIGSEGV:
throw Error(
throw nix::Error(
"while %s, evaluation worker got killed by SIGSEGV, "
"(possible infinite recursion)",
msg);
#endif
default:
throw Error("while %s, evaluation worker got killed by "
"signal %d (%s)",
msg, WTERMSIG(status),
sigdescr_np(WTERMSIG(status)));
throw nix::Error("while %s, evaluation worker got killed by "
"signal %d (%s)",
msg, WTERMSIG(status),
sigdescr_np(WTERMSIG(status)));
}
} // else ignore WIFSTOPPED and WIFCONTINUED
}
Expand All @@ -261,7 +269,7 @@ auto joinAttrPath(nlohmann::json &attrPath) -> std::string {
return joined;
}

void collector(Sync<State> &state_, std::condition_variable &wakeup) {
void collector(nix::Sync<State> &state_, std::condition_variable &wakeup) {
try {
std::optional<std::unique_ptr<Proc>> proc_;
std::optional<std::unique_ptr<LineReader>> fromReader_;
Expand All @@ -288,9 +296,10 @@ void collector(Sync<State> &state_, std::condition_variable &wakeup) {
} else if (s != "next") {
try {
auto json = nlohmann::json::parse(s);
throw Error("worker error: %s", std::string(json["error"]));
throw nix::Error("worker error: %s",
std::string(json["error"]));
} catch (const nlohmann::json::exception &e) {
throw Error(
throw nix::Error(
"Received invalid JSON from worker: %s\n json: '%s'",
e.what(), s);
}
Expand All @@ -300,7 +309,7 @@ void collector(Sync<State> &state_, std::condition_variable &wakeup) {
nlohmann::json attrPath;

while (true) {
checkInterrupt();
nix::checkInterrupt();
auto state(state_.lock());
if ((state->todo.empty() && state->active.empty()) ||
state->exc) {
Expand Down Expand Up @@ -335,7 +344,7 @@ void collector(Sync<State> &state_, std::condition_variable &wakeup) {
try {
response = nlohmann::json::parse(respString);
} catch (const nlohmann::json::exception &e) {
throw Error(
throw nix::Error(
"Received invalid JSON from worker: %s\n json: '%s'",
e.what(), respString);
}
Expand All @@ -344,7 +353,8 @@ void collector(Sync<State> &state_, std::condition_variable &wakeup) {
std::vector<nlohmann::json> newAttrs;
if (response.find("attrs") != response.end()) {
for (auto &i : response["attrs"]) {
nlohmann::json newAttr = nlohmann::json(response["attrPath"]);
nlohmann::json newAttr =
nlohmann::json(response["attrPath"]);
newAttr.emplace_back(i);
newAttrs.push_back(newAttr);
}
Expand Down Expand Up @@ -397,44 +407,45 @@ auto main(int argc, char **argv) -> int {

auto args = std::span(argv, argc);

return handleExceptions(args[0], [&]() {
initNix();
initGC();
flake::initLib(flakeSettings);
return nix::handleExceptions(args[0], [&]() {
nix::initNix();
nix::initGC();
nix::flake::initLib(nix::flakeSettings);

myArgs.parseArgs(argv, argc);

/* FIXME: The build hook in conjunction with import-from-derivation is
* causing "unexpected EOF" during eval */
settings.builders = "";
nix::settings.builders = "";

/* Prevent access to paths outside of the Nix search path and
to the environment. */
evalSettings.restrictEval = false;
nix::evalSettings.restrictEval = false;

/* When building a flake, use pure evaluation (no access to
'getEnv', 'currentSystem' etc. */
if (myArgs.impure) {
evalSettings.pureEval = false;
nix::evalSettings.pureEval = false;
} else if (myArgs.flake) {
evalSettings.pureEval = true;
nix::evalSettings.pureEval = true;
}

if (myArgs.releaseExpr.empty()) {
throw UsageError("no expression specified");
throw nix::UsageError("no expression specified");
}

if (myArgs.gcRootsDir.empty()) {
printMsg(lvlError, "warning: `--gc-roots-dir' not specified");
nix::logger->log(nix::lvlError,
"warning: `--gc-roots-dir' not specified");
} else {
myArgs.gcRootsDir = std::filesystem::absolute(myArgs.gcRootsDir);
}

if (myArgs.showTrace) {
loggerSettings.showTrace.assign(true);
nix::loggerSettings.showTrace.assign(true);
}

Sync<State> state_;
nix::Sync<State> state_;

/* Start a collector thread per worker process. */
std::vector<Thread> threads;
Expand Down

0 comments on commit 0bf233c

Please sign in to comment.