Fix #239: IdleStateHandler starts two timers
1) Do not rely on ChannelPipeline.isAttached() to ensure initialize() is called once. 2) Fix a race condition where initialize() can schedule timeouts after destroy() is called.
This commit is contained in:
parent
5ecadf930e
commit
bab2ebc32d
@ -222,15 +222,7 @@ public class IdleStateHandler extends SimpleChannelUpstreamHandler
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void beforeAdd(ChannelHandlerContext ctx) throws Exception {
|
public void beforeAdd(ChannelHandlerContext ctx) throws Exception {
|
||||||
if (ctx.getPipeline().isAttached()) {
|
|
||||||
// channelOpen event has been fired already, which means
|
|
||||||
// this.channelOpen() will not be invoked.
|
|
||||||
// We have to initialize here instead.
|
|
||||||
initialize(ctx);
|
initialize(ctx);
|
||||||
} else {
|
|
||||||
// channelOpen event has not been fired yet.
|
|
||||||
// this.channelOpen() will be invoked and initialization will occur there.
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void afterAdd(ChannelHandlerContext ctx) throws Exception {
|
public void afterAdd(ChannelHandlerContext ctx) throws Exception {
|
||||||
@ -245,16 +237,6 @@ public class IdleStateHandler extends SimpleChannelUpstreamHandler
|
|||||||
// NOOP
|
// NOOP
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e)
|
|
||||||
throws Exception {
|
|
||||||
// This method will be invoked only if this handler was added
|
|
||||||
// before channelOpen event is fired. If a user adds this handler
|
|
||||||
// after the channelOpen event, initialize() will be called by beforeAdd().
|
|
||||||
initialize(ctx);
|
|
||||||
ctx.sendUpstream(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e)
|
public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e)
|
||||||
throws Exception {
|
throws Exception {
|
||||||
@ -281,8 +263,13 @@ public class IdleStateHandler extends SimpleChannelUpstreamHandler
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void initialize(ChannelHandlerContext ctx) {
|
private void initialize(ChannelHandlerContext ctx) {
|
||||||
State state = new State();
|
State state = state(ctx);
|
||||||
ctx.setAttachment(state);
|
|
||||||
|
// Avoid the case where destroy() is called before scheduling timeouts.
|
||||||
|
// See: https://github.com/netty/netty/issues/143
|
||||||
|
if (state.destroyed) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
state.lastReadTime = state.lastWriteTime = System.currentTimeMillis();
|
state.lastReadTime = state.lastWriteTime = System.currentTimeMillis();
|
||||||
if (readerIdleTimeMillis > 0) {
|
if (readerIdleTimeMillis > 0) {
|
||||||
@ -303,13 +290,13 @@ public class IdleStateHandler extends SimpleChannelUpstreamHandler
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void destroy(ChannelHandlerContext ctx) {
|
private void destroy(ChannelHandlerContext ctx) {
|
||||||
State state = (State) ctx.getAttachment();
|
State state;
|
||||||
|
|
||||||
|
synchronized (ctx) {
|
||||||
|
state = state(ctx);
|
||||||
|
state.destroyed = true;
|
||||||
|
}
|
||||||
|
|
||||||
// Check if the state was set before, it may not if the destroy method was called before the
|
|
||||||
// channelOpen(...) method.
|
|
||||||
//
|
|
||||||
// See #143
|
|
||||||
if (state != null) {
|
|
||||||
if (state.readerIdleTimeout != null) {
|
if (state.readerIdleTimeout != null) {
|
||||||
state.readerIdleTimeout.cancel();
|
state.readerIdleTimeout.cancel();
|
||||||
state.readerIdleTimeout = null;
|
state.readerIdleTimeout = null;
|
||||||
@ -324,6 +311,18 @@ public class IdleStateHandler extends SimpleChannelUpstreamHandler
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private State state(ChannelHandlerContext ctx) {
|
||||||
|
State state;
|
||||||
|
synchronized (ctx) {
|
||||||
|
// FIXME: It could have been better if there is setAttachmentIfAbsent().
|
||||||
|
state = (State) ctx.getAttachment();
|
||||||
|
if (state != null) {
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
state = new State();
|
||||||
|
ctx.setAttachment(state);
|
||||||
|
}
|
||||||
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void channelIdle(
|
protected void channelIdle(
|
||||||
@ -448,5 +447,7 @@ public class IdleStateHandler extends SimpleChannelUpstreamHandler
|
|||||||
volatile long lastWriteTime;
|
volatile long lastWriteTime;
|
||||||
|
|
||||||
volatile Timeout allIdleTimeout;
|
volatile Timeout allIdleTimeout;
|
||||||
|
|
||||||
|
volatile boolean destroyed;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user