Skip to content

Commit

Permalink
rpcdaemon: workaround for Windows build error after PR 1777 (#1781)
Browse files Browse the repository at this point in the history
  • Loading branch information
canepat authored Jan 28, 2024
1 parent 25754a5 commit d97dac5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
28 changes: 26 additions & 2 deletions silkworm/rpc/json/stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
#include <boost/asio/co_spawn.hpp>
#include <boost/asio/compose.hpp>
#include <boost/asio/detached.hpp>
#ifndef _WIN32 // Workaround for Windows build error due to bug https://github.com/chriskohlhoff/asio/issues/1281
#include <boost/asio/experimental/use_promise.hpp>
#endif // _WIN32

#include <silkworm/infra/common/log.hpp>

Expand Down Expand Up @@ -52,12 +54,23 @@ Stream::Stream(boost::asio::any_io_executor& executor, StreamWriter& writer, std
: writer_(writer),
buffer_capacity_(buffer_capacity),
channel_{executor, kChannelCapacity},
// Workaround for Windows build error due to bug https://github.com/chriskohlhoff/asio/issues/1281
#ifndef _WIN32
run_completion_promise_{co_spawn(
executor, [](auto self) -> Task<void> {
co_await self->run();
}(this),
boost::asio::experimental::use_promise)} {
buffer_.reserve(buffer_capacity_ + buffer_capacity_ / 4); // try to prevent reallocation when buffer overflows
#else
run_completion_channel_{executor, 1} {
co_spawn(
executor, [](auto self) -> Task<void> {
co_await self->run();
}(this),
boost::asio::detached);
#endif // _WIN32
// Try to prevent reallocation when buffer overflows
buffer_.reserve(buffer_capacity_ + buffer_capacity_ / 4);
}

Task<void> Stream::close() {
Expand All @@ -66,7 +79,12 @@ Task<void> Stream::close() {
}
co_await do_async_write(nullptr);

// Workaround for Windows build error due to bug https://github.com/chriskohlhoff/asio/issues/1281
#ifndef _WIN32
co_await run_completion_promise_(boost::asio::use_awaitable);
#else
co_await run_completion_channel_.async_receive(boost::asio::use_awaitable);
#endif // _WIN32

co_await writer_.close();
}
Expand Down Expand Up @@ -252,7 +270,7 @@ void Stream::ensure_separator() {
}

void Stream::do_write(ChunkPtr chunk) {
// Stream write API will usually be called by worker threads rather than I/O contexts, but we handle both
// Stream write API will usually be called by worker threads rather than I/O contexts, but we must handle both
const auto& channel_executor{channel_.get_executor()};
if (channel_executor.target<boost::asio::io_context::executor_type>()->running_in_this_thread()) [[unlikely]] {
// Delegate any back pressure to do_async_write
Expand All @@ -270,6 +288,7 @@ void Stream::do_write(ChunkPtr chunk) {
}

Task<void> Stream::do_async_write(ChunkPtr chunk) {
// TODO(canepat) handle back pressure
try {
co_await channel_.async_send(boost::system::error_code(), chunk, boost::asio::use_awaitable);
} catch (const boost::system::system_error& se) {
Expand Down Expand Up @@ -305,6 +324,11 @@ Task<void> Stream::run() {

channel_.close();

// Workaround for Windows build error due to bug https://github.com/chriskohlhoff/asio/issues/1281
#ifdef _WIN32
co_await run_completion_channel_.async_send(boost::system::error_code(), 0, boost::asio::use_awaitable);
#endif // _WIN32

SILK_TRACE << "Stream::run total_writes: " << total_writes << " total_bytes_sent: " << total_bytes_sent;
}

Expand Down
8 changes: 8 additions & 0 deletions silkworm/rpc/json/stream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
#include <silkworm/infra/concurrency/task.hpp>

#include <boost/asio/experimental/concurrent_channel.hpp>
#ifndef _WIN32 // Workaround for Windows build error due to bug https://github.com/chriskohlhoff/asio/issues/1281
#include <boost/asio/experimental/promise.hpp>
#endif // _WIN32
#include <boost/asio/io_context.hpp>
#include <nlohmann/json.hpp>

Expand Down Expand Up @@ -86,8 +88,14 @@ class Stream {
using ChunkChannel = boost::asio::experimental::concurrent_channel<void(boost::system::error_code, ChunkPtr)>;
ChunkChannel channel_; // Chunks enqueued waiting to be written asynchronously

// Workaround for Windows build error due to bug https://github.com/chriskohlhoff/asio/issues/1281
#ifndef _WIN32
using RunPromise = boost::asio::experimental::promise<void(std::exception_ptr)>;
RunPromise run_completion_promise_; // Rendez-vous for run loop completion
#else
using SyncChannel = boost::asio::experimental::concurrent_channel<void(boost::system::error_code, int)>;
SyncChannel run_completion_channel_; // Rendez-vous for run loop completion
#endif // _WIN32
};

} // namespace silkworm::rpc::json

0 comments on commit d97dac5

Please sign in to comment.