From b6e71fa34bbb5e533b6b2991246fa77649bcc01b Mon Sep 17 00:00:00 2001 From: Oleg Nenashev Date: Thu, 18 Aug 2016 15:16:14 +0200 Subject: [PATCH] [JENKINS-25218] - Prevent NPE when we close the channel in parallel with the FifoBuffer#receive() loop --- src/main/java/org/jenkinsci/remoting/nio/FifoBuffer.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/main/java/org/jenkinsci/remoting/nio/FifoBuffer.java b/src/main/java/org/jenkinsci/remoting/nio/FifoBuffer.java index 3a82e7440..bee6780ef 100644 --- a/src/main/java/org/jenkinsci/remoting/nio/FifoBuffer.java +++ b/src/main/java/org/jenkinsci/remoting/nio/FifoBuffer.java @@ -310,6 +310,8 @@ public int writeNonBlock(ByteBuffer buf) { * * @return * number of bytes read, or -1 if the given channel has reached EOF and no further read is possible. + * @exception IOException + * receive error */ public int receive(ReadableByteChannel ch) throws IOException { if (closed) @@ -321,6 +323,13 @@ public int receive(ReadableByteChannel ch) throws IOException { int chunk = writable(); if (chunk==0) return written; // no more space to write + + // If the buffer gets closed before we acquire lock, we are at risk of null "w" and NPE. + // So in such case we just interrupt the receive process + if (closed) { + throw new IOException("closed during the receive() operation"); + } + try { int received = w.receive(ch, chunk); if (received==0)