From 703fb237499a57c05cb216773145211dd1d6ecb4 Mon Sep 17 00:00:00 2001 From: Christopher Dykes Date: Tue, 9 Aug 2016 14:57:19 -0700 Subject: [PATCH] Support using fcntl to mark pipes as non-blocking Summary: Because the comment was a lie; sockets are blocking by default, not non-blocking. Reviewed By: yfeldblum Differential Revision: D3691145 fbshipit-source-id: 5d3c62b3573205fe416d77fe4b5b9fbd593ffd93 --- folly/portability/Fcntl.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/folly/portability/Fcntl.cpp b/folly/portability/Fcntl.cpp index e02a767c..015f4a6d 100755 --- a/folly/portability/Fcntl.cpp +++ b/folly/portability/Fcntl.cpp @@ -60,8 +60,7 @@ int fcntl(int fd, int cmd, ...) { case F_SETFL: { int flags = va_arg(args, int); if (flags & O_NONBLOCK) { - // If it's not a socket, it's probably a pipe, and - // those are non-blocking by default with Windows. + // If it's not a socket, it's probably a pipe. if (folly::portability::sockets::is_fh_socket(fd)) { SOCKET s = (SOCKET)_get_osfhandle(fd); if (s != INVALID_SOCKET) { @@ -69,7 +68,13 @@ int fcntl(int fd, int cmd, ...) { res = ioctlsocket(s, FIONBIO, &nonBlockingEnabled); } } else { - res = 0; + HANDLE p = (HANDLE)_get_osfhandle(fd); + if (GetFileType(p) == FILE_TYPE_PIPE) { + DWORD newMode = PIPE_READMODE_BYTE | PIPE_NOWAIT; + if (SetNamedPipeHandleState(p, &newMode, nullptr, nullptr)) { + res = 0; + } + } } } break; -- 2.34.1