1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-12-14 04:51:05 +01:00

Merge pull request #13860 from obsidiansystems/derivation-building-resources-code-cleanup

Derivation building resources code cleanup
This commit is contained in:
Jörg Thalheim 2025-09-01 20:22:30 +02:00 committed by GitHub
commit de7f137f31
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 127 additions and 136 deletions

View file

@ -54,24 +54,9 @@ DerivationBuildingGoal::~DerivationBuildingGoal()
{
/* Careful: we should never ever throw an exception from a
destructor. */
try {
killChild();
} catch (...) {
ignoreExceptionInDestructor();
}
#ifndef _WIN32 // TODO enable `DerivationBuilder` on Windows
if (builder) {
try {
builder->stopDaemon();
} catch (...) {
ignoreExceptionInDestructor();
}
try {
builder->deleteTmpDir(false);
} catch (...) {
ignoreExceptionInDestructor();
}
}
if (builder)
builder.reset();
#endif
try {
closeLogFile();
@ -95,22 +80,8 @@ void DerivationBuildingGoal::killChild()
hook.reset();
#endif
#ifndef _WIN32 // TODO enable `DerivationBuilder` on Windows
if (builder && builder->pid != -1) {
if (builder && builder->killChild())
worker.childTerminated(this);
// FIXME: move this into DerivationBuilder.
/* If we're using a build user, then there is a tricky race
condition: if we kill the build user before the child has
done its setuid() to the build user uid, then it won't be
killed, and we'll potentially lock up in pid.wait(). So
also send a conventional kill to the child. */
::kill(-builder->pid, SIGKILL); /* ignore the result */
builder->killSandbox(true);
builder->pid.wait();
}
#endif
}
@ -653,11 +624,6 @@ Goal::Co DerivationBuildingGoal::tryToBuild()
goal.worker.childTerminated(&goal);
}
void markContentsGood(const StorePath & path) override
{
goal.worker.markContentsGood(path);
}
Path openLogFile() override
{
return goal.openLogFile();
@ -756,21 +722,22 @@ Goal::Co DerivationBuildingGoal::tryToBuild()
trace("build done");
auto res = builder->unprepareBuild();
// N.B. cannot use `std::visit` with co-routine return
if (auto * ste = std::get_if<0>(&res)) {
SingleDrvOutputs builtOutputs;
try {
builtOutputs = builder->unprepareBuild();
} catch (BuildError & e) {
outputLocks.unlock();
// Allow selecting a subset of enum values
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wswitch-enum"
switch (ste->status) {
switch (e.status) {
case BuildResult::HashMismatch:
worker.hashMismatch = true;
/* See header, the protocols don't know about `HashMismatch`
yet, so change it to `OutputRejected`, which they expect
for this case (hash mismatch is a type of output
rejection). */
ste->status = BuildResult::OutputRejected;
e.status = BuildResult::OutputRejected;
break;
case BuildResult::NotDeterministic:
worker.checkMismatch = true;
@ -780,11 +747,15 @@ Goal::Co DerivationBuildingGoal::tryToBuild()
break;
}
# pragma GCC diagnostic pop
co_return doneFailure(std::move(*ste));
} else if (auto * builtOutputs = std::get_if<1>(&res)) {
co_return doneFailure(std::move(e));
}
{
StorePathSet outputPaths;
for (auto & [_, output] : *builtOutputs)
for (auto & [_, output] : builtOutputs) {
// for sake of `bmRepair`
worker.markContentsGood(output.outPath);
outputPaths.insert(output.outPath);
}
runPostBuildHook(worker.store, *logger, drvPath, outputPaths);
/* It is now safe to delete the lock files, since all future
@ -793,9 +764,7 @@ Goal::Co DerivationBuildingGoal::tryToBuild()
(unlinked) lock files. */
outputLocks.setDeletion(true);
outputLocks.unlock();
co_return doneSuccess(BuildResult::Built, std::move(*builtOutputs));
} else {
unreachable();
co_return doneSuccess(BuildResult::Built, std::move(builtOutputs));
}
#endif
}