From 5519f9b74f5501c8c401e13033367c20c258300f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Henrik=20Grubbstr=C3=B6m=20=28Grubba=29?=
 <grubba@grubba.org>
Date: Fri, 10 Jan 2020 11:34:17 +0100
Subject: [PATCH] I/O [NT]: Avoid blocking on read from pipes with data.

Do not attempt to read more data from pipes than is already buffered.

Potential fix for [EP-1611] (where a read from a pipe was stalled
until the other end of the pipe terminated).
---
 src/fdlib.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)

diff --git a/src/fdlib.c b/src/fdlib.c
index 825f3a2eea..6a7de01179 100644
--- a/src/fdlib.c
+++ b/src/fdlib.c
@@ -2088,9 +2088,26 @@ PMOD_EXPORT ptrdiff_t debug_fd_read(FD fd, void *to, ptrdiff_t len)
       FDDEBUG(fprintf(stderr,"Read on %d returned %ld\n",fd,rret));
       return rret;
 
+    case FD_PIPE:
+      if (len) {
+	DWORD available_bytes = 0;
+	if (PeekNamedFile(handle, NULL, 0, NULL, &available_bytes, NULL)) {
+	  if (available_bytes) {
+	    if (available_bytes < len) {
+	      len = available_bytes;
+	    }
+	  } else {
+	    /* Wait for some data, but avoid waiting for the entire
+	     * buffer to fill. Higher level code is responsible to
+	     * reschedule the read to get the rest (if any).
+	     */
+	    len = 1;
+	  }
+	}
+      }
+      /* FALLTHRU */
     case FD_CONSOLE:
     case FD_FILE:
-    case FD_PIPE:
       ret=0;
       if(len && !ReadFile(handle, to,
 			  DO_NOT_WARN((DWORD)len),
-- 
GitLab