From 890b7095daf36ea478f74d6dd3e6a5a86b000c6d Mon Sep 17 00:00:00 2001 From: Takeshi Yoshino <4511440+tyoshino@users.noreply.github.com> Date: Mon, 27 Apr 2026 13:12:20 +0000 Subject: [PATCH] feat(wish/cpp): make sure to stop the event loop in the destructor --- wish/cpp/src/plain_client.cc | 3 +++ wish/cpp/src/tls_client.cc | 9 ++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/wish/cpp/src/plain_client.cc b/wish/cpp/src/plain_client.cc index 943e291..3f33eca 100644 --- a/wish/cpp/src/plain_client.cc +++ b/wish/cpp/src/plain_client.cc @@ -10,6 +10,9 @@ PlainClient::PlainClient(const std::string& host, int port) handler_(nullptr) {} PlainClient::~PlainClient() { + if (base_) { + event_base_loopbreak(base_); + } if (dns_base_) { evdns_base_free(dns_base_, 0); } diff --git a/wish/cpp/src/tls_client.cc b/wish/cpp/src/tls_client.cc index 1ceb56a..9c52e90 100644 --- a/wish/cpp/src/tls_client.cc +++ b/wish/cpp/src/tls_client.cc @@ -20,15 +20,18 @@ TlsClient::TlsClient(const std::string& ca_file, const std::string& cert_file, handler_(nullptr) {} TlsClient::~TlsClient() { + // Signal the event loop to exit before freeing it. If Run() is still + // executing in another thread (e.g. the caller forgot to call Stop()), + // event_base_loopbreak wakes it up immediately so event_base_free is safe. + if (base_) { + event_base_loopbreak(base_); + } if (dns_base_) { evdns_base_free(dns_base_, 0); } if (base_) { event_base_free(base_); } - // WishHandler deletes itself when the connection closes - // But if it wasn't started, we might need to delete it. - // Assuming it manages its own lifecycle for now. } bool TlsClient::Init() {