mirror of
https://github.com/savoirfairelinux/opendht.git
synced 2026-11-08 12:28:42 +00:00
Resolver::add_callback invoked the callback synchronously on the calling thread when resolution was already completed. Since Request objects are commonly created and sent from application threads while the io_context runs on a dedicated io thread, the whole continuation (Request::send -> Request::connect -> Connection::async_connect -> asio::async_connect start_connect_op) then executed on the caller's thread, mutating asio reactor state (kqueue/epoll per-descriptor op queues) concurrently with the io thread. This data race corrupts the heap and was observed crashing with SIGSEGV/EXC_BAD_ACCESS inside asio start_connect_op, both from the io thread and from the application main thread (e.g. a name lookup reusing a shared, already-completed Resolver), on macOS. Fix by always posting completed-resolution callbacks to the resolver's executor, so the connect chain consistently runs on the io_context thread regardless of the caller. Also make the Connection::async_connect completion handler hold a weak_ptr to the Connection instead of capturing raw `this` and a reference to the underlying socket, matching the existing async_handshake pattern, so a Connection destroyed while a connect operation is in flight (e.g. after a request timeout) can no longer be written to after free. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>