mirror of
https://github.com/savoirfairelinux/opendht.git
synced 2026-11-08 12:28:42 +00:00
asio sockets are not thread-safe. http::Connection::close() used to close the socket synchronously on the calling thread. Request::cancel() and Request::~Request() (via terminate()) are called from arbitrary threads (e.g. DhtProxyClient::stop(), cancelAllListeners(), cancelListen()), so the close raced with in-flight operations on the io_context thread. In particular, asio::async_connect's range_connect_op closes and re-opens the socket when trying the next endpoint (common on mobile where the IPv6 endpoint fails first). A concurrent close() from another thread corrupts the epoll reactor's per-descriptor state, leading to crashes in epoll_reactor::deregister_descriptor / register_descriptor called from range_connect_op::process. Dispatch the close to the io_context thread when not already on it. Also keep the connection alive during the TLS handshake, like every other asynchronous operation, so that the destructor can never run while an operation is pending. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>