Skip to content

Commit

Permalink
rpcdaemon: workaround for Windows build error after PR 1777
Browse files Browse the repository at this point in the history
  • Loading branch information
canepat committed Jan 27, 2024
1 parent 1b28786 commit 68b59fe
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
20 changes: 20 additions & 0 deletions silkworm/rpc/json/stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,21 @@ 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)} {
#else
run_completion_channel_{executor, 1} {
co_spawn(
executor, [](auto self) -> Task<void> {
co_await self->run();
}(this),
boost::asio::detached);
#endif // _WIN32
buffer_.reserve(buffer_capacity_ + buffer_capacity_ / 4); // try to prevent reallocation when buffer overflows
}

Expand All @@ -66,7 +76,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 @@ -305,6 +320,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
11 changes: 8 additions & 3 deletions silkworm/rpc/json/stream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@
#include <stack>
#include <string>
#include <string_view>
#ifdef _WIN32
#include <tuple>
#endif // _WIN32

#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 @@ -89,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 68b59fe

Please sign in to comment.