Skip to content

Commit

Permalink
Don't endlessly wait on writer coroutine on disconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabteab committed Nov 28, 2024
1 parent 0479a59 commit 64d55df
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion lib/remote/jsonrpcconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,17 @@ void JsonRpcConnection::Disconnect()
IoEngine::SpawnCoroutine(m_IoStrand, [this, keepAlive](asio::yield_context yc) {
m_OutgoingMessagesQueued.Set();

m_WriterDone.Wait(yc);
{
asio::deadline_timer writerTimeout(m_IoStrand.context(), boost::posix_time::seconds(5));
writerTimeout.async_wait(asio::bind_executor(m_IoStrand, [this](boost::system::error_code ec) {
if (!ec) {
m_WriterDone.Set();
}
}));

m_WriterDone.Wait(yc);
// We don't need to explicitly cancel the timer here; its destructor will handle it for us.
}

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

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

Timeout::Ptr shutdownTimeout (new Timeout(
Expand Down

0 comments on commit 64d55df

Please sign in to comment.