Fix event loop blocking on DNS abort in CppDnsLookupWorker
abortLookup() called m_result.get() synchronously, blocking the entire event loop while waiting for a potentially long getaddrinfo() call to finish (~18s when the DNS server is unreachable). This caused the HTTP /status endpoint and all other event loop processing to stall whenever a TrunkLink reconnect cycle aborted a pending DNS lookup. Move the future and thread context to a detached cleanup thread so the event loop returns immediately.
This commit is contained in:
1 parent
9f5ed86196
commit
64697e61d7
1 file changed
+14
-5
@@ -45,6 +45,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
@@ -249,11 +250,6 @@ bool CppDnsLookupWorker::doLookup(void)
|
|||||||
|
|
||||||
void CppDnsLookupWorker::abortLookup(void)
|
void CppDnsLookupWorker::abortLookup(void)
|
||||||
{
|
{
|
||||||
if (m_result.valid())
|
|
||||||
{
|
|
||||||
m_result.get();
|
|
||||||
}
|
|
||||||
|
|
||||||
int fd = m_notifier_watch.fd();
|
int fd = m_notifier_watch.fd();
|
||||||
if (fd >= 0)
|
if (fd >= 0)
|
||||||
{
|
{
|
||||||
@@ -261,7 +257,20 @@ void CppDnsLookupWorker::abortLookup(void)
|
|||||||
close(fd);
|
close(fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (m_result.valid())
|
||||||
|
{
|
||||||
|
// Move the future and context to a detached thread so the event loop
|
||||||
|
// is not blocked waiting for a potentially long getaddrinfo() call.
|
||||||
|
auto result = std::move(m_result);
|
||||||
|
auto ctx = std::move(m_ctx);
|
||||||
|
std::thread([result = std::move(result), ctx = std::move(ctx)]() mutable {
|
||||||
|
result.get();
|
||||||
|
}).detach();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
m_ctx.reset();
|
m_ctx.reset();
|
||||||
|
}
|
||||||
} /* CppDnsLookupWorker::abortLookup */
|
} /* CppDnsLookupWorker::abortLookup */
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user