Skip to content

Commit 1169502

Browse files
committed
Switch Win32 pipes to PIPE_WAIT
1 parent b2a1fad commit 1169502

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

src/event/event_windows.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,9 @@ _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.
280283
BOOL bSuccess = ReadFile(hPipe, cBuffer, /* nNumberOfBytesToRead */ 0,
281284
&dwNumberOfBytesTransferred, &ov);
282285
DWORD dwBytesAvailable;

src/io.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,20 +1437,20 @@ _dispatch_fd_entry_create_with_fd(dispatch_fd_t fd, uintptr_t hash)
14371437
int result = ioctlsocket((SOCKET)fd, (long)FIONBIO, &value);
14381438
(void)dispatch_assume_zero(result);
14391439
} else {
1440-
// Try to make writing nonblocking, although pipes not coming
1441-
// from Foundation.Pipe may not have FILE_WRITE_ATTRIBUTES.
1440+
// The _dispatch_pipe_monitor_thread expects pipes to be
1441+
// PIPE_WAIT and exploits this assumption by using a blocking
1442+
// 0-byte read as a synchronization mechanism.
14421443
DWORD dwPipeMode = 0;
14431444
if (GetNamedPipeHandleState((HANDLE)fd, &dwPipeMode, NULL,
1444-
NULL, NULL, NULL, 0) && !(dwPipeMode & PIPE_NOWAIT)) {
1445-
dwPipeMode |= PIPE_NOWAIT;
1445+
NULL, NULL, NULL, 0) && !(dwPipeMode & PIPE_WAIT)) {
1446+
dwPipeMode |= PIPE_WAIT;
14461447
if (!SetNamedPipeHandleState((HANDLE)fd, &dwPipeMode,
14471448
NULL, NULL)) {
1448-
// We may end up blocking on subsequent writes, but we
1449-
// don't have a good alternative.
1450-
// The WriteQuotaAvailable from NtQueryInformationFile
1451-
// erroneously returns 0 when there is a blocking read
1452-
// on the other end of the pipe.
1453-
_dispatch_fd_entry_debug("failed to set PIPE_NOWAIT",
1449+
// If setting the pipe to PIPE_WAIT fails, the
1450+
// monitoring thread will spin constantly, saturating
1451+
// a core, which is undesirable but non-fatal.
1452+
// The semantics will still be correct in this case.
1453+
_dispatch_fd_entry_debug("failed to set PIPE_WAIT",
14541454
fd_entry);
14551455
}
14561456
}

0 commit comments

Comments
 (0)