Skip to content

Commit

Permalink
rec: make quit-nicely wait on actual quit
Browse files Browse the repository at this point in the history
  • Loading branch information
omoerbeek committed Dec 16, 2024
1 parent 3dfd8e3 commit 34774c4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
21 changes: 19 additions & 2 deletions pdns/recursordist/rec-main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ int RecThreadInfo::runThreads(Logr::log_t log)
recursorThread();

for (unsigned int thread = 0; thread < RecThreadInfo::numRecursorThreads(); thread++) {
if (thread == 1) {
if (thread == 0 || thread == 1) {
continue;
}
auto& tInfo = RecThreadInfo::info(thread);
Expand Down Expand Up @@ -351,6 +351,9 @@ int RecThreadInfo::runThreads(Logr::log_t log)
info.start(currentThreadId, "web+stat", cpusMap, log);

for (auto& tInfo : RecThreadInfo::infos()) {
if (tInfo.getName() == "web+stat") { // XXX testing for isHandler() does not work as expected!
continue;
}
tInfo.thread.join();
if (tInfo.exitCode != 0) {
ret = tInfo.exitCode;
Expand Down Expand Up @@ -2446,8 +2449,14 @@ static void handleRCC(int fileDesc, FDMultiplexer::funcparam_t& /* var */)
RecursorControlParser::func_t* command = nullptr;
auto answer = RecursorControlParser::getAnswer(clientfd, msg, &command);

g_rcc.send(clientfd, answer);
if (command != doExitNicely) {
g_rcc.send(clientfd, answer);
}
command();
if (command == doExitNicely) {
g_rcc.send(clientfd, answer);
g_rcc.~RecursorControlChannel();
}
}
catch (const std::exception& e) {
SLOG(g_log << Logger::Error << "Error dealing with control socket request: " << e.what() << endl,
Expand Down Expand Up @@ -3149,6 +3158,8 @@ static void setupLogging(const string& logname)
}
}

DoneRunning doneRunning;

int main(int argc, char** argv)
{
g_argc = argc;
Expand Down Expand Up @@ -3318,6 +3329,12 @@ int main(int argc, char** argv)
}

ret = serviceMain(startupLog);
{
std::lock_guard lock (doneRunning.mutex);
doneRunning.done = true;
doneRunning.condVar.notify_one();
}
RecThreadInfo::joinThread0();
}
catch (const PDNSException& ae) {
SLOG(g_log << Logger::Error << "Exception: " << ae.reason << endl,
Expand Down
12 changes: 12 additions & 0 deletions pdns/recursordist/rec-main.hh
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,13 @@ extern thread_local unique_ptr<FDMultiplexer> t_fdm;
extern uint16_t g_minUdpSourcePort;
extern uint16_t g_maxUdpSourcePort;
extern bool g_regressionTestMode;
struct DoneRunning
{
std::mutex mutex;
std::condition_variable condVar;
std::atomic<bool> done{false};
};
extern DoneRunning doneRunning;

// you can ask this class for a UDP socket to send a query from
// this socket is not yours, don't even think about deleting it
Expand Down Expand Up @@ -539,6 +546,11 @@ public:
mt = theMT;
}

static void joinThread0()
{
info(0).thread.join();
}

private:
// FD corresponding to TCP sockets this thread is listening on.
// These FDs are also in deferredAdds when we have one socket per
Expand Down
7 changes: 6 additions & 1 deletion pdns/recursordist/rec_channel_rec.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1379,16 +1379,21 @@ void doExitGeneric(bool nicely)
_exit(0); // regression test check for exit 0
#endif
g_log << Logger::Error << "Exiting on user request" << endl;
g_rcc.~RecursorControlChannel();

if (!g_pidfname.empty()) {
unlink(g_pidfname.c_str()); // we can at least try..
}

if (nicely) {
RecursorControlChannel::stop = true;
{
std::unique_lock lock(doneRunning.mutex);
doneRunning.condVar.wait(lock, [] { return doneRunning.done.load(); });
}
// g_rcc.~RecursorControlChannel() do not call, will be done by caller!
}
else {
g_rcc.~RecursorControlChannel();
#if defined(__SANITIZE_ADDRESS__) && defined(HAVE_LEAK_SANITIZER_INTERFACE)
clearLuaScript();
pdns::coverage::dumpCoverageData();
Expand Down

0 comments on commit 34774c4

Please sign in to comment.