Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/libstore/gc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,17 @@ void LocalStore::collectGarbage(const GCOptions & options, GCResults & results)

debug("GC roots server accepted new client");

/* Process the connection in a separate thread. */
/* Process the connection in a separate thread. Hold
the `connections` lock while starting the thread
and registering it, so that the thread's cleanup
handler (which removes it from `connections`)
can't run before it has been inserted. Otherwise
a client that disconnects immediately would leave
a stale entry, and a later client that gets the
same fd number would cause a duplicate key insert,
destroying a joinable std::thread and thus calling
std::terminate(). */
auto conn(connections.lock());
auto fdClient_ = fdClient.get();
std::thread clientThread([&, fdClient = std::move(fdClient)]() {
Finally cleanup([&]() {
Expand Down Expand Up @@ -516,7 +526,8 @@ void LocalStore::collectGarbage(const GCOptions & options, GCResults & results)
}
});

connections.lock()->insert({fdClient_, std::move(clientThread)});
auto [it, inserted] = conn->insert({fdClient_, std::move(clientThread)});
assert(inserted);
}
}
});
Expand Down
Loading