diff --git a/libi2pd/Destination.cpp b/libi2pd/Destination.cpp index 2dee646c..74a97f58 100644 --- a/libi2pd/Destination.cpp +++ b/libi2pd/Destination.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include "Crypto.h" #include "ECIESX25519AEADRatchetSession.h" @@ -1680,7 +1681,20 @@ namespace client { if (IsRunning ()) { - ClientDestination::Stop (); + // the destination's own thread may still be handling packets of this + // very destination, so tear it down there rather than under the caller + if (GetIOService ().get_executor ().running_in_this_thread ()) + ClientDestination::Stop (); + else + { + std::promise done; + boost::asio::post (GetIOService (), [this, &done]() + { + ClientDestination::Stop (); + done.set_value (); + }); + done.get_future ().wait (); + } StopIOService (); } }