@@ -343,23 +343,9 @@ OnionRequestRouter::OnionRequestRouter(
343343}
344344
345345OnionRequestRouter::~OnionRequestRouter () {
346- std::vector<std::thread> threads_to_join;
347-
348346 // Use 'call_get' to force this to be synchronous
349347 if (_loop)
350- _loop->call_get ([this , &threads_to_join] {
351- // Harvest upload thread handles *before* _close_connections clears the map
352- for (auto & [_, upload] : _active_uploads)
353- if (upload.second .joinable ())
354- threads_to_join.push_back (std::move (upload.second ));
355-
356- _close_connections ();
357- });
358-
359- // Block until upload threads have finished
360- for (auto & t : threads_to_join)
361- if (t.joinable ())
362- t.join ();
348+ _loop->call_get ([this ] { _close_connections (); });
363349
364350 log::debug (cat, " Destroyed." );
365351}
@@ -662,8 +648,13 @@ void OnionRequestRouter::_pre_build_paths_if_needed() {
662648}
663649
664650void OnionRequestRouter::_close_connections () {
651+ std::vector<std::thread> threads_to_join;
652+
665653 // Cancel any uploads and downloads
666654 for (auto & [id, request_and_thread] : _active_uploads) {
655+ if (request_and_thread.second .joinable ())
656+ threads_to_join.push_back (std::move (request_and_thread.second ));
657+
667658 request_and_thread.first .cancel ();
668659
669660 if (request_and_thread.first .on_complete )
@@ -720,6 +711,12 @@ void OnionRequestRouter::_close_connections() {
720711 _path_rotation_schedule.clear ();
721712 _pending_rotation_paths.clear ();
722713 _update_status ();
714+
715+ // Block until upload threads have finished
716+ for (auto & t : threads_to_join)
717+ if (t.joinable ())
718+ t.join ();
719+
723720 log::info (cat, " Closed all connections." );
724721}
725722
0 commit comments