Skip to content

Commit

Permalink
[JENKINS-25218] - Prevent NPE when we close the channel in parallel w…
Browse files Browse the repository at this point in the history
…ith the FifoBuffer#receive() loop
  • Loading branch information
oleg-nenashev committed Aug 18, 2016
1 parent 072341c commit b6e71fa
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/main/java/org/jenkinsci/remoting/nio/FifoBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit b6e71fa

Please sign in to comment.