public inbox for cygwin-cvs@sourceware.org help / color / mirror / Atom feed
From: Takashi Yano <tyan0@sourceware.org> To: cygwin-cvs@sourceware.org Subject: [newlib-cygwin] Cygwin: pipe: Handle STATUS_PENDING even for nonblocking mode. Date: Tue, 16 Nov 2021 23:15:28 +0000 (GMT) [thread overview] Message-ID: <20211116231528.C16873858410@sourceware.org> (raw) https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=5badb8aa0a6a1a79504dc0cd4ee40df1f173060c commit 5badb8aa0a6a1a79504dc0cd4ee40df1f173060c Author: Takashi Yano <takashi.yano@nifty.ne.jp> Date: Wed Nov 17 08:13:17 2021 +0900 Cygwin: pipe: Handle STATUS_PENDING even for nonblocking mode. - NtReadFile() and NtWriteFile() seems to return STATUS_PENDING occasionally even in nonblocking mode. This patch adds handling for STATUS_PENDING in nonblocking mode. Addresses: https://cygwin.com/pipermail/cygwin/2021-November/249910.html Diff: --- winsup/cygwin/fhandler_pipe.cc | 81 ++++++++++++++++++++---------------------- winsup/cygwin/release/3.3.3 | 5 +++ 2 files changed, 43 insertions(+), 43 deletions(-) diff --git a/winsup/cygwin/fhandler_pipe.cc b/winsup/cygwin/fhandler_pipe.cc index 9392a28c1..3cbd434b7 100644 --- a/winsup/cygwin/fhandler_pipe.cc +++ b/winsup/cygwin/fhandler_pipe.cc @@ -279,13 +279,12 @@ fhandler_pipe::raw_read (void *ptr, size_t& len) size_t nbytes = 0; NTSTATUS status = STATUS_SUCCESS; IO_STATUS_BLOCK io; - HANDLE evt = NULL; + HANDLE evt; if (!len) return; - /* Create a wait event if we're in blocking mode. */ - if (!is_nonblocking () && !(evt = CreateEvent (NULL, false, false, NULL))) + if (!(evt = CreateEvent (NULL, false, false, NULL))) { __seterrno (); len = (size_t) -1; @@ -321,8 +320,7 @@ fhandler_pipe::raw_read (void *ptr, size_t& len) ULONG len1 = (ULONG) (len - nbytes); waitret = WAIT_OBJECT_0; - if (evt) - ResetEvent (evt); + ResetEvent (evt); FILE_PIPE_LOCAL_INFORMATION fpli; status = NtQueryInformationFile (get_handle (), &io, &fpli, sizeof (fpli), @@ -336,7 +334,7 @@ fhandler_pipe::raw_read (void *ptr, size_t& len) break; status = NtReadFile (get_handle (), evt, NULL, NULL, &io, ptr, len1, NULL, NULL); - if (evt && status == STATUS_PENDING) + if (status == STATUS_PENDING) { waitret = cygwait (evt, INFINITE, cw_cancel | cw_sig); /* If io.Status is STATUS_CANCELLED after CancelIo, IO has actually @@ -406,8 +404,7 @@ fhandler_pipe::raw_read (void *ptr, size_t& len) break; } ReleaseMutex (read_mtx); - if (evt) - CloseHandle (evt); + CloseHandle (evt); if (status == STATUS_THREAD_SIGNALED && nbytes == 0) { set_errno (EINTR); @@ -437,7 +434,7 @@ fhandler_pipe_fifo::raw_write (const void *ptr, size_t len) ULONG chunk; NTSTATUS status = STATUS_SUCCESS; IO_STATUS_BLOCK io; - HANDLE evt = NULL; + HANDLE evt; if (!len) return 0; @@ -456,8 +453,7 @@ fhandler_pipe_fifo::raw_write (const void *ptr, size_t len) else chunk = pipe_buf_size; - /* Create a wait event if the pipe or fifo is in blocking mode. */ - if (!is_nonblocking () && !(evt = CreateEvent (NULL, false, false, NULL))) + if (!(evt = CreateEvent (NULL, false, false, NULL))) { __seterrno (); return -1; @@ -502,41 +498,41 @@ fhandler_pipe_fifo::raw_write (const void *ptr, size_t len) { status = NtWriteFile (get_handle (), evt, NULL, NULL, &io, (PVOID) ptr, len1, NULL, NULL); - if (evt || !NT_SUCCESS (status) || io.Information > 0 - || len <= PIPE_BUF) - break; - len1 >>= 1; - } - if (evt && status == STATUS_PENDING) - { - while (WAIT_TIMEOUT == - (waitret = cygwait (evt, (DWORD) 0, cw_cancel | cw_sig))) + if (status == STATUS_PENDING) { - if (reader_closed ()) + while (WAIT_TIMEOUT == + (waitret = cygwait (evt, (DWORD) 0, cw_cancel | cw_sig))) { - CancelIo (get_handle ()); - set_errno (EPIPE); - raise (SIGPIPE); - goto out; + if (reader_closed ()) + { + CancelIo (get_handle ()); + set_errno (EPIPE); + raise (SIGPIPE); + goto out; + } + else + cygwait (select_sem, 10); } + /* 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_CANCELED) + status = STATUS_THREAD_CANCELED; + else if (waitret == WAIT_SIGNALED) + status = STATUS_THREAD_SIGNALED; else - cygwait (select_sem, 10); + status = io.Status; } - /* 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_CANCELED) - status = STATUS_THREAD_CANCELED; - else if (waitret == WAIT_SIGNALED) - status = STATUS_THREAD_SIGNALED; - else - status = io.Status; + if (!is_nonblocking () || !NT_SUCCESS (status) || io.Information > 0 + || len <= PIPE_BUF) + break; + len1 >>= 1; } if (isclosed ()) /* A signal handler might have closed the fd. */ { @@ -570,8 +566,7 @@ fhandler_pipe_fifo::raw_write (const void *ptr, size_t len) break; } out: - if (evt) - CloseHandle (evt); + CloseHandle (evt); if (status == STATUS_THREAD_SIGNALED && nbytes == 0) set_errno (EINTR); else if (status == STATUS_THREAD_CANCELED) diff --git a/winsup/cygwin/release/3.3.3 b/winsup/cygwin/release/3.3.3 index e8404be27..7248302a3 100644 --- a/winsup/cygwin/release/3.3.3 +++ b/winsup/cygwin/release/3.3.3 @@ -22,3 +22,8 @@ Bug Fixes - Fix long-standing problem that fchmod or facl on newly created files screw up the DOS file attributes. Addresses: https://cygwin.com/pipermail/cygwin/2021-November/249909.html + +- Fix issue that pipe read()/write() occasionally returns a garbage + length when NtReadFile/NtWriteFile returns STATUS_PENDING in non- + blocking mode. + Addresses: https://cygwin.com/pipermail/cygwin/2021-November/249910.html
reply other threads:[~2021-11-16 23:15 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20211116231528.C16873858410@sourceware.org \ --to=tyan0@sourceware.org \ --cc=cygwin-cvs@sourceware.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).