From d97dac542278f59d6a13959148722a260329ad3d Mon Sep 17 00:00:00 2001 From: canepat <16927169+canepat@users.noreply.github.com> Date: Sun, 28 Jan 2024 12:17:49 +0100 Subject: [PATCH] rpcdaemon: workaround for Windows build error after PR 1777 (#1781) --- silkworm/rpc/json/stream.cpp | 28 ++++++++++++++++++++++++++-- silkworm/rpc/json/stream.hpp | 8 ++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/silkworm/rpc/json/stream.cpp b/silkworm/rpc/json/stream.cpp index e0a11f4bb1..09001e8af9 100644 --- a/silkworm/rpc/json/stream.cpp +++ b/silkworm/rpc/json/stream.cpp @@ -24,7 +24,9 @@ #include #include #include +#ifndef _WIN32 // Workaround for Windows build error due to bug https://github.com/chriskohlhoff/asio/issues/1281 #include +#endif // _WIN32 #include @@ -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 { 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 { + 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 Stream::close() { @@ -66,7 +79,12 @@ Task 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(); } @@ -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()->running_in_this_thread()) [[unlikely]] { // Delegate any back pressure to do_async_write @@ -270,6 +288,7 @@ void Stream::do_write(ChunkPtr chunk) { } Task 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) { @@ -305,6 +324,11 @@ Task 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; } diff --git a/silkworm/rpc/json/stream.hpp b/silkworm/rpc/json/stream.hpp index e4e991ec17..f176a6a170 100644 --- a/silkworm/rpc/json/stream.hpp +++ b/silkworm/rpc/json/stream.hpp @@ -24,7 +24,9 @@ #include #include +#ifndef _WIN32 // Workaround for Windows build error due to bug https://github.com/chriskohlhoff/asio/issues/1281 #include +#endif // _WIN32 #include #include @@ -86,8 +88,14 @@ class Stream { using ChunkChannel = boost::asio::experimental::concurrent_channel; 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; RunPromise run_completion_promise_; // Rendez-vous for run loop completion +#else + using SyncChannel = boost::asio::experimental::concurrent_channel; + SyncChannel run_completion_channel_; // Rendez-vous for run loop completion +#endif // _WIN32 }; } // namespace silkworm::rpc::json