Skip to content

Commit

Permalink
[netty#1719] Make sure PendingWrite is only recycled once in ChunkedW…
Browse files Browse the repository at this point in the history
…riteHandler
  • Loading branch information
Norman Maurer committed Aug 9, 2013
1 parent 0d6a6bd commit 72395bf
Showing 1 changed file with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.netty.channel.ChannelProgressivePromise;
import io.netty.channel.ChannelPromise;
import io.netty.util.ReferenceCountUtil;
import io.netty.util.concurrent.Promise;
import io.netty.util.internal.PendingWrite;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
Expand Down Expand Up @@ -214,6 +215,7 @@ private void doFlush(final ChannelHandlerContext ctx) throws Exception {
}
needsFlush = true;
final PendingWrite currentWrite = this.currentWrite;
final Promise<Void> currentPromise = this.currentWrite.promise();
final Object pendingMessage = currentWrite.msg();

if (pendingMessage instanceof ChunkedInput) {
Expand Down Expand Up @@ -281,7 +283,16 @@ public void run() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
pendingWrites.decrementAndGet();
currentWrite.successAndRecycle();
if (future.isSuccess()) {
if (!currentPromise.isDone()) {
currentWrite.successAndRecycle();
}
} else {
if (!currentPromise.isDone()) {
currentWrite.failAndRecycle(future.cause());
}
}

closeInput(chunks);
}
});
Expand All @@ -292,7 +303,10 @@ public void operationComplete(ChannelFuture future) throws Exception {
pendingWrites.decrementAndGet();
if (!future.isSuccess()) {
closeInput((ChunkedInput<?>) pendingMessage);
currentWrite.failAndRecycle(future.cause());
if (!currentPromise.isDone()) {
// only recycle if not done before
currentWrite.failAndRecycle(future.cause());
}
} else {
progress((ChannelPromise) currentWrite.promise());
}
Expand All @@ -305,7 +319,10 @@ public void operationComplete(ChannelFuture future) throws Exception {
pendingWrites.decrementAndGet();
if (!future.isSuccess()) {
closeInput((ChunkedInput<?>) pendingMessage);
currentWrite.failAndRecycle(future.cause());
if (!currentPromise.isDone()) {
// only recycle if not done before
currentWrite.failAndRecycle(future.cause());
}
} else {
progress((ChannelPromise) currentWrite.promise());
if (isWritable()) {
Expand Down

0 comments on commit 72395bf

Please sign in to comment.