diff --git a/src/MoqxPicoRelayServer.cpp b/src/MoqxPicoRelayServer.cpp index 62aed59..4f8a171 100644 --- a/src/MoqxPicoRelayServer.cpp +++ b/src/MoqxPicoRelayServer.cpp @@ -93,12 +93,14 @@ MoqxPicoRelayServer::~MoqxPicoRelayServer() { } void MoqxPicoRelayServer::stop() { - if (!context_) { + if (stopped_) { return; } + stopped_ = true; + // Keep context_ alive: terminateClientSession can run after stop() returns, + // from handleClientSession coroutines still draining on the evb. context_->stop(); evb_->runImmediatelyOrRunInEventBaseThreadAndWait([this] { MoQPicoQuicEventBaseServer::stop(); }); - context_.reset(); } void MoqxPicoRelayServer::setStatsRegistry(std::shared_ptr registry) { diff --git a/src/MoqxPicoRelayServer.h b/src/MoqxPicoRelayServer.h index cda2fbf..e35fc88 100644 --- a/src/MoqxPicoRelayServer.h +++ b/src/MoqxPicoRelayServer.h @@ -64,6 +64,7 @@ class MoqxPicoRelayServer : public moxygen::MoQPicoQuicEventBaseServer { config::ListenerConfig listenerCfg_; std::shared_ptr context_; folly::EventBase* evb_; + bool stopped_{false}; }; } // namespace openmoq::moqx diff --git a/src/MoqxRelayServer.cpp b/src/MoqxRelayServer.cpp index aedc404..1bd3a84 100644 --- a/src/MoqxRelayServer.cpp +++ b/src/MoqxRelayServer.cpp @@ -143,12 +143,13 @@ MoqxRelayServer::~MoqxRelayServer() { } void MoqxRelayServer::stop() { - if (!context_) { + if (stopped_) { return; } - // QuicServer::shutdown drives terminateClientSession, which uses context_. + stopped_ = true; + // Keep context_ alive: terminateClientSession can run after stop() returns, + // from handleClientSession coroutines still draining on IO threads. MoQServer::stop(); - context_.reset(); } void MoqxRelayServer::setStatsRegistry(std::shared_ptr registry) { diff --git a/src/MoqxRelayServer.h b/src/MoqxRelayServer.h index 0dd0fb5..f99524c 100644 --- a/src/MoqxRelayServer.h +++ b/src/MoqxRelayServer.h @@ -57,6 +57,7 @@ class MoqxRelayServer : public moxygen::MoQServer { config::ListenerConfig listenerCfg_; std::shared_ptr context_; folly::IOThreadPoolExecutor* ioExecutor_; + bool stopped_{false}; }; } // namespace openmoq::moqx