From d36c8b4c55caac75ee2a4cff53fc7eed5a77c1a9 Mon Sep 17 00:00:00 2001 From: reuk Date: Tue, 12 Oct 2021 19:52:42 +0100 Subject: [PATCH] NamedPipe: Avoid early exits from writes on unavailable pipes --- .../juce_core/native/juce_posix_NamedPipe.cpp | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/modules/juce_core/native/juce_posix_NamedPipe.cpp b/modules/juce_core/native/juce_posix_NamedPipe.cpp index 8b14f16d3a..5af501ce6a 100644 --- a/modules/juce_core/native/juce_posix_NamedPipe.cpp +++ b/modules/juce_core/native/juce_posix_NamedPipe.cpp @@ -66,7 +66,9 @@ public: if (numRead <= 0) { - if (errno != EWOULDBLOCK || stopReadOperation.load() || hasExpired (timeoutEnd)) + const auto error = errno; + + if (! (error == EWOULDBLOCK || error == EAGAIN) || stopReadOperation.load() || hasExpired (timeoutEnd)) return -1; const int maxWaitingTime = 30; @@ -97,8 +99,20 @@ public: auto bytesThisTime = numBytesToWrite - bytesWritten; auto numWritten = (int) ::write (pipeOut, sourceBuffer, (size_t) bytesThisTime); - if (numWritten <= 0) - return -1; + if (numWritten < 0) + { + const auto error = errno; + const int maxWaitingTime = 30; + + if (error == EWOULDBLOCK || error == EAGAIN) + waitToWrite (pipeOut, timeoutEnd == 0 ? maxWaitingTime + : jmin (maxWaitingTime, + (int) (timeoutEnd - Time::getMillisecondCounter()))); + else + return -1; + + numWritten = 0; + } bytesWritten += numWritten; sourceBuffer += numWritten; @@ -178,6 +192,12 @@ private: poll (&pfd, 1, timeoutMsecs); } + static void waitToWrite (int handle, int timeoutMsecs) noexcept + { + pollfd pfd { handle, POLLOUT, 0 }; + poll (&pfd, 1, timeoutMsecs); + } + JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Pimpl) };