Fix async handlers running on wrong executor#3075
Fix async handlers running on wrong executor#3075ssam18 wants to merge 1 commit intoboostorg:developfrom
Conversation
When async_base::complete dispatches a handler, it now properly binds the handler to the operation's executor to ensure correct execution context.
|
The fix (also?) doesn't look accurate to me. In particular, you applied it to the branch where the documentation claims the immediate executor should be used, however, your bind overrides it with any associated non-immediate executor. Note that Assuming that you ran into this as a problem in your code, can you share a minimal example where the wrong executor is used? Then we can understand what actually needs to be fixed. I can see two potential causes here:
That is, assuming you have demonstrated a case where the wrong executor is effectively used. |
|
I believe this is related to the issue briefly described in #3047. Asio recommends using asio::async_immediate(self.get_io_executor(), std::move(self));Internally, this leads to a call to template <typename CompletionHandler>
void operator()(CompletionHandler&& handler) const
{
typename associated_immediate_executor<
CompletionHandler, executor_type>::type ex =
(get_associated_immediate_executor)(handler, ex_);
(dispatch)(ex, static_cast<CompletionHandler&&>(handler));
}If the handler has an associated executor, In this context, the handler is the composed operation object ( Our current implementation behaves slightly differently. Instead of passing the composed operation object to auto const ex = this->get_immediate_executor();
net::dispatch(
ex,
net::append(std::move(h_), std::forward<Args>(args)...));Because of this,
|
The async_base::complete function now correctly binds the dispatched handler to the operation's executor, thereby ensuring the appropriate execution context.