Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closing the concurrent channel does not cancel outstanding async operations. Cancelling produces wrong error code #1575

Open
dragon-dreamer opened this issue Dec 22, 2024 · 0 comments

Comments

@dragon-dreamer
Copy link

dragon-dreamer commented Dec 22, 2024

Here is an example code which demonstrates the issue (boost 1.87, VS2022, Win10):

#include <boost/asio/thread_pool.hpp>
#include <boost/asio/awaitable.hpp>
#include <boost/asio/use_awaitable.hpp>
#include <boost/asio/co_spawn.hpp>
#include <boost/asio/detached.hpp>
#include <boost/asio/this_coro.hpp>
#include <boost/asio/as_tuple.hpp>
#include <boost/asio/experimental/awaitable_operators.hpp>
#include <boost/asio/experimental/concurrent_channel.hpp>

#include <syncstream>
#include <iostream>

boost::asio::awaitable<void> send_to_channel(
	boost::asio::experimental::concurrent_channel<void()>& ch)
{
	co_await boost::asio::this_coro::throw_if_cancelled(false);

	std::osyncstream(std::cerr) << "Sending to channel...\n";
	auto [ec] = co_await ch.async_send(boost::asio::as_tuple(boost::asio::use_awaitable));
	std::osyncstream(std::cerr) << "Error " << ec << "\n";
}

int main()
{
	boost::asio::thread_pool ctx{ 20 };

	boost::asio::experimental::concurrent_channel<void()> ch(ctx);

	using namespace boost::asio::experimental::awaitable_operators;
	for (int i = 0; i < 50; ++i)
	{
		boost::asio::co_spawn(ctx, send_to_channel(ch),
			boost::asio::detached);
	}

	std::this_thread::sleep_for(std::chrono::seconds(1));
	std::cerr << "Closing channel!\n";

        // I would expect this call to cancel all outstanding async
        // operations, but this does not happen.
	ch.close();

	ctx.join();
}

The program just hangs, and the last line in the output I see is "Closing channel!".

If I call ch.cancel() after close(), this does cancel the pending operations, however, the error code I get is 0 (no error), which is also unexpected. According to the manual, it should be boost::asio::experimental::error::channel_cancelled instead.

@dragon-dreamer dragon-dreamer changed the title Closing the concurrent channel does not cancel outstanding async operations Closing the concurrent channel does not cancel outstanding async operations. Cancelling produces wrong error code Dec 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant