2023-12-10 13:22:32 +00:00
|
|
|
|
2020-11-29 14:33:55 +00:00
|
|
|
#include <map>
|
|
|
|
#include <thread>
|
2023-12-10 13:22:32 +00:00
|
|
|
#include <condition_variable>
|
2020-11-29 14:33:55 +00:00
|
|
|
#include <nix/config.h>
|
2023-12-10 13:22:32 +00:00
|
|
|
#include <nix/eval-settings.hh>
|
2020-11-29 20:32:04 +00:00
|
|
|
#include <nix/common-eval-args.hh>
|
2023-12-10 13:22:32 +00:00
|
|
|
#include <nix/args/root.hh>
|
|
|
|
|
|
|
|
#include <nix/shared.hh>
|
|
|
|
#include <nix/sync.hh>
|
2023-12-06 08:14:07 +00:00
|
|
|
#include <nix/terminal.hh>
|
2023-12-10 13:22:32 +00:00
|
|
|
#include <nix/eval.hh>
|
|
|
|
#include <nix/local-fs-store.hh>
|
2023-03-09 21:18:00 +00:00
|
|
|
#include <nix/installable-flake.hh>
|
2023-12-10 13:22:32 +00:00
|
|
|
#include <nix/get-drvs.hh>
|
|
|
|
#include <nix/attr-path.hh>
|
2021-08-23 23:29:04 +00:00
|
|
|
#include <nix/value-to-json.hh>
|
2023-12-10 13:22:32 +00:00
|
|
|
#include <nix/signals.hh>
|
2020-11-29 14:33:55 +00:00
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <sys/resource.h>
|
|
|
|
|
2023-12-10 13:22:32 +00:00
|
|
|
#include "eval-args.hh"
|
2023-12-10 14:07:38 +00:00
|
|
|
#include "drv.hh"
|
2023-12-10 13:22:32 +00:00
|
|
|
|
2020-11-29 14:33:55 +00:00
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
|
|
|
|
using namespace nix;
|
2022-04-26 05:11:17 +00:00
|
|
|
using namespace nlohmann;
|
2020-11-29 14:33:55 +00:00
|
|
|
|
2022-04-21 17:02:05 +00:00
|
|
|
// Safe to ignore - the args will be static.
|
2022-04-25 15:53:17 +00:00
|
|
|
#ifdef __GNUC__
|
2022-04-21 17:02:05 +00:00
|
|
|
#pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
|
2022-04-25 15:53:17 +00:00
|
|
|
#elif __clang__
|
2022-04-21 17:02:05 +00:00
|
|
|
#pragma clang diagnostic ignored "-Wnon-virtual-dtor"
|
2022-04-25 15:53:17 +00:00
|
|
|
#endif
|
2020-11-29 14:33:55 +00:00
|
|
|
static MyArgs myArgs;
|
|
|
|
|
2022-04-26 09:15:56 +00:00
|
|
|
static Value *releaseExprTopLevelValue(EvalState &state, Bindings &autoArgs) {
|
2020-11-29 14:33:55 +00:00
|
|
|
Value vTop;
|
|
|
|
|
2022-08-17 06:38:37 +00:00
|
|
|
if (myArgs.fromArgs) {
|
2023-06-10 11:28:57 +00:00
|
|
|
Expr *e = state.parseExprFromString(
|
|
|
|
myArgs.releaseExpr, state.rootPath(CanonPath::fromCwd()));
|
2022-08-17 06:38:37 +00:00
|
|
|
state.eval(e, vTop);
|
|
|
|
} else {
|
|
|
|
state.evalFile(lookupFileArg(state, myArgs.releaseExpr), vTop);
|
|
|
|
}
|
2022-04-21 18:34:49 +00:00
|
|
|
|
|
|
|
auto vRoot = state.allocValue();
|
|
|
|
|
|
|
|
state.autoCallFunction(autoArgs, vTop, *vRoot);
|
2020-11-29 14:33:55 +00:00
|
|
|
|
2022-04-21 18:34:49 +00:00
|
|
|
return vRoot;
|
|
|
|
}
|
2020-11-29 14:33:55 +00:00
|
|
|
|
2022-04-13 22:41:51 +00:00
|
|
|
|
2022-04-26 05:11:17 +00:00
|
|
|
std::string attrPathJoin(json input) {
|
|
|
|
return std::accumulate(input.begin(), input.end(), std::string(),
|
|
|
|
[](std::string ss, std::string s) {
|
|
|
|
// Escape token if containing dots
|
|
|
|
if (s.find(".") != std::string::npos) {
|
|
|
|
s = "\"" + s + "\"";
|
|
|
|
}
|
|
|
|
return ss.empty() ? s : ss + "." + s;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-12-09 16:17:01 +00:00
|
|
|
[[nodiscard]] static int tryWriteLine(int fd, std::string s) {
|
|
|
|
s += "\n";
|
|
|
|
std::string_view sv{s};
|
|
|
|
while (!sv.empty()) {
|
|
|
|
checkInterrupt();
|
|
|
|
ssize_t res = write(fd, sv.data(), sv.size());
|
|
|
|
if (res == -1 && errno != EINTR) {
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
if (res > 0) {
|
|
|
|
sv.remove_prefix(res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-12-10 07:16:54 +00:00
|
|
|
class LineReader {
|
|
|
|
public:
|
|
|
|
LineReader(int fd) {
|
|
|
|
stream = fdopen(fd, "r");
|
|
|
|
if (!stream) {
|
|
|
|
throw Error("fdopen failed: %s", strerror(errno));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~LineReader() {
|
|
|
|
fclose(stream);
|
|
|
|
free(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
LineReader(LineReader &&other) {
|
|
|
|
stream = other.stream;
|
|
|
|
other.stream = nullptr;
|
|
|
|
buffer = other.buffer;
|
|
|
|
other.buffer = nullptr;
|
|
|
|
len = other.len;
|
|
|
|
other.len = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] std::string_view readLine() {
|
|
|
|
ssize_t read = getline(&buffer, &len, stream);
|
|
|
|
|
|
|
|
if (read == -1) {
|
|
|
|
return {}; // Return an empty string_view in case of error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove trailing newline
|
|
|
|
return std::string_view(buffer, read - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
FILE *stream = nullptr;
|
|
|
|
char *buffer = nullptr;
|
|
|
|
size_t len = 0;
|
|
|
|
};
|
|
|
|
|
2023-01-22 09:28:47 +00:00
|
|
|
static void worker(ref<EvalState> state, Bindings &autoArgs, AutoCloseFD &to,
|
2022-04-26 09:15:56 +00:00
|
|
|
AutoCloseFD &from) {
|
2022-11-04 15:10:54 +00:00
|
|
|
|
2023-07-14 04:34:20 +00:00
|
|
|
nix::Value *vRoot = [&]() {
|
|
|
|
if (myArgs.flake) {
|
|
|
|
auto [flakeRef, fragment, outputSpec] =
|
2023-07-14 06:50:35 +00:00
|
|
|
parseFlakeRefWithFragmentAndExtendedOutputsSpec(
|
|
|
|
myArgs.releaseExpr, absPath("."));
|
|
|
|
InstallableFlake flake{
|
|
|
|
{}, state, std::move(flakeRef), fragment, outputSpec,
|
|
|
|
{}, {}, myArgs.lockFlags};
|
2023-07-14 04:34:20 +00:00
|
|
|
|
|
|
|
return flake.toValue(*state).first;
|
|
|
|
} else {
|
|
|
|
return releaseExprTopLevelValue(*state, autoArgs);
|
|
|
|
}
|
|
|
|
}();
|
2020-11-29 14:33:55 +00:00
|
|
|
|
2023-12-10 07:16:54 +00:00
|
|
|
LineReader fromReader(from.release());
|
|
|
|
|
2020-11-29 14:33:55 +00:00
|
|
|
while (true) {
|
2022-04-21 04:01:02 +00:00
|
|
|
/* Wait for the collector to send us a job name. */
|
2023-12-09 16:17:01 +00:00
|
|
|
if (tryWriteLine(to.get(), "next") < 0) {
|
|
|
|
return; // main process died
|
|
|
|
}
|
2020-11-29 14:33:55 +00:00
|
|
|
|
2023-12-10 07:16:54 +00:00
|
|
|
auto s = fromReader.readLine();
|
2023-12-10 07:55:56 +00:00
|
|
|
if (s == "exit") {
|
2022-04-26 09:15:56 +00:00
|
|
|
break;
|
2023-12-10 07:55:56 +00:00
|
|
|
}
|
|
|
|
if (!hasPrefix(s, "do ")) {
|
|
|
|
fprintf(stderr, "worker error: received invalid command '%s'\n",
|
|
|
|
s.data());
|
2022-04-26 09:15:56 +00:00
|
|
|
abort();
|
2023-12-10 07:55:56 +00:00
|
|
|
}
|
2022-04-26 05:11:17 +00:00
|
|
|
auto path = json::parse(s.substr(3));
|
|
|
|
auto attrPathS = attrPathJoin(path);
|
2020-11-29 14:33:55 +00:00
|
|
|
|
2022-04-26 05:11:17 +00:00
|
|
|
debug("worker process %d at '%s'", getpid(), path);
|
2020-11-29 14:33:55 +00:00
|
|
|
|
2022-04-21 04:01:02 +00:00
|
|
|
/* Evaluate it and send info back to the collector. */
|
2022-04-26 09:15:56 +00:00
|
|
|
json reply = json{{"attr", attrPathS}, {"attrPath", path}};
|
2020-11-29 14:33:55 +00:00
|
|
|
try {
|
2022-04-26 09:15:56 +00:00
|
|
|
auto vTmp =
|
2023-01-22 09:28:47 +00:00
|
|
|
findAlongAttrPath(*state, attrPathS, autoArgs, *vRoot).first;
|
2020-11-29 14:33:55 +00:00
|
|
|
|
2023-01-22 09:28:47 +00:00
|
|
|
auto v = state->allocValue();
|
|
|
|
state->autoCallFunction(autoArgs, *vTmp, *v);
|
2020-11-29 14:33:55 +00:00
|
|
|
|
2022-09-09 11:26:15 +00:00
|
|
|
if (v->type() == nAttrs) {
|
2023-01-22 09:28:47 +00:00
|
|
|
if (auto drvInfo = getDerivation(*state, *v, false)) {
|
2023-12-10 14:07:38 +00:00
|
|
|
auto drv = Drv(attrPathS, *state, *drvInfo, myArgs);
|
2022-09-09 11:26:15 +00:00
|
|
|
reply.update(drv);
|
|
|
|
|
|
|
|
/* Register the derivation as a GC root. !!! This
|
|
|
|
registers roots for jobs that we may have already
|
|
|
|
done. */
|
|
|
|
if (myArgs.gcRootsDir != "") {
|
|
|
|
Path root = myArgs.gcRootsDir + "/" +
|
|
|
|
std::string(baseNameOf(drv.drvPath));
|
|
|
|
if (!pathExists(root)) {
|
|
|
|
auto localStore =
|
2023-01-22 09:28:47 +00:00
|
|
|
state->store
|
2022-09-09 11:26:15 +00:00
|
|
|
.dynamic_pointer_cast<LocalFSStore>();
|
|
|
|
auto storePath =
|
|
|
|
localStore->parseStorePath(drv.drvPath);
|
|
|
|
localStore->addPermRoot(storePath, root);
|
|
|
|
}
|
2022-04-21 18:22:29 +00:00
|
|
|
}
|
2022-09-09 11:26:15 +00:00
|
|
|
} else {
|
|
|
|
auto attrs = nlohmann::json::array();
|
|
|
|
bool recurse =
|
2022-10-18 00:56:45 +00:00
|
|
|
myArgs.forceRecurse ||
|
2022-09-09 11:26:15 +00:00
|
|
|
path.size() == 0; // Dont require `recurseForDerivations
|
|
|
|
// = true;` for top-level attrset
|
|
|
|
|
|
|
|
for (auto &i :
|
2023-01-22 09:28:47 +00:00
|
|
|
v->attrs->lexicographicOrder(state->symbols)) {
|
|
|
|
const std::string &name = state->symbols[i->name];
|
2022-09-09 11:26:15 +00:00
|
|
|
attrs.push_back(name);
|
|
|
|
|
2023-04-24 15:08:57 +00:00
|
|
|
if (name == "recurseForDerivations" &&
|
|
|
|
!myArgs.forceRecurse) {
|
2022-09-09 11:26:15 +00:00
|
|
|
auto attrv =
|
2023-01-22 09:28:47 +00:00
|
|
|
v->attrs->get(state->sRecurseForDerivations);
|
2023-03-09 21:18:00 +00:00
|
|
|
recurse = state->forceBool(
|
|
|
|
*attrv->value, attrv->pos,
|
|
|
|
"while evaluating recurseForDerivations");
|
2022-09-09 11:26:15 +00:00
|
|
|
}
|
2022-04-25 09:23:06 +00:00
|
|
|
}
|
2022-09-09 11:26:15 +00:00
|
|
|
if (recurse)
|
|
|
|
reply["attrs"] = std::move(attrs);
|
|
|
|
else
|
|
|
|
reply["attrs"] = nlohmann::json::array();
|
2020-11-29 14:33:55 +00:00
|
|
|
}
|
2022-09-09 11:26:15 +00:00
|
|
|
} else {
|
|
|
|
// We ignore everything that cannot be build
|
|
|
|
reply["attrs"] = nlohmann::json::array();
|
2020-11-29 14:33:55 +00:00
|
|
|
}
|
2022-04-26 09:15:56 +00:00
|
|
|
} catch (EvalError &e) {
|
2022-01-07 07:20:41 +00:00
|
|
|
auto err = e.info();
|
|
|
|
std::ostringstream oss;
|
|
|
|
showErrorInfo(oss, err, loggerSettings.showTrace.get());
|
|
|
|
auto msg = oss.str();
|
|
|
|
|
2020-11-29 14:33:55 +00:00
|
|
|
// Transmits the error we got from the previous evaluation
|
|
|
|
// in the JSON output.
|
2021-03-14 22:16:57 +00:00
|
|
|
reply["error"] = filterANSIEscapes(msg, true);
|
2020-11-29 14:33:55 +00:00
|
|
|
// Don't forget to print it into the STDERR log, this is
|
|
|
|
// what's shown in the Hydra UI.
|
2022-01-07 07:20:41 +00:00
|
|
|
printError(e.msg());
|
2020-11-29 14:33:55 +00:00
|
|
|
}
|
|
|
|
|
2023-12-09 16:17:01 +00:00
|
|
|
if (tryWriteLine(to.get(), reply.dump()) < 0) {
|
|
|
|
return; // main process died
|
|
|
|
}
|
2020-11-29 14:33:55 +00:00
|
|
|
|
2022-04-21 04:01:02 +00:00
|
|
|
/* If our RSS exceeds the maximum, exit. The collector will
|
2020-11-29 14:33:55 +00:00
|
|
|
start a new process. */
|
|
|
|
struct rusage r;
|
|
|
|
getrusage(RUSAGE_SELF, &r);
|
2022-04-26 09:15:56 +00:00
|
|
|
if ((size_t)r.ru_maxrss > myArgs.maxMemorySize * 1024)
|
|
|
|
break;
|
2020-11-29 14:33:55 +00:00
|
|
|
}
|
|
|
|
|
2023-12-09 16:17:01 +00:00
|
|
|
if (tryWriteLine(to.get(), "restart") < 0) {
|
|
|
|
return; // main process died
|
|
|
|
};
|
2020-11-29 14:33:55 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 09:28:47 +00:00
|
|
|
typedef std::function<void(ref<EvalState> state, Bindings &autoArgs,
|
2022-04-26 09:15:56 +00:00
|
|
|
AutoCloseFD &to, AutoCloseFD &from)>
|
2022-04-15 06:28:19 +00:00
|
|
|
Processor;
|
|
|
|
|
|
|
|
/* Auto-cleanup of fork's process and fds. */
|
|
|
|
struct Proc {
|
|
|
|
AutoCloseFD to, from;
|
|
|
|
Pid pid;
|
|
|
|
|
2022-04-26 09:15:56 +00:00
|
|
|
Proc(const Processor &proc) {
|
2022-04-15 06:28:19 +00:00
|
|
|
Pipe toPipe, fromPipe;
|
|
|
|
toPipe.create();
|
|
|
|
fromPipe.create();
|
|
|
|
auto p = startProcess(
|
|
|
|
[&,
|
|
|
|
to{std::make_shared<AutoCloseFD>(std::move(fromPipe.writeSide))},
|
2022-04-26 09:15:56 +00:00
|
|
|
from{
|
|
|
|
std::make_shared<AutoCloseFD>(std::move(toPipe.readSide))}]() {
|
2022-04-15 06:28:19 +00:00
|
|
|
debug("created worker process %d", getpid());
|
|
|
|
try {
|
2023-01-22 09:28:47 +00:00
|
|
|
auto state = std::make_shared<EvalState>(
|
|
|
|
myArgs.searchPath, openStore(*myArgs.evalStoreUrl));
|
|
|
|
Bindings &autoArgs = *myArgs.getAutoArgs(*state);
|
|
|
|
proc(ref<EvalState>(state), autoArgs, *to, *from);
|
2022-04-26 09:15:56 +00:00
|
|
|
} catch (Error &e) {
|
2022-04-15 06:28:19 +00:00
|
|
|
nlohmann::json err;
|
|
|
|
auto msg = e.msg();
|
|
|
|
err["error"] = filterANSIEscapes(msg, true);
|
|
|
|
printError(msg);
|
2023-12-09 16:17:01 +00:00
|
|
|
if (tryWriteLine(to->get(), err.dump()) < 0) {
|
|
|
|
return; // main process died
|
|
|
|
};
|
2022-04-15 06:28:19 +00:00
|
|
|
// Don't forget to print it into the STDERR log, this is
|
|
|
|
// what's shown in the Hydra UI.
|
2023-12-09 16:17:01 +00:00
|
|
|
if (tryWriteLine(to->get(), "restart") < 0) {
|
|
|
|
return; // main process died
|
|
|
|
}
|
2022-04-15 06:28:19 +00:00
|
|
|
}
|
|
|
|
},
|
2022-04-26 09:15:56 +00:00
|
|
|
ProcessOptions{.allowVfork = false});
|
2022-04-15 06:28:19 +00:00
|
|
|
|
|
|
|
to = std::move(toPipe.writeSide);
|
|
|
|
from = std::move(fromPipe.readSide);
|
|
|
|
pid = p;
|
|
|
|
}
|
|
|
|
|
2022-04-26 09:15:56 +00:00
|
|
|
~Proc() {}
|
2022-04-15 06:28:19 +00:00
|
|
|
};
|
|
|
|
|
2022-04-26 09:15:56 +00:00
|
|
|
struct State {
|
|
|
|
std::set<json> todo = json::array({json::array()});
|
|
|
|
std::set<json> active;
|
|
|
|
std::exception_ptr exc;
|
2022-04-21 04:01:02 +00:00
|
|
|
};
|
|
|
|
|
2023-12-10 07:16:54 +00:00
|
|
|
void handleBrokenWorkerPipe(Proc &proc) {
|
2023-12-09 16:17:01 +00:00
|
|
|
while (1) {
|
2023-12-10 07:16:54 +00:00
|
|
|
int rc = waitpid(proc.pid, nullptr, WNOHANG);
|
2023-12-09 16:17:01 +00:00
|
|
|
if (rc == 0) {
|
2023-12-10 07:16:54 +00:00
|
|
|
proc.pid = -1; // we already took the process status from Proc, no
|
|
|
|
// need to wait for it again to avoid error messages
|
2023-12-09 16:17:01 +00:00
|
|
|
throw Error("BUG: worker pipe closed but worker still running?");
|
|
|
|
} else if (rc == -1) {
|
2023-12-10 07:16:54 +00:00
|
|
|
proc.pid = -1;
|
2023-12-09 16:17:01 +00:00
|
|
|
throw Error("BUG: waitpid waiting for worker failed: %s",
|
|
|
|
strerror(errno));
|
|
|
|
} else {
|
|
|
|
if (WIFEXITED(rc)) {
|
2023-12-10 07:16:54 +00:00
|
|
|
proc.pid = -1;
|
2023-12-09 16:17:01 +00:00
|
|
|
throw Error("evaluation worker exited with %d",
|
|
|
|
WEXITSTATUS(rc));
|
|
|
|
} else if (WIFSIGNALED(rc)) {
|
2023-12-10 07:16:54 +00:00
|
|
|
proc.pid = -1;
|
|
|
|
if (WTERMSIG(rc) == SIGKILL) {
|
|
|
|
throw Error("evaluation worker killed by SIGKILL, maybe "
|
|
|
|
"memory limit reached?");
|
|
|
|
}
|
2023-12-09 16:17:01 +00:00
|
|
|
throw Error("evaluation worker killed by signal %d",
|
|
|
|
WTERMSIG(rc));
|
|
|
|
} // else ignore WIFSTOPPED and WIFCONTINUED
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-26 09:15:56 +00:00
|
|
|
std::function<void()> collector(Sync<State> &state_,
|
|
|
|
std::condition_variable &wakeup) {
|
2022-04-21 04:01:02 +00:00
|
|
|
return [&]() {
|
|
|
|
try {
|
|
|
|
std::optional<std::unique_ptr<Proc>> proc_;
|
2023-12-10 07:16:54 +00:00
|
|
|
std::optional<std::unique_ptr<LineReader>> fromReader_;
|
2022-04-21 04:01:02 +00:00
|
|
|
|
|
|
|
while (true) {
|
2023-12-10 07:16:54 +00:00
|
|
|
if (!proc_.has_value()) {
|
|
|
|
proc_ = std::make_unique<Proc>(worker);
|
|
|
|
fromReader_ = std::make_unique<LineReader>(
|
|
|
|
proc_.value()->from.release());
|
|
|
|
}
|
|
|
|
auto proc = std::move(proc_.value());
|
|
|
|
auto fromReader = std::move(fromReader_.value());
|
2022-04-21 04:01:02 +00:00
|
|
|
|
|
|
|
/* Check whether the existing worker process is still there. */
|
2023-12-10 07:16:54 +00:00
|
|
|
auto s = fromReader->readLine();
|
|
|
|
if (s == "") {
|
|
|
|
handleBrokenWorkerPipe(*proc.get());
|
|
|
|
} else if (s == "restart") {
|
2022-04-21 04:01:02 +00:00
|
|
|
proc_ = std::nullopt;
|
2023-12-10 07:16:54 +00:00
|
|
|
fromReader_ = std::nullopt;
|
2022-04-21 04:01:02 +00:00
|
|
|
continue;
|
|
|
|
} else if (s != "next") {
|
2023-12-10 07:16:54 +00:00
|
|
|
try {
|
|
|
|
auto json = json::parse(s);
|
|
|
|
throw Error("worker error: %s",
|
|
|
|
(std::string)json["error"]);
|
|
|
|
} catch (const json::exception &e) {
|
|
|
|
throw Error(
|
|
|
|
"Received invalid JSON from worker: %s '%s'",
|
|
|
|
e.what(), s);
|
|
|
|
}
|
2022-04-21 04:01:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for a job name to become available. */
|
2022-04-26 05:11:17 +00:00
|
|
|
json attrPath;
|
2022-04-21 04:01:02 +00:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
checkInterrupt();
|
|
|
|
auto state(state_.lock());
|
2022-04-26 09:15:56 +00:00
|
|
|
if ((state->todo.empty() && state->active.empty()) ||
|
|
|
|
state->exc) {
|
2023-12-09 16:17:01 +00:00
|
|
|
if (tryWriteLine(proc->to.get(), "exit") < 0) {
|
2023-12-10 07:16:54 +00:00
|
|
|
handleBrokenWorkerPipe(*proc.get());
|
2023-12-09 16:17:01 +00:00
|
|
|
}
|
2022-04-21 04:01:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!state->todo.empty()) {
|
|
|
|
attrPath = *state->todo.begin();
|
|
|
|
state->todo.erase(state->todo.begin());
|
|
|
|
state->active.insert(attrPath);
|
|
|
|
break;
|
|
|
|
} else
|
|
|
|
state.wait(wakeup);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Tell the worker to evaluate it. */
|
2023-12-09 16:17:01 +00:00
|
|
|
if (tryWriteLine(proc->to.get(), "do " + attrPath.dump()) < 0) {
|
2023-12-10 07:16:54 +00:00
|
|
|
handleBrokenWorkerPipe(*proc.get());
|
2023-12-09 16:17:01 +00:00
|
|
|
}
|
2022-04-21 04:01:02 +00:00
|
|
|
|
|
|
|
/* Wait for the response. */
|
2023-12-10 07:16:54 +00:00
|
|
|
auto respString = fromReader->readLine();
|
|
|
|
if (respString == "") {
|
|
|
|
handleBrokenWorkerPipe(*proc.get());
|
|
|
|
}
|
|
|
|
json response;
|
|
|
|
try {
|
|
|
|
response = json::parse(respString);
|
|
|
|
if (response.find("error") != response.end()) {
|
|
|
|
throw Error("worker error: %s",
|
|
|
|
(std::string)response["error"]);
|
|
|
|
}
|
|
|
|
} catch (const json::exception &e) {
|
|
|
|
throw Error("Received invalid JSON from worker: %s '%s'",
|
|
|
|
e.what(), respString);
|
|
|
|
}
|
2022-04-21 04:01:02 +00:00
|
|
|
|
|
|
|
/* Handle the response. */
|
2022-04-26 05:11:17 +00:00
|
|
|
std::vector<json> newAttrs;
|
2022-04-21 04:01:02 +00:00
|
|
|
if (response.find("attrs") != response.end()) {
|
2022-04-26 09:15:56 +00:00
|
|
|
for (auto &i : response["attrs"]) {
|
|
|
|
json newAttr = json(response["attrPath"]);
|
|
|
|
newAttr.emplace_back(i);
|
|
|
|
newAttrs.push_back(newAttr);
|
2022-04-21 04:01:02 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
auto state(state_.lock());
|
|
|
|
std::cout << respString << "\n" << std::flush;
|
|
|
|
}
|
|
|
|
|
|
|
|
proc_ = std::move(proc);
|
2023-12-10 07:16:54 +00:00
|
|
|
fromReader_ = std::move(fromReader);
|
2022-04-21 04:01:02 +00:00
|
|
|
|
|
|
|
/* Add newly discovered job names to the queue. */
|
|
|
|
{
|
|
|
|
auto state(state_.lock());
|
|
|
|
state->active.erase(attrPath);
|
2022-04-26 05:11:17 +00:00
|
|
|
for (auto p : newAttrs) {
|
|
|
|
state->todo.insert(p);
|
|
|
|
}
|
2022-04-21 04:01:02 +00:00
|
|
|
wakeup.notify_all();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (...) {
|
|
|
|
auto state(state_.lock());
|
|
|
|
state->exc = std::current_exception();
|
|
|
|
wakeup.notify_all();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-04-26 09:15:56 +00:00
|
|
|
int main(int argc, char **argv) {
|
2020-11-29 14:33:55 +00:00
|
|
|
/* Prevent undeclared dependencies in the evaluation via
|
|
|
|
$NIX_PATH. */
|
|
|
|
unsetenv("NIX_PATH");
|
|
|
|
|
2022-04-21 16:31:53 +00:00
|
|
|
/* We are doing the garbage collection by killing forks */
|
|
|
|
setenv("GC_DONT_GC", "1", 1);
|
|
|
|
|
2020-11-29 14:33:55 +00:00
|
|
|
return handleExceptions(argv[0], [&]() {
|
|
|
|
initNix();
|
|
|
|
initGC();
|
|
|
|
|
2023-12-10 13:22:32 +00:00
|
|
|
myArgs.parseArgs(argv, argc);
|
2020-11-29 14:33:55 +00:00
|
|
|
|
2022-04-26 09:15:56 +00:00
|
|
|
/* FIXME: The build hook in conjunction with import-from-derivation is
|
|
|
|
* causing "unexpected EOF" during eval */
|
2020-11-29 14:33:55 +00:00
|
|
|
settings.builders = "";
|
|
|
|
|
|
|
|
/* Prevent access to paths outside of the Nix search path and
|
|
|
|
to the environment. */
|
2021-03-14 22:32:36 +00:00
|
|
|
evalSettings.restrictEval = false;
|
2020-11-29 14:33:55 +00:00
|
|
|
|
|
|
|
/* When building a flake, use pure evaluation (no access to
|
|
|
|
'getEnv', 'currentSystem' etc. */
|
2022-08-17 06:38:37 +00:00
|
|
|
if (myArgs.impure) {
|
|
|
|
evalSettings.pureEval = false;
|
|
|
|
} else if (myArgs.flake) {
|
|
|
|
evalSettings.pureEval = true;
|
|
|
|
}
|
2020-11-29 14:33:55 +00:00
|
|
|
|
2022-04-26 09:15:56 +00:00
|
|
|
if (myArgs.releaseExpr == "")
|
|
|
|
throw UsageError("no expression specified");
|
2020-11-29 14:33:55 +00:00
|
|
|
|
2022-09-20 18:12:55 +00:00
|
|
|
if (myArgs.gcRootsDir == "") {
|
2022-04-26 09:15:56 +00:00
|
|
|
printMsg(lvlError, "warning: `--gc-roots-dir' not specified");
|
2022-09-20 18:12:55 +00:00
|
|
|
} else {
|
|
|
|
myArgs.gcRootsDir = std::filesystem::absolute(myArgs.gcRootsDir);
|
|
|
|
}
|
2020-11-29 14:33:55 +00:00
|
|
|
|
2022-01-07 07:20:41 +00:00
|
|
|
if (myArgs.showTrace) {
|
|
|
|
loggerSettings.showTrace.assign(true);
|
|
|
|
}
|
|
|
|
|
2020-11-29 14:33:55 +00:00
|
|
|
Sync<State> state_;
|
|
|
|
|
2022-04-21 04:01:02 +00:00
|
|
|
/* Start a collector thread per worker process. */
|
2020-11-29 14:33:55 +00:00
|
|
|
std::vector<std::thread> threads;
|
2022-04-21 04:01:02 +00:00
|
|
|
std::condition_variable wakeup;
|
2020-11-29 14:33:55 +00:00
|
|
|
for (size_t i = 0; i < myArgs.nrWorkers; i++)
|
2022-04-21 04:01:02 +00:00
|
|
|
threads.emplace_back(std::thread(collector(state_, wakeup)));
|
2020-11-29 14:33:55 +00:00
|
|
|
|
2022-04-26 09:15:56 +00:00
|
|
|
for (auto &thread : threads)
|
2020-11-29 14:33:55 +00:00
|
|
|
thread.join();
|
|
|
|
|
|
|
|
auto state(state_.lock());
|
|
|
|
|
|
|
|
if (state->exc)
|
|
|
|
std::rethrow_exception(state->exc);
|
|
|
|
});
|
|
|
|
}
|