Skip to content

Commit a740a01

Browse files
committed
Improve worker resources cleanup
1 parent b9c0868 commit a740a01

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

yasio/io_service.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -833,9 +833,13 @@ void io_service::handle_stop()
833833
this->worker_.join();
834834
if (this->state_ != state::AT_EXITING)
835835
{
836-
// after join if state not AT_EXITING, means worker thread was terminate outside
836+
// after join if state not AT_EXITING, means worker thread was terminated externally
837837
// i.g .net managed exception occurred when invoke c# delegate
838-
this->state_ = state::AT_EXITING;
838+
YASIO_KLOGW("[core] the worker thread terminated unexpectedly");
839+
handle_worker_exit();
840+
// clear pending events to prevent dispatch to io event handler due to the handler
841+
// has unexpected exception
842+
this->events_.clear();
839843
}
840844
}
841845

@@ -1021,14 +1025,17 @@ void io_service::run()
10211025

10221026
} while (!this->stop_flag_ || !this->transports_.empty());
10231027

1028+
handle_worker_exit();
1029+
}
1030+
void io_service::handle_worker_exit()
1031+
{
10241032
#if defined(YASIO_USE_CARES)
10251033
destroy_ares_channel();
10261034
#endif
10271035
#if defined(YASIO_SSL_BACKEND)
10281036
cleanup_ssl_context(YSSL_CLIENT);
10291037
cleanup_ssl_context(YSSL_SERVER);
10301038
#endif
1031-
10321039
this->state_ = io_service::state::AT_EXITING;
10331040
}
10341041
void io_service::process_transports()

yasio/io_service.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,6 +1199,7 @@ class YASIO_API io_service // lgtm [cpp/class-many-fields]
11991199

12001200
YASIO__DECL bool cleanup_channel(io_channel* channel, bool clear_mask = true);
12011201
YASIO__DECL bool cleanup_io(io_base* obj, bool clear_mask = true);
1202+
YASIO__DECL void handle_worker_exit();
12021203

12031204
YASIO__DECL void handle_close(transport_handle_t);
12041205

0 commit comments

Comments
 (0)