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: Sat, 6 Nov 2021 15:10:47 +0900	[thread overview]
Message-ID: <20211106151047.4d8f626bd6ebe9e4d8017f3b@nifty.ne.jp> (raw)
In-Reply-To: <2cfa5de7-3b95-9062-4572-f36d304bc916@cornell.edu>

# This topic is moved from cygwin@cygwin.com mailing list.

Hi Ken,

On Fri, 5 Nov 2021 16:08:31 -0400
Ken Brown wrote:
> On 11/5/2021 3:41 PM, Takashi Yano via Cygwin wrote:
> > Thanks much for the detailed steps. I could reproduce the problem.
> > 
> > It seems that the cause is the overhaul for the pipe implementation.
> > I also found the workaround for this issue. Please try:
> > export CYGWIN=pipe_byte
> > 
> > Corinna, Ken,
> > What about setting pipe mode to byte by default?
> 
> I have a vague recollection that this caused some other problem, but I'll have 
> to review the email discussions from a few months ago.  I'm traveling at the 
> moment and won't get to this for a few days.
> 
> In the meantime, could you explain (probably on cygwin-developers) why message 
> mode causes the reported issue?  Also, does the problem occur only when there 
> are non-Cygwin apps involved?

I digged deeper this problem and might find the cause.

Perhaps, C# program sometimes writes 0 byte to stdout.
As a result, if stdout is a message pipe, raw_read() returns
0 byte and EOF is falsely detected. So, setting pipe type
to byte solves the issue.

I used the following two test programs. The output of
second test program (do_pipe) is like:
res=1
len=0          <<<<< Look here!!
res=1
len=14
res=0
len=0


/* C# program which is supposed to write 0 byte msg. */
/* Filename: produce.cs */
/* Compile this with "csc produce.cs" */
using System;
namespace Produce
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World.");
        }
    }
}
/* End of produce.cs */


/* Filename: do_pipe.c */
/* Compile this with "x86_64-w64-mingw32-gcc do_pipe.c -o do_pipe" */
#include <windows.h>
#include <stdio.h>

#if 0
#define PTYPE PIPE_TYPE_BYTE
#else
#define PTYPE PIPE_TYPE_MESSAGE
#endif

int main()
{
	HANDLE hr, hw;
	STARTUPINFO si;
	PROCESS_INFORMATION pi;
	char buf[1024];
	BOOL res;

	SECURITY_ATTRIBUTES sec_none = {sizeof(SECURITY_ATTRIBUTES), NULL, TRUE};
	hr = CreateNamedPipe("\\\\.\\pipe\\pipe-test1",
			PIPE_ACCESS_INBOUND, PTYPE,
			1, 4096, 4096, NMPWAIT_USE_DEFAULT_WAIT, &sec_none);
	hw = CreateFile("\\\\.\\pipe\\pipe-test1",
			GENERIC_WRITE | FILE_READ_ATTRIBUTES, 0, &sec_none,
			OPEN_EXISTING, 0, 0);

	ZeroMemory(&si, sizeof(si));
	si.cb = sizeof(si);
	si.dwFlags = STARTF_USESTDHANDLES;
	si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
	si.hStdOutput = hw;
	si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
	CreateProcess(NULL, "produce", NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);

	CloseHandle(hw);

	do {
		DWORD len;
		res = ReadFile(hr, buf, sizeof(buf), &len, NULL); 
		printf("res=%d\n", res);
		printf("len=%d\n", len);
	} while (res);

	CloseHandle(hr);

	WaitForSingleObject(pi.hProcess, INFINITE);
	return 0;
}
/* End of do_pipe.c */


Therefore, the following patch instead of setting pipe type to byte
also resolves the issue.

diff --git a/winsup/cygwin/fhandler_pipe.cc b/winsup/cygwin/fhandler_pipe.cc
index 43771e8f7..bc06d157c 100644
--- a/winsup/cygwin/fhandler_pipe.cc
+++ b/winsup/cygwin/fhandler_pipe.cc
@@ -393,7 +393,8 @@ fhandler_pipe::raw_read (void *ptr, size_t& len)
 	    }
 	}
 
-      if (nbytes_now == 0 || status == STATUS_BUFFER_OVERFLOW)
+      if ((nbytes_now == 0 && !NT_SUCCESS (status))
+	  || status == STATUS_BUFFER_OVERFLOW)
 	break;
     }
   ReleaseMutex (read_mtx);

I am not sure which is the better solution.


P.S.
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.

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

       reply	other threads:[~2021-11-06  6:11 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 [this message]
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
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=20211106151047.4d8f626bd6ebe9e4d8017f3b@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).