From 4158152b24c1e20cc9d024486a6bc6a0ee336a92 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Wed, 29 Feb 2012 14:02:12 -0800 Subject: [PATCH] Trigger exceptionCaught event from the middle of the pipline (#210) .. because the previous handlers have no interest in the exceptions raised by the next handlers. --- .../handler/stream/ChunkedWriteHandler.java | 8 +++----- .../handler/timeout/WriteTimeoutHandler.java | 2 +- .../main/java/io/netty/channel/Channels.java | 17 ++++++++++++++++- 3 files changed, 20 insertions(+), 7 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 6fc29eaebd..2e70697e0e 100644 --- a/handler/src/main/java/io/netty/handler/stream/ChunkedWriteHandler.java +++ b/handler/src/main/java/io/netty/handler/stream/ChunkedWriteHandler.java @@ -169,16 +169,14 @@ public class ChunkedWriteHandler implements ChannelUpstreamHandler, ChannelDowns } currentEvent.getFuture().setFailure(cause); fireExceptionCaught = true; - - currentEvent = null; } if (fireExceptionCaught) { if (fireNow) { - fireExceptionCaught(ctx.getChannel(), cause); + fireExceptionCaught(ctx, cause); } else { - fireExceptionCaughtLater(ctx.getChannel(), cause); + fireExceptionCaughtLater(ctx, cause); } } } @@ -227,7 +225,7 @@ public class ChunkedWriteHandler implements ChannelUpstreamHandler, ChannelDowns if (fireNow) { fireExceptionCaught(ctx, t); } else { - fireExceptionCaughtLater(ctx.getChannel(), t); + fireExceptionCaughtLater(ctx, t); } closeInput(chunks); diff --git a/handler/src/main/java/io/netty/handler/timeout/WriteTimeoutHandler.java b/handler/src/main/java/io/netty/handler/timeout/WriteTimeoutHandler.java index 5dd2b4e19c..bb6adde8fc 100644 --- a/handler/src/main/java/io/netty/handler/timeout/WriteTimeoutHandler.java +++ b/handler/src/main/java/io/netty/handler/timeout/WriteTimeoutHandler.java @@ -154,7 +154,7 @@ public class WriteTimeoutHandler extends SimpleChannelDownstreamHandler } protected void writeTimedOut(ChannelHandlerContext ctx) throws Exception { - Channels.fireExceptionCaughtLater(ctx.getChannel(), EXCEPTION); + Channels.fireExceptionCaughtLater(ctx, EXCEPTION); } private final class WriteTimeoutTask implements TimerTask { diff --git a/transport/src/main/java/io/netty/channel/Channels.java b/transport/src/main/java/io/netty/channel/Channels.java index c5aa9ce93e..1ea98c0827 100644 --- a/transport/src/main/java/io/netty/channel/Channels.java +++ b/transport/src/main/java/io/netty/channel/Channels.java @@ -520,7 +520,22 @@ public final class Channels { }); } - + /** + * Sends a {@code "exceptionCaught"} event to the + * {@link ChannelUpstreamHandler} which is placed in the closest upstream + * from the handler associated with the specified + * {@link ChannelHandlerContext} once the io-thread runs again. + */ + public static ChannelFuture fireExceptionCaughtLater(final ChannelHandlerContext ctx, final Throwable cause) { + return ctx.getPipeline().execute(new Runnable() { + + @Override + public void run() { + fireExceptionCaught(ctx, cause); + } + }); + } + /** * Sends a {@code "exceptionCaught"} event to the first * {@link ChannelUpstreamHandler} in the {@link ChannelPipeline} of