public inbox for cygwin-developers@cygwin.com
 help / color / mirror / Atom feed
From: Takashi Yano <takashi.yano@nifty.ne.jp>
To: cygwin-developers@cygwin.com
Subject: Re: 3.3.0: Possible regression in cygwin DLL (Win10); fixed in snapshot
Date: Wed, 10 Nov 2021 17:30:03 +0900	[thread overview]
Message-ID: <20211110173003.88359e8482ffa8b8be326903@nifty.ne.jp> (raw)
In-Reply-To: <YYZqNiBRTfYWwFev@calimero.vinschen.de>

[-- Attachment #1: Type: text/plain, Size: 1500 bytes --]

Hi Corinna,

On Sat, 6 Nov 2021 12:42:46 +0100
Corinna Vinschen wrote:
> On Nov  6 15:10, Takashi Yano wrote:
> > Unfortunately, these solutions do not resolve the issue
> > which is another issue with C# program:
> > https://cygwin.com/pipermail/cygwin/2021-March/247987.html
> > This still needs FILE_SYNCHRONOUS_IO_NONALERT flag.
> 
> If we want to add FILE_SYNCHRONOUS_IO_NONALERT, this would have to be
> solved by running NtReadFile/NtWriteFile synchronously in a thread,
> started on every invocation of raw_read/raw_write.  raw_read/raw_write
> would then call cygwait on the thread object.  To break on signal or
> thread cancallation events, it would have to call CancelSynchronousIo.
> That's certainly doable.

I tried to implement your idea, however, I noticed that
NtQueryObject(ObjectNameInformation) call in
get_query_hdl_per_process() is blocked while reading the
pipe if FIPE_SYNCHRONOUS_IO_NONALERT is set and pipe is
in blocking mode.

So I would like to propose alternative implementation with
FILE_SYNCHRONOUS_IO_NONALERT being set. Please have a look
at attached patch. With this patch, pipe itself in read side
is always set to nonblocking mode and simulate the blocking
behaviour in raw_read(). This can eliminate creating thread
for reading as well as calling CancelSynchronousIo().

Note that setting FILE_SYNCHRONOUS_IO_NONALERT only for read
pipe seems to be enough for C# programs.

What do you think of this implementation?

-- 
Takashi Yano <takashi.yano@nifty.ne.jp>

[-- Attachment #2: pipe20211110.patch --]
[-- Type: application/octet-stream, Size: 5801 bytes --]

diff --git a/winsup/cygwin/fhandler_pipe.cc b/winsup/cygwin/fhandler_pipe.cc
index 13731437e..41121394e 100644
--- a/winsup/cygwin/fhandler_pipe.cc
+++ b/winsup/cygwin/fhandler_pipe.cc
@@ -90,7 +90,10 @@ fhandler_pipe::init (HANDLE f, DWORD a, mode_t mode, int64_t uniq_id)
   set_ino (uniq_id);
   set_unique_id (uniq_id | !!(mode & GENERIC_WRITE));
   if (opened_properly)
-    set_pipe_non_blocking (is_nonblocking ());
+    /* Set read pipe always nonblocking to allow signal handling
+       even with FILE_SYNCHRONOUS_IO_NONALERT. */
+    set_pipe_non_blocking (get_device () == FH_PIPER ?
+			   true : is_nonblocking ());
   return 1;
 }
 
@@ -264,9 +267,9 @@ fhandler_pipe::release_select_sem (const char *from)
   if (get_dev () == FH_PIPER) /* Number of select() and writer */
     n_release = get_obj_handle_count (select_sem)
       - get_obj_handle_count (read_mtx);
-  else /* Number of select() call */
+  else /* Number of select() call and reader */
     n_release = get_obj_handle_count (select_sem)
-      - get_obj_handle_count (hdl_cnt_mtx);
+      - get_obj_handle_count (get_handle ());
   debug_printf("%s(%s) release %d", from,
 	       get_dev () == FH_PIPER ? "PIPER" : "PIPEW", n_release);
   if (n_release)
@@ -279,19 +282,10 @@ fhandler_pipe::raw_read (void *ptr, size_t& len)
   size_t nbytes = 0;
   NTSTATUS status = STATUS_SUCCESS;
   IO_STATUS_BLOCK io;
-  HANDLE evt = NULL;
 
   if (!len)
     return;
 
-  /* Create a wait event if we're in blocking mode. */
-  if (!is_nonblocking () && !(evt = CreateEvent (NULL, false, false, NULL)))
-    {
-      __seterrno ();
-      len = (size_t) -1;
-      return;
-    }
-
   DWORD timeout = is_nonblocking () ? 0 : INFINITE;
   DWORD waitret = cygwait (read_mtx, timeout);
   switch (waitret)
@@ -319,48 +313,23 @@ fhandler_pipe::raw_read (void *ptr, size_t& len)
     {
       ULONG_PTR nbytes_now = 0;
       ULONG len1 = (ULONG) (len - nbytes);
-      waitret = WAIT_OBJECT_0;
 
-      if (evt)
-	ResetEvent (evt);
       FILE_PIPE_LOCAL_INFORMATION fpli;
       status = NtQueryInformationFile (get_handle (), &io,
 				       &fpli, sizeof (fpli),
 				       FilePipeLocalInformation);
       if (NT_SUCCESS (status))
 	{
-	if (fpli.ReadDataAvailable == 0 && nbytes != 0)
-	  break;
+	  if (fpli.ReadDataAvailable == 0 && nbytes != 0)
+	    break;
 	}
       else if (nbytes != 0)
 	break;
-      status = NtReadFile (get_handle (), evt, NULL, NULL, &io, ptr,
+      status = NtReadFile (get_handle (), NULL, NULL, NULL, &io, ptr,
 			   len1, NULL, NULL);
-      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_CANCELED)
-	    status = STATUS_THREAD_CANCELED;
-	  else if (waitret == WAIT_SIGNALED)
-	    status = STATUS_THREAD_SIGNALED;
-	  else
-	    status = io.Status;
-	}
       if (isclosed ())  /* A signal handler might have closed the fd. */
 	{
-	  if (waitret == WAIT_OBJECT_0)
-	    set_errno (EBADF);
-	  else
-	    __seterrno ();
+	  set_errno (EBADF);
 	  nbytes = (size_t) -1;
 	}
       else if (NT_SUCCESS (status)
@@ -393,7 +362,16 @@ fhandler_pipe::raw_read (void *ptr, size_t& len)
 		  nbytes = (size_t) -1;
 		  break;
 		}
-	      fallthrough;
+	      waitret = cygwait (select_sem, 1);
+	      if (waitret == WAIT_CANCELED)
+		pthread::static_cancel_self ();
+	      else if (waitret == WAIT_SIGNALED)
+		{
+		  set_errno (EINTR);
+		  nbytes = (size_t) -1;
+		  break;
+		}
+	      continue;
 	    default:
 	      __seterrno_from_nt_status (status);
 	      nbytes = (size_t) -1;
@@ -406,8 +384,6 @@ fhandler_pipe::raw_read (void *ptr, size_t& len)
 	break;
     }
   ReleaseMutex (read_mtx);
-  if (evt)
-    CloseHandle (evt);
   if (status == STATUS_THREAD_SIGNALED && nbytes == 0)
     {
       set_errno (EINTR);
@@ -993,9 +969,12 @@ nt_create (LPSECURITY_ATTRIBUTES sa_ptr, HANDLE &r, HANDLE &w,
 				  npfsh, sa_ptr->lpSecurityDescriptor);
 
       timeout.QuadPart = -500000;
+      /* Set FILE_SYNCHRONOUS_IO_NONALERT flag so that native
+	 C# programs work with cygwin pipe. */
       status = NtCreateNamedPipeFile (&r, access, &attr, &io,
 				      FILE_SHARE_READ | FILE_SHARE_WRITE,
-				      FILE_CREATE, 0, pipe_type,
+				      FILE_CREATE,
+				      FILE_SYNCHRONOUS_IO_NONALERT, pipe_type,
 				      FILE_PIPE_BYTE_STREAM_MODE,
 				      0, 1, psize, psize, &timeout);
 
@@ -1107,7 +1086,9 @@ fhandler_pipe::fcntl (int cmd, intptr_t arg)
   const bool was_nonblocking = is_nonblocking ();
   int res = fhandler_base::fcntl (cmd, arg);
   const bool now_nonblocking = is_nonblocking ();
-  if (now_nonblocking != was_nonblocking)
+  /* Do not set blocking mode for read pipe to allow signal handling
+     even with FILE_SYNCHRONOUS_IO_NONALERT. */
+  if (now_nonblocking != was_nonblocking && get_device () != FH_PIPER)
     set_pipe_non_blocking (now_nonblocking);
   return res;
 }
diff --git a/winsup/cygwin/globals.cc b/winsup/cygwin/globals.cc
index 48fb312de..ac5ad0307 100644
--- a/winsup/cygwin/globals.cc
+++ b/winsup/cygwin/globals.cc
@@ -69,7 +69,7 @@ int NO_COPY dynamically_loaded;
 /* Some CYGWIN environment variable variables. */
 bool allow_glob = true;
 bool ignore_case_with_glob;
-bool pipe_byte;
+bool pipe_byte = true; /* Default to byte mode so that C# programs work. */
 bool reset_com;
 bool wincmdln;
 winsym_t allow_winsymlinks = WSYM_default;

  parent reply	other threads:[~2021-11-10  8:30 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CAEv6GOB8PXHgHoz7hdJy6Bia2GWEmUDnTd252gTGinz2vuv=hA@mail.gmail.com>
     [not found] ` <20211105123950.b118a7f2ba38379764df4c12@nifty.ne.jp>
     [not found]   ` <CAEv6GOA-y58YrftXgEgFrjqtOTHmfdu2Vrq76Lwn0suZpZ=U9w@mail.gmail.com>
     [not found]     ` <20211105170542.96ce6dd4ca32880ddfddd660@nifty.ne.jp>
     [not found]       ` <CAEv6GODiM88Xfhk9R3AcEW6UTYSzACzYe4C0gPoTYm=u9ZTqRQ@mail.gmail.com>
     [not found]         ` <20211106044116.698b465a5d8ed6ce2cc75c99@nifty.ne.jp>
     [not found]           ` <2cfa5de7-3b95-9062-4572-f36d304bc916@cornell.edu>
2021-11-06  6:10             ` Takashi Yano
2021-11-06 11:42               ` Corinna Vinschen
2021-11-06 12:02                 ` Corinna Vinschen
2021-11-06 14:13                   ` Takashi Yano
2021-11-06 17:20                     ` Corinna Vinschen
2021-11-07  3:01                       ` Takashi Yano
2021-11-06 16:38                 ` Ken Brown
2021-11-06 17:20                   ` Corinna Vinschen
2021-11-07  3:46                   ` Takashi Yano
2021-11-07 22:20                     ` Ken Brown
2021-11-08  8:23                       ` Takashi Yano
2021-11-08  9:46                         ` Corinna Vinschen
2021-11-10  8:30                 ` Takashi Yano [this message]
2021-11-10 10:34                   ` Corinna Vinschen
2021-11-10 13:30                     ` Takashi Yano
2021-11-10 20:35                       ` Corinna Vinschen
2021-11-10 21:32                         ` Ken Brown
2021-11-11 16:11                           ` Ken Brown
2021-11-12  8:38                             ` Takashi Yano
2021-11-16 23:46                             ` Takashi Yano
2021-11-17  8:10                               ` Takashi Yano
2021-11-17 15:12                                 ` Ken Brown
2021-11-11  9:52                         ` Corinna Vinschen
2021-11-11 11:12                           ` Takashi Yano
2021-11-11 11:33                             ` Corinna Vinschen
2021-11-11 12:02                               ` Takashi Yano
2021-11-11 13:20                                 ` Takashi Yano
2021-11-11 16:07                                   ` Corinna Vinschen
2021-11-12  8:33                             ` Takashi Yano
2021-11-12 10:02                               ` Corinna Vinschen
2021-12-12 13:26                                 ` Takashi Yano
2021-12-12 13:36                                   ` Ken Brown
2021-12-13 11:15                                     ` Takashi Yano

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=20211110173003.88359e8482ffa8b8be326903@nifty.ne.jp \
    --to=takashi.yano@nifty.ne.jp \
    --cc=cygwin-developers@cygwin.com \
    /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: link
Be 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).