# c++ – How do I cancel composed asynchronous operations in Asio?

I want to use the composed async operation asio::async_connect, and cancel it, possibly between the individual calls to basic_socket::async_connect which it makes.

In the same manner, I’d like to cancel my own composed async operations (which use async_compose).

I am not asking how to cancel the non-composed member function basic_socket::async_connect, for which I could call basic_socket::cancel.

The free function is composed, so while calling cancel on the socket might stop an ongoing socket::async_connect, it would not stop queued operations.

1. How do I cancel asio::async_connect?

2. In general, how do I cancel composed operations?

3. How do I cancel custom composed operations? (At least with this third option I could hack in some kind of cancellation token system that can check a flag between “building-block” async operations, but I still can’t use existing composed operations in this case).

To summarize the problem, see this diagram from Robert Leahy’s 2019 CppCon talk:

This shows the issue with async_write, a composed operation which also has this problem.

Robert Leahy solves this by reimplementing async_write in a wrapper object which checks a cancellation flag before calling every atomic async_write_some operation on the underlying stream.

Perhaps this is the solution, but it means that I would have to re-implement every built-in composed asio operation to use this kind of similar wrapper object.