1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-08 19:46:02 +01:00

Merge pull request #14121 from obsidiansystems/file-transfer-quit

Some Curl file transfer cleanups
This commit is contained in:
Jörg Thalheim 2025-09-30 09:12:08 +02:00 committed by GitHub
commit a5facbd2d1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -594,10 +594,24 @@ struct curlFileTransfer : public FileTransfer
}
};
bool quit = false;
std::
priority_queue<std::shared_ptr<TransferItem>, std::vector<std::shared_ptr<TransferItem>>, EmbargoComparator>
incoming;
private:
bool quitting = false;
public:
void quit()
{
quitting = true;
/* We wil not be processing any more incomming requests */
while (!incoming.empty())
incoming.pop();
}
bool isQuitting()
{
return quitting;
}
};
Sync<State> state_;
@ -649,7 +663,7 @@ struct curlFileTransfer : public FileTransfer
/* Signal the worker thread to exit. */
{
auto state(state_.lock());
state->quit = true;
state->quit();
}
#ifndef _WIN32 // TODO need graceful async exit support on Windows?
writeFull(wakeupPipe.writeSide.get(), " ", false);
@ -750,7 +764,7 @@ struct curlFileTransfer : public FileTransfer
break;
}
}
quit = state->quit;
quit = state->isQuitting();
}
for (auto & item : incoming) {
@ -767,18 +781,20 @@ struct curlFileTransfer : public FileTransfer
void workerThreadEntry()
{
// Unwinding or because someone called `quit`.
bool normalExit = true;
try {
workerThreadMain();
} catch (nix::Interrupted & e) {
normalExit = false;
} catch (std::exception & e) {
printError("unexpected error in download thread: %s", e.what());
normalExit = false;
}
{
if (!normalExit) {
auto state(state_.lock());
while (!state->incoming.empty())
state->incoming.pop();
state->quit = true;
state->quit();
}
}
@ -789,7 +805,7 @@ struct curlFileTransfer : public FileTransfer
{
auto state(state_.lock());
if (state->quit)
if (state->isQuitting())
throw nix::Error("cannot enqueue download request because the download thread is shutting down");
state->incoming.push(item);
}
@ -845,7 +861,7 @@ ref<FileTransfer> getFileTransfer()
{
static ref<curlFileTransfer> fileTransfer = makeCurlFileTransfer();
if (fileTransfer->state_.lock()->quit)
if (fileTransfer->state_.lock()->isQuitting())
fileTransfer = makeCurlFileTransfer();
return fileTransfer;