From 0b8e732c6cce5bade1cf5d8a8d87e5c788c6d20f Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Thu, 9 Jan 2014 08:22:34 +0100 Subject: [PATCH] [#2086] Fix race which could produce NPE in AbstractNioUnsafe.finishConnect --- .../java/io/netty/channel/nio/AbstractNioChannel.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/transport/src/main/java/io/netty/channel/nio/AbstractNioChannel.java b/transport/src/main/java/io/netty/channel/nio/AbstractNioChannel.java index c02adaa373..b52db1eb96 100644 --- a/transport/src/main/java/io/netty/channel/nio/AbstractNioChannel.java +++ b/transport/src/main/java/io/netty/channel/nio/AbstractNioChannel.java @@ -236,10 +236,15 @@ public abstract class AbstractNioChannel extends AbstractChannel { assert eventLoop().inEventLoop(); assert connectPromise != null; + // Cache connect promise as connectPromise will be set to null once it is notified. + // This is needed to prevent a possible NPE + // See https://github.com/netty/netty/issues/2086 + ChannelPromise promise = connectPromise; + try { boolean wasActive = isActive(); doFinishConnect(); - fulfillConnectPromise(connectPromise, wasActive); + fulfillConnectPromise(promise, wasActive); } catch (Throwable t) { if (t instanceof ConnectException) { Throwable newT = new ConnectException(t.getMessage() + ": " + requestedRemoteAddress); @@ -248,7 +253,7 @@ public abstract class AbstractNioChannel extends AbstractChannel { } // Use tryFailure() instead of setFailure() to avoid the race against cancel(). - connectPromise.tryFailure(t); + promise.tryFailure(t); closeIfClosed(); } finally { // Check for null as the connectTimeoutFuture is only created if a connectTimeoutMillis > 0 is used