From 0317baa10d271a28187f8bcd831ffbfa9ef5cc93 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Tue, 26 Feb 2013 10:19:59 -0800 Subject: [PATCH] Fix regression in AioCompletionHandler that leads to StackOverflowError --- .../channel/aio/AioCompletionHandler.java | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/transport/src/main/java/io/netty/channel/aio/AioCompletionHandler.java b/transport/src/main/java/io/netty/channel/aio/AioCompletionHandler.java index d965d39db1..4292f3f018 100644 --- a/transport/src/main/java/io/netty/channel/aio/AioCompletionHandler.java +++ b/transport/src/main/java/io/netty/channel/aio/AioCompletionHandler.java @@ -35,11 +35,27 @@ public abstract class AioCompletionHandler implements Comp */ protected abstract void failed0(Throwable exc, A channel); + private static final int MAX_STACK_DEPTH = 8; + private static final ThreadLocal STACK_DEPTH = new ThreadLocal() { + @Override + protected Integer initialValue() { + return 0; + } + }; + @Override public final void completed(final V result, final A channel) { EventLoop loop = channel.eventLoop(); if (loop.inEventLoop()) { - completed0(result, channel); + Integer d = STACK_DEPTH.get(); + if (d < MAX_STACK_DEPTH) { + STACK_DEPTH.set(d + 1); + try { + completed0(result, channel); + } finally { + STACK_DEPTH.set(d); + } + } } else { loop.execute(new Runnable() { @Override @@ -54,7 +70,15 @@ public abstract class AioCompletionHandler implements Comp public final void failed(final Throwable exc, final A channel) { EventLoop loop = channel.eventLoop(); if (loop.inEventLoop()) { - failed0(exc, channel); + Integer d = STACK_DEPTH.get(); + if (d < MAX_STACK_DEPTH) { + STACK_DEPTH.set(d + 1); + try { + failed0(exc, channel); + } finally { + STACK_DEPTH.set(d); + } + } } else { loop.execute(new Runnable() { @Override