Skip to content

Commit 14b096c

Browse files
authored
Merge pull request #883 from kcieplak/main
Fixes: #882 - Revert "Merge pull request #854 PIPE_WAIT"
2 parents fdeda69 + 2609ca7 commit 14b096c

File tree

3 files changed

+17
-52
lines changed

3 files changed

+17
-52
lines changed

src/event/event_windows.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,6 @@ _dispatch_pipe_monitor_thread(void *context)
277277
char cBuffer[1];
278278
DWORD dwNumberOfBytesTransferred;
279279
OVERLAPPED ov = {0};
280-
// Block on a 0-byte read; this will only resume when data is
281-
// available in the pipe. The pipe must be PIPE_WAIT or this thread
282-
// will spin.
283280
BOOL bSuccess = ReadFile(hPipe, cBuffer, /* nNumberOfBytesToRead */ 0,
284281
&dwNumberOfBytesTransferred, &ov);
285282
DWORD dwBytesAvailable;

src/io.c

Lines changed: 16 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,20 +1462,20 @@ _dispatch_fd_entry_create_with_fd(dispatch_fd_t fd, uintptr_t hash)
14621462
int result = ioctlsocket((SOCKET)fd, (long)FIONBIO, &value);
14631463
(void)dispatch_assume_zero(result);
14641464
} else {
1465-
// The _dispatch_pipe_monitor_thread expects pipes to be
1466-
// PIPE_WAIT and exploits this assumption by using a blocking
1467-
// 0-byte read as a synchronization mechanism.
1465+
// Try to make writing nonblocking, although pipes not coming
1466+
// from Foundation.Pipe may not have FILE_WRITE_ATTRIBUTES.
14681467
DWORD dwPipeMode = 0;
14691468
if (GetNamedPipeHandleState((HANDLE)fd, &dwPipeMode, NULL,
1470-
NULL, NULL, NULL, 0) && !(dwPipeMode & PIPE_WAIT)) {
1471-
dwPipeMode |= PIPE_WAIT;
1469+
NULL, NULL, NULL, 0) && !(dwPipeMode & PIPE_NOWAIT)) {
1470+
dwPipeMode |= PIPE_NOWAIT;
14721471
if (!SetNamedPipeHandleState((HANDLE)fd, &dwPipeMode,
14731472
NULL, NULL)) {
1474-
// If setting the pipe to PIPE_WAIT fails, the
1475-
// monitoring thread will spin constantly, saturating
1476-
// a core, which is undesirable but non-fatal.
1477-
// The semantics will still be correct in this case.
1478-
_dispatch_fd_entry_debug("failed to set PIPE_WAIT",
1473+
// We may end up blocking on subsequent writes, but we
1474+
// don't have a good alternative.
1475+
// The WriteQuotaAvailable from NtQueryInformationFile
1476+
// erroneously returns 0 when there is a blocking read
1477+
// on the other end of the pipe.
1478+
_dispatch_fd_entry_debug("failed to set PIPE_NOWAIT",
14791479
fd_entry);
14801480
}
14811481
}
@@ -2550,40 +2550,13 @@ _dispatch_operation_perform(dispatch_operation_t op)
25502550
NTSTATUS status = _dispatch_NtQueryInformationFile(hFile,
25512551
&iosb, &fpli, sizeof(fpli), FilePipeLocalInformation);
25522552
if (NT_SUCCESS(status)) {
2553-
// WriteQuotaAvailable is the free space in the output buffer
2554-
// that has not already been reserved for reading. In other words,
2555-
// WriteQuotaAvailable =
2556-
// OutboundQuota - WriteQuotaUsed - QueuedReadSize.
2557-
// It is not documented that QueuedReadSize is part of this
2558-
// calculation, but this behavior has been observed experimentally.
2559-
// Unfortunately, this means that it is not possible to distinguish
2560-
// between a full output buffer and a reader blocked waiting for a
2561-
// full buffer's worth of data. This is a problem because if the
2562-
// output buffer is full and no reader is waiting for data, then
2563-
// attempting to write to the buffer of a PIPE_WAIT, non-
2564-
// overlapped I/O pipe will block the dispatch queue thread.
2565-
//
2566-
// In order to work around this idiosyncrasy, we bound the size of
2567-
// the write to be OutboundQuota - 1. This affords us a sentinel value
2568-
// in WriteQuotaAvailable that can be used to detect if a reader is
2569-
// making progress or not.
2570-
// WriteQuotaAvailable = 0 => a reader is blocked waiting for data.
2571-
// WriteQuotaAvailable = 1 => the pipe has been written to, but no
2572-
// reader is making progress.
2573-
// When we detect that WriteQuotaAvailable == 1, we write 0 bytes to
2574-
// avoid blocking the dispatch queue thread.
2575-
if (fpli.WriteQuotaAvailable == 0) {
2576-
// This condition can only occur when we have a reader blocked
2577-
// waiting for data on the pipe. In this case, write a full
2578-
// buffer's worth of data (less one byte to preserve this
2579-
// sentinel value of WriteQuotaAvailable == 0).
2580-
len = MIN(len, fpli.OutboundQuota - 1);
2581-
} else {
2582-
// Subtract 1 from WriteQuotaAvailable to ensure we do not fill
2583-
// the pipe and preserve the sentinel value of
2584-
// WriteQuotaAvailable == 1.
2585-
len = MIN(len, fpli.WriteQuotaAvailable - 1);
2553+
// WriteQuotaAvailable is unreliable in the presence
2554+
// of a blocking reader, when it can return zero, so only
2555+
// account for it otherwise
2556+
if (fpli.WriteQuotaAvailable > 0) {
2557+
len = MIN(len, fpli.WriteQuotaAvailable);
25862558
}
2559+
len = MIN(len, fpli.OutboundQuota);
25872560
}
25882561

25892562
OVERLAPPED ovlOverlapped = {};

tests/dispatch_io_pipe.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -408,12 +408,7 @@ test_dispatch_write(int kind, int delay)
408408
dispatch_group_t g = dispatch_group_create();
409409
dispatch_group_enter(g);
410410

411-
// The libdispatch implementation writes at most bufsize-1 bytes
412-
// before requiring a reader to start making progress. Because
413-
// these tests operate serially, the reader will not make progress
414-
// until the write finishes, and a write of >= bufsize will not
415-
// finish until the reader starts draining the pipe.
416-
const size_t bufsize = test_get_pipe_buffer_size(kind) - 1;
411+
const size_t bufsize = test_get_pipe_buffer_size(kind);
417412

418413
char *buf = calloc(bufsize, 1);
419414
assert(buf);

0 commit comments

Comments
 (0)