Skip to content

Commit c6fa827

Browse files
committed
NETTY-417 client channel still open after close and wait
* Fixed a race condition where NioSocketChannel's state variable is updated *after* its close future is notified * Removed unnecessary use of ChannelFutureListeners in NioSocketChannel and AbstractChannel
1 parent e85996e commit c6fa827

File tree

2 files changed

+5
-22
lines changed

2 files changed

+5
-22
lines changed

src/main/java/org/jboss/netty/channel/AbstractChannel.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
public abstract class AbstractChannel implements Channel {
3333

3434
static final ConcurrentMap<Integer, Channel> allChannels = new ConcurrentHashMap<Integer, Channel>();
35-
private static final IdDeallocator ID_DEALLOCATOR = new IdDeallocator();
3635

3736
private static Integer allocateId(Channel channel) {
3837
Integer id = Integer.valueOf(System.identityHashCode(channel));
@@ -49,17 +48,6 @@ private static Integer allocateId(Channel channel) {
4948
}
5049
}
5150

52-
private static final class IdDeallocator implements ChannelFutureListener {
53-
IdDeallocator() {
54-
super();
55-
}
56-
57-
@Override
58-
public void operationComplete(ChannelFuture future) throws Exception {
59-
allChannels.remove(future.getChannel().getId());
60-
}
61-
}
62-
6351
private final Integer id;
6452
private final Channel parent;
6553
private final ChannelFactory factory;
@@ -94,7 +82,6 @@ protected AbstractChannel(
9482
this.pipeline = pipeline;
9583

9684
id = allocateId(this);
97-
closeFuture.addListener(ID_DEALLOCATOR);
9885

9986
pipeline.attach(this, sink);
10087
}
@@ -200,6 +187,10 @@ public boolean isOpen() {
200187
* closed yet
201188
*/
202189
protected boolean setClosed() {
190+
// Deallocate the current channel's ID from allChannels so that other
191+
// new channels can use it.
192+
allChannels.remove(id);
193+
203194
return closeFuture.setClosed();
204195
}
205196

src/main/java/org/jboss/netty/channel/socket/nio/NioSocketChannel.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,6 @@ public NioSocketChannel(
8383
this.socket = socket;
8484
this.worker = worker;
8585
config = new DefaultNioSocketChannelConfig(socket.socket());
86-
87-
// TODO Move the state variable to AbstractChannel so that we don't need
88-
// to add many listeners.
89-
getCloseFuture().addListener(new ChannelFutureListener() {
90-
@Override
91-
public void operationComplete(ChannelFuture future) throws Exception {
92-
state = ST_CLOSED;
93-
}
94-
});
9586
}
9687

9788
@Override
@@ -157,6 +148,7 @@ final void setConnected() {
157148

158149
@Override
159150
protected boolean setClosed() {
151+
state = ST_CLOSED;
160152
return super.setClosed();
161153
}
162154

0 commit comments

Comments
 (0)