[#239] IdleStateHandler and ReadTimeoutHandler starts two timers

- Ensure initialize does not start timer twice
This commit is contained in:
Trustin Lee 2012-08-21 20:13:44 +09:00
parent a93ada2031
commit 31a51b4937
2 changed files with 33 additions and 18 deletions

View File

@ -309,8 +309,13 @@ public class IdleStateHandler extends SimpleChannelUpstreamHandler
// Avoid the case where destroy() is called before scheduling timeouts. // Avoid the case where destroy() is called before scheduling timeouts.
// See: https://github.com/netty/netty/issues/143 // See: https://github.com/netty/netty/issues/143
if (state.destroyed) { synchronized (state) {
return; switch (state.state) {
case 1:
case 2:
return;
}
state.state = 1;
} }
state.lastReadTime = state.lastWriteTime = System.currentTimeMillis(); state.lastReadTime = state.lastWriteTime = System.currentTimeMillis();
@ -332,11 +337,12 @@ public class IdleStateHandler extends SimpleChannelUpstreamHandler
} }
private static void destroy(ChannelHandlerContext ctx) { private static void destroy(ChannelHandlerContext ctx) {
State state; State state = state(ctx);
synchronized (state) {
synchronized (ctx) { if (state.state != 1) {
state = state(ctx); return;
state.destroyed = true; }
state.state = 2;
} }
if (state.readerIdleTimeout != null) { if (state.readerIdleTimeout != null) {
@ -478,9 +484,8 @@ public class IdleStateHandler extends SimpleChannelUpstreamHandler
} }
private static final class State { private static final class State {
State() { // 0 - none, 1 - initialized, 2 - destroyed
super(); int state;
}
volatile Timeout readerIdleTimeout; volatile Timeout readerIdleTimeout;
volatile long lastReadTime; volatile long lastReadTime;
@ -490,6 +495,8 @@ public class IdleStateHandler extends SimpleChannelUpstreamHandler
volatile Timeout allIdleTimeout; volatile Timeout allIdleTimeout;
volatile boolean destroyed; State() {
super();
}
} }
} }

View File

@ -189,8 +189,13 @@ public class ReadTimeoutHandler extends SimpleChannelUpstreamHandler
// Avoid the case where destroy() is called before scheduling timeouts. // Avoid the case where destroy() is called before scheduling timeouts.
// See: https://github.com/netty/netty/issues/143 // See: https://github.com/netty/netty/issues/143
if (state.destroyed) { synchronized (state) {
return; switch (state.state) {
case 1:
case 2:
return;
}
state.state = 1;
} }
if (timeoutMillis > 0) { if (timeoutMillis > 0) {
@ -199,10 +204,12 @@ public class ReadTimeoutHandler extends SimpleChannelUpstreamHandler
} }
private static void destroy(ChannelHandlerContext ctx) { private static void destroy(ChannelHandlerContext ctx) {
State state; State state = state(ctx);
synchronized (ctx) { synchronized (state) {
state = state(ctx); if (state.state != 1) {
state.destroyed = true; return;
}
state.state = 2;
} }
if (state.timeout != null) { if (state.timeout != null) {
@ -269,9 +276,10 @@ public class ReadTimeoutHandler extends SimpleChannelUpstreamHandler
} }
private static final class State { private static final class State {
// 0 - none, 1 - initialized, 2 - destroyed
int state;
volatile Timeout timeout; volatile Timeout timeout;
volatile long lastReadTime = System.currentTimeMillis(); volatile long lastReadTime = System.currentTimeMillis();
volatile boolean destroyed;
State() { State() {
super(); super();