1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-09 12:06:01 +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:: std::
priority_queue<std::shared_ptr<TransferItem>, std::vector<std::shared_ptr<TransferItem>>, EmbargoComparator> priority_queue<std::shared_ptr<TransferItem>, std::vector<std::shared_ptr<TransferItem>>, EmbargoComparator>
incoming; 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_; Sync<State> state_;
@ -649,7 +663,7 @@ struct curlFileTransfer : public FileTransfer
/* Signal the worker thread to exit. */ /* Signal the worker thread to exit. */
{ {
auto state(state_.lock()); auto state(state_.lock());
state->quit = true; state->quit();
} }
#ifndef _WIN32 // TODO need graceful async exit support on Windows? #ifndef _WIN32 // TODO need graceful async exit support on Windows?
writeFull(wakeupPipe.writeSide.get(), " ", false); writeFull(wakeupPipe.writeSide.get(), " ", false);
@ -750,7 +764,7 @@ struct curlFileTransfer : public FileTransfer
break; break;
} }
} }
quit = state->quit; quit = state->isQuitting();
} }
for (auto & item : incoming) { for (auto & item : incoming) {
@ -767,18 +781,20 @@ struct curlFileTransfer : public FileTransfer
void workerThreadEntry() void workerThreadEntry()
{ {
// Unwinding or because someone called `quit`.
bool normalExit = true;
try { try {
workerThreadMain(); workerThreadMain();
} catch (nix::Interrupted & e) { } catch (nix::Interrupted & e) {
normalExit = false;
} catch (std::exception & e) { } catch (std::exception & e) {
printError("unexpected error in download thread: %s", e.what()); printError("unexpected error in download thread: %s", e.what());
normalExit = false;
} }
{ if (!normalExit) {
auto state(state_.lock()); auto state(state_.lock());
while (!state->incoming.empty()) state->quit();
state->incoming.pop();
state->quit = true;
} }
} }
@ -789,7 +805,7 @@ struct curlFileTransfer : public FileTransfer
{ {
auto state(state_.lock()); auto state(state_.lock());
if (state->quit) if (state->isQuitting())
throw nix::Error("cannot enqueue download request because the download thread is shutting down"); throw nix::Error("cannot enqueue download request because the download thread is shutting down");
state->incoming.push(item); state->incoming.push(item);
} }
@ -845,7 +861,7 @@ ref<FileTransfer> getFileTransfer()
{ {
static ref<curlFileTransfer> fileTransfer = makeCurlFileTransfer(); static ref<curlFileTransfer> fileTransfer = makeCurlFileTransfer();
if (fileTransfer->state_.lock()->quit) if (fileTransfer->state_.lock()->isQuitting())
fileTransfer = makeCurlFileTransfer(); fileTransfer = makeCurlFileTransfer();
return fileTransfer; return fileTransfer;