[#2086] Fix race which could produce NPE in AbstractNioUnsafe.finishConnect

This commit is contained in:
Norman Maurer 2014-01-09 08:22:34 +01:00
parent 06823e3aff
commit 0b8e732c6c

View File

@ -236,10 +236,15 @@ public abstract class AbstractNioChannel extends AbstractChannel {
assert eventLoop().inEventLoop(); assert eventLoop().inEventLoop();
assert connectPromise != null; 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 { try {
boolean wasActive = isActive(); boolean wasActive = isActive();
doFinishConnect(); doFinishConnect();
fulfillConnectPromise(connectPromise, wasActive); fulfillConnectPromise(promise, wasActive);
} catch (Throwable t) { } catch (Throwable t) {
if (t instanceof ConnectException) { if (t instanceof ConnectException) {
Throwable newT = new ConnectException(t.getMessage() + ": " + requestedRemoteAddress); 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(). // Use tryFailure() instead of setFailure() to avoid the race against cancel().
connectPromise.tryFailure(t); promise.tryFailure(t);
closeIfClosed(); closeIfClosed();
} finally { } finally {
// Check for null as the connectTimeoutFuture is only created if a connectTimeoutMillis > 0 is used // Check for null as the connectTimeoutFuture is only created if a connectTimeoutMillis > 0 is used