Skip to content

Commit 64d55df

Browse files
committed
Don't endlessly wait on writer coroutine on disconnect
1 parent 0479a59 commit 64d55df

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

lib/remote/jsonrpcconnection.cpp

+13-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,17 @@ void JsonRpcConnection::Disconnect()
213213
IoEngine::SpawnCoroutine(m_IoStrand, [this, keepAlive](asio::yield_context yc) {
214214
m_OutgoingMessagesQueued.Set();
215215

216-
m_WriterDone.Wait(yc);
216+
{
217+
asio::deadline_timer writerTimeout(m_IoStrand.context(), boost::posix_time::seconds(5));
218+
writerTimeout.async_wait(asio::bind_executor(m_IoStrand, [this](boost::system::error_code ec) {
219+
if (!ec) {
220+
m_WriterDone.Set();
221+
}
222+
}));
223+
224+
m_WriterDone.Wait(yc);
225+
// We don't need to explicitly cancel the timer here; its destructor will handle it for us.
226+
}
217227

218228
/*
219229
* Do not swallow exceptions in a coroutine.
@@ -228,6 +238,8 @@ void JsonRpcConnection::Disconnect()
228238
m_CheckLivenessTimer.cancel();
229239
m_HeartbeatTimer.cancel();
230240

241+
// In case the writer coroutine is not done yet which might got stuck somewhere in async_write
242+
// or async_flush, cancel all operations on the underlying socket to unblock it.
231243
m_Stream->lowest_layer().cancel(ec);
232244

233245
Timeout::Ptr shutdownTimeout (new Timeout(

0 commit comments

Comments
 (0)