Clearing O_NONBLOCK from a pipe may lose data

Lasse Collin
Wed Feb 18 22:41:00 GMT 2015

(Please Cc me when replying, I'm not subscribed to the list.)


I suspect that there is a bug in Cygwin:

1. Create a pipe with both ends in blocking mode (O_NONBLOCK
   is not set).
2. The writer sets its end to non-blocking mode.
3. The writer writes to the pipe.
4. The writer restores its end of the pipe to blocking mode
   before the reader has read anything from the pipe.
5. The writer closes its end of the pipe.
6. The reader reads from the pipe in blocking mode. The last
   bytes written by the writer never appear at the reader,
   thus data is silently lost.

Omitting the step 4 above makes the problem go away.

A small test case is attached. I have tested it under Cygwin64 that was
installed a few days ago on Windows 7. Usage:

    gcc -std=gnu99 -Wall -Wextra writer.c -o writer
    gcc -std=gnu99 -Wall -Wextra reader.c -o reader
    ./writer | ./reader

The reader only gets the first 65536 bytes. The remaining 5555 bytes
are never seen by the reader even though all write() calls succeed at
the writer.

I found this problem because I had heard a rumor that there is something
wrong with xz 5.2.0 on Cygwin. Since the file I/O code had been changed
in 5.2.0, I investigated that first. There also seems to be something
wrong with xz 5.2.0 in threaded mode under Cygwin which needs more
investigation (random pthread_join() failures cause assertion failures
if xz was built with --enable-debug).

Lasse Collin  |  IRC: Larhzu @ IRCnet & Freenode
-------------- next part --------------
A non-text attachment was scrubbed...
Name: reader.c
Type: text/x-c++src
Size: 844 bytes
Desc: not available
URL: <>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: writer.c
Type: text/x-c++src
Size: 1544 bytes
Desc: not available
URL: <>
-------------- next part --------------
Problem reports:
Unsubscribe info:

More information about the Cygwin mailing list