From 1884a5697c0c7957c534711a8f8a7fb0be9b7369 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Thu, 20 Feb 2014 17:14:25 -0800 Subject: [PATCH] Avoid unnecessary IllegalStateException in ChunkedWriteHandler Motivation: ChunkedWriteHandler can sometimes fail to write the last chunk of a ChunkedInput due to an I/O error. Subsequently, the ChunkedInput's associated promise is marked as failure and the connection is closed. When the connection is closed, ChunkedWriteHandler attempts to clean up its message queue and to mark their promises as success or failure. However, because the promise of the ChunkedInput, which was consumed completely yet failed to be written, is already marked as failure, the attempt to mark it as success fails, leading a WARN level log. Modification: Use trySuccess() instead of setSuccess() so that the attempt to mark a ChunkedInput as success does not raise an exception even if the promise is already done. Result: Fixes #2249 --- .../io/netty/handler/stream/ChunkedWriteHandler.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/handler/src/main/java/io/netty/handler/stream/ChunkedWriteHandler.java b/handler/src/main/java/io/netty/handler/stream/ChunkedWriteHandler.java index 7c53d859f7..201df47ab6 100644 --- a/handler/src/main/java/io/netty/handler/stream/ChunkedWriteHandler.java +++ b/handler/src/main/java/io/netty/handler/stream/ChunkedWriteHandler.java @@ -339,17 +339,21 @@ public class ChunkedWriteHandler void fail(Throwable cause) { ReferenceCountUtil.release(msg); - if (promise != null) { - promise.tryFailure(cause); - } + promise.tryFailure(cause); } void success() { + if (promise.isDone()) { + // No need to notify the progress or fulfill the promise because it's done already. + return; + } + if (promise instanceof ChannelProgressivePromise) { // Now we know what the total is. ((ChannelProgressivePromise) promise).tryProgress(progress, progress); } - promise.setSuccess(); + + promise.trySuccess(); } void progress(int amount) {