From 155c12edeab26e5838bf785828ce527d1c3dfb32 Mon Sep 17 00:00:00 2001 From: levlam Date: Sun, 22 Nov 2020 23:30:40 +0300 Subject: [PATCH] Improve ConcurrentScheduler closing on exit. --- td/telegram/Client.cpp | 4 +--- tdactor/td/actor/impl/ConcurrentScheduler.cpp | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/td/telegram/Client.cpp b/td/telegram/Client.cpp index eddd5bd85..829ccdcbb 100644 --- a/td/telegram/Client.cpp +++ b/td/telegram/Client.cpp @@ -185,9 +185,7 @@ class ClientManager::Impl final { while (!tds_.empty() && !ExitGuard::is_exited()) { receive(0.1); } - if (!ExitGuard::is_exited()) { // prevent closing of schedulers from already killed by OS threads - concurrent_scheduler_->finish(); - } + concurrent_scheduler_->finish(); } private: diff --git a/tdactor/td/actor/impl/ConcurrentScheduler.cpp b/tdactor/td/actor/impl/ConcurrentScheduler.cpp index f2676b42d..e32de3be2 100644 --- a/tdactor/td/actor/impl/ConcurrentScheduler.cpp +++ b/tdactor/td/actor/impl/ConcurrentScheduler.cpp @@ -11,6 +11,7 @@ #include "td/actor/impl/ActorInfo.h" #include "td/actor/impl/Scheduler.h" +#include "td/utils/ExitGuard.h" #include "td/utils/MpscPollableQueue.h" #include "td/utils/port/thread_local.h" @@ -140,6 +141,19 @@ void ConcurrentScheduler::finish() { detail::Iocp::Guard iocp_guard(iocp_.get()); #endif + if (ExitGuard::is_exited()) { + // prevent closing of schedulers from already killed by OS threads + for (auto &thread : threads_) { + thread.detach(); + } + +#if TD_PORT_WINDOWS + iocp_->interrupt_loop(); + iocp_thread_.detach(); +#endif + return; + } + #if !TD_THREAD_UNSUPPORTED && !TD_EVENTFD_UNSUPPORTED for (auto &thread : threads_) { thread.join();