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

Improve support for cancellation #32

Closed
mzimbres opened this issue Oct 2, 2022 · 2 comments
Closed

Improve support for cancellation #32

mzimbres opened this issue Oct 2, 2022 · 2 comments

Comments

@mzimbres
Copy link
Collaborator

mzimbres commented Oct 2, 2022

At the moment the connection classes provide support to cancellation with cancel member function. Although not common or even advisable, some users may want to cancel operations with timeouts like this

co_await (conn.async_run(...) || timer.async_wait(...));

This is working as long as the timeout is not too small e.g. 10ms. To handle that properly Aedis need to improve cancelation support. Below some parts of a conversation I've had with Richard

timer waits and IO reads will be cancellation-aware. But an operation that you have written yourself may not be.

these operations may want to check the cancellation context hasn't already been cancelled before initiating, and also store a cancellation handler in connected cancellation slots in order to cut short any long-standing operation. asio coroutines do this pre-initiation check for you https://github.com/boostorg/asio/blob/0af7858e7b5603a2415a30b69e16c7ef1d47a5e9/include/boost/asio/detail/deadline_timer_service.hpp#L251

also, If you cancel after posting an operation, when the posted code is executed, it may want to check whether the cancellation_state has been cancelled while the posted step was waiting in the executor.

// some code...
asio::post(exec, some_next_step(std::move(my_handler));
// cancellation happens here...
// some_next_step will not know that the handler was cancelled unless it checks.
// ...
// ... some_next_step now executes...
void some_next_step(auto handler)
{
  auto cs = get_canecllation_state(handler);
  if (cs.cancelled() != asio::cancellation_type::none)
  {
    complete(handler, operation_aborted);
    return;
  }
  // ... not cancelled
}
@mzimbres
Copy link
Collaborator Author

mzimbres commented Oct 2, 2022

Motivation for this issue was #28.

@mzimbres
Copy link
Collaborator Author

All forms of cancellation are now supported, for example, implicit with operator || and && or explicit with the cancel member function.

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