Skip to content

Commit

Permalink
Make sure all write attempts made after a channel is closed are marke…
Browse files Browse the repository at this point in the history
…d as failure
  • Loading branch information
trustin committed Jul 17, 2013
1 parent a8d67b0 commit cc9b956
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions transport/src/main/java/io/netty/channel/AbstractChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.ByteBufHolder;
import io.netty.util.DefaultAttributeMap;
import io.netty.util.internal.EmptyArrays;
import io.netty.util.internal.PlatformDependent;
import io.netty.util.internal.ThreadLocalRandom;
import io.netty.util.internal.logging.InternalLogger;
Expand All @@ -38,6 +39,14 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha

private static final InternalLogger logger = InternalLoggerFactory.getInstance(AbstractChannel.class);

static final ClosedChannelException CLOSED_CHANNEL_EXCEPTION = new ClosedChannelException();
static final NotYetConnectedException NOT_YET_CONNECTED_EXCEPTION = new NotYetConnectedException();

static {
CLOSED_CHANNEL_EXCEPTION.setStackTrace(EmptyArrays.EMPTY_STACK_TRACE);
NOT_YET_CONNECTED_EXCEPTION.setStackTrace(EmptyArrays.EMPTY_STACK_TRACE);
}

private final Channel parent;
private final long hashCode = ThreadLocalRandom.current().nextLong();
private final Unsafe unsafe;
Expand All @@ -53,7 +62,6 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
private volatile EventLoop eventLoop;
private volatile boolean registered;

private ClosedChannelException closedChannelException;
private boolean inFlush0;

/** Cache for the string representation of this channel */
Expand Down Expand Up @@ -500,10 +508,7 @@ public final void close(final ChannelPromise promise) {

if (!outboundBuffer.isEmpty()) {
// fail all queued messages
if (closedChannelException == null) {
closedChannelException = new ClosedChannelException();
}
outboundBuffer.fail(closedChannelException);
outboundBuffer.fail(CLOSED_CHANNEL_EXCEPTION);
}

outboundBuffer.clearUnflushed();
Expand Down Expand Up @@ -589,7 +594,16 @@ public void run() {

@Override
public void write(Object msg, ChannelPromise promise) {
outboundBuffer.addMessage(msg, promise);
if (!isActive()) {
// Mark the write request as failure if the channl is inactive.
if (isOpen()) {
promise.tryFailure(NOT_YET_CONNECTED_EXCEPTION);
} else {
promise.tryFailure(CLOSED_CHANNEL_EXCEPTION);
}
} else {
outboundBuffer.addMessage(msg, promise);
}
}

@Override
Expand All @@ -611,9 +625,9 @@ protected void flush0() {
// Mark all pending write requests as failure if the channel is inactive.
if (!isActive()) {
if (isOpen()) {
outboundBuffer.fail(new NotYetConnectedException());
outboundBuffer.fail(NOT_YET_CONNECTED_EXCEPTION);
} else {
outboundBuffer.fail(new ClosedChannelException());
outboundBuffer.fail(CLOSED_CHANNEL_EXCEPTION);
}
inFlush0 = false;
return;
Expand Down Expand Up @@ -675,8 +689,7 @@ protected final boolean ensureOpen(ChannelPromise promise) {
return true;
}

Exception e = new ClosedChannelException();
promise.setFailure(e);
promise.setFailure(CLOSED_CHANNEL_EXCEPTION);
return false;
}

Expand Down

0 comments on commit cc9b956

Please sign in to comment.