Boost logo

Boost Users :

Subject: Re: [Boost-users] asio: cancelling a named pipe client
From: Gavin Lambert (gavinl_at_[hidden])
Date: 2018-01-30 03:27:46


On 27/01/2018 06:50, Stian Zeljko Vrba wrote:
> Am I correct in assuming that cancel() is an apparent noop in this case
> because of the race-condition where an I/O request completes
> successfully before cancel is invoked?

Most likely yes. It just internally calls through to the OS API, which
will have nothing to do if there isn't an outstanding OS request at that
exact moment.

ASIO can't internally consider this a permanent failure because there
may be cases where you wanted to cancel a single operation and then
start a new one that you expect to continue normally.

> If so, can you suggest a more elegant way  (i.e., a way that doesn't
> induce a hard error) of exiting a loop as described here? Setting a
> member variable instead of calling cancel?

Probably the best thing to do is to do both, in this order:

   1. Set a member variable that tells your completion handler code to
not start a new operation.
   2. Call cancel() to abort any pending operation.

This covers both cases; if you miss the pending operation then the
member will tell your completion handler to not start a new one and just
return, and if you don't then the cancellation will generate an
operation_aborted which you can either silently ignore and return
immediately or fall through to the code that checks the member.

There's still a race between when you check the member and when the
operation actually starts -- but that's why you need to post your
cancellation request to the same io_service (and use explicit strands if
you have more than one worker thread).

Omitting the cancel isn't recommended as this would prolong shutdown in
the case that the remote end isn't constantly transmitting.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net