Skip to content

Commit 653aa48

Browse files
committed
Lock NIO loop initialization (precaution)
The initialization is supposed to be called in a lock already. (cherry picked from commit 7d7ca78)
1 parent e1f2c8e commit 653aa48

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/main/java/com/rabbitmq/client/impl/nio/NioLoopContext.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,19 @@ public NioLoopContext(SocketChannelFrameHandlerFactory socketChannelFrameHandler
5454
}
5555

5656
void initStateIfNecessary() throws IOException {
57-
// FIXME this should be synchronized
58-
if (this.readSelectorState == null) {
59-
this.readSelectorState = new SelectorHolder(Selector.open());
60-
this.writeSelectorState = new SelectorHolder(Selector.open());
57+
// This code is supposed to be called only from the SocketChannelFrameHandlerFactory
58+
// and while holding the lock.
59+
// We lock just in case some other code calls this method in the future.
60+
socketChannelFrameHandlerFactory.lock();
61+
try {
62+
if (this.readSelectorState == null) {
63+
this.readSelectorState = new SelectorHolder(Selector.open());
64+
this.writeSelectorState = new SelectorHolder(Selector.open());
6165

62-
startIoLoops();
66+
startIoLoops();
67+
}
68+
} finally {
69+
socketChannelFrameHandlerFactory.unlock();
6370
}
6471
}
6572

0 commit comments

Comments
 (0)