From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2155) id ABECD385781B; Tue, 14 Sep 2021 15:06:59 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org ABECD385781B Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Corinna Vinschen To: cygwin-cvs@sourceware.org Subject: [newlib-cygwin] Cygwin: pipes: always terminate async IO in blocking mode X-Act-Checkin: newlib-cygwin X-Git-Author: Corinna Vinschen X-Git-Refname: refs/heads/master X-Git-Oldrev: 477a593693076ce252ee463c4d06a9fa12a00d02 X-Git-Newrev: 4003e3dfa1b9bfee12d686f016353a1e20b51e74 Message-Id: <20210914150659.ABECD385781B@sourceware.org> Date: Tue, 14 Sep 2021 15:06:59 +0000 (GMT) X-BeenThere: cygwin-cvs@cygwin.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Cygwin core component git logs List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 14 Sep 2021 15:06:59 -0000 https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=4003e3dfa1b9bfee12d686f016353a1e20b51e74 commit 4003e3dfa1b9bfee12d686f016353a1e20b51e74 Author: Corinna Vinschen Date: Tue Sep 7 10:50:47 2021 +0200 Cygwin: pipes: always terminate async IO in blocking mode In blocking mode, the underlying IO must always be terminated, one way or the other, to make sure the application knows the exact state after returning from the IO function. Therefore, always call CancelIo in blocking mode. Signed-off-by: Corinna Vinschen Diff: --- winsup/cygwin/fhandler_pipe.cc | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/winsup/cygwin/fhandler_pipe.cc b/winsup/cygwin/fhandler_pipe.cc index 76ce895e2..8041a5d51 100644 --- a/winsup/cygwin/fhandler_pipe.cc +++ b/winsup/cygwin/fhandler_pipe.cc @@ -293,6 +293,14 @@ fhandler_pipe::raw_read (void *ptr, size_t& len) if (evt && status == STATUS_PENDING) { waitret = cygwait (evt, INFINITE, cw_cancel | cw_sig); + /* If io.Status is STATUS_CANCELLED after CancelIo, IO has actually + been cancelled and io.Information contains the number of bytes + processed so far. + Otherwise IO has been finished regulary and io.Status contains + valid success or error information. */ + CancelIo (get_handle ()); + if (waitret == WAIT_SIGNALED && io.Status != STATUS_CANCELLED) + waitret = WAIT_OBJECT_0; if (waitret == WAIT_OBJECT_0) status = io.Status; } @@ -302,7 +310,11 @@ fhandler_pipe::raw_read (void *ptr, size_t& len) break; } else if (waitret == WAIT_SIGNALED) - status = STATUS_THREAD_SIGNALED; + { + status = STATUS_THREAD_SIGNALED; + nbytes += io.Information; + break; + } else if (isclosed ()) /* A signal handler might have closed the fd. */ { if (waitret == WAIT_OBJECT_0) @@ -363,10 +375,7 @@ fhandler_pipe::raw_read (void *ptr, size_t& len) nbytes = (size_t) -1; } else if (status == STATUS_THREAD_CANCELED) - { - CancelIo (get_handle ()); - pthread::static_cancel_self (); - } + pthread::static_cancel_self (); len = nbytes; } @@ -443,6 +452,14 @@ fhandler_pipe_fifo::raw_write (const void *ptr, size_t len) if (evt && status == STATUS_PENDING) { waitret = cygwait (evt, INFINITE, cw_cancel | cw_sig); + /* If io.Status is STATUS_CANCELLED after CancelIo, IO has actually + been cancelled and io.Information contains the number of bytes + processed so far. + Otherwise IO has been finished regulary and io.Status contains + valid success or error information. */ + CancelIo (get_handle ()); + if (waitret == WAIT_SIGNALED && io.Status != STATUS_CANCELLED) + waitret = WAIT_OBJECT_0; if (waitret == WAIT_OBJECT_0) status = io.Status; } @@ -452,7 +469,11 @@ fhandler_pipe_fifo::raw_write (const void *ptr, size_t len) break; } else if (waitret == WAIT_SIGNALED) - status = STATUS_THREAD_SIGNALED; + { + status = STATUS_THREAD_SIGNALED; + nbytes += io.Information; + break; + } else if (isclosed ()) /* A signal handler might have closed the fd. */ { if (waitret == WAIT_OBJECT_0) @@ -485,10 +506,7 @@ fhandler_pipe_fifo::raw_write (const void *ptr, size_t len) if (status == STATUS_THREAD_SIGNALED && nbytes == 0) set_errno (EINTR); else if (status == STATUS_THREAD_CANCELED) - { - CancelIo (get_handle ()); - pthread::static_cancel_self (); - } + pthread::static_cancel_self (); return nbytes ?: -1; }