Skip to content

Commit

Permalink
NioEventLoop.register(...) should offload to the EventLoop if not alr… (
Browse files Browse the repository at this point in the history
netty#8612)


Motivation:

java.nio.channels.spi.AbstractSelectableChannel.register(...) need to obtain multiple locks during execution which may produce a long wait time if we currently select. This lead to multiple CI failures in the past.

Modifications:

Ensure the register call takes place on the EventLoop.

Result:

No more flacky CI test timeouts.
  • Loading branch information
normanmaurer authored Dec 5, 2018
1 parent 9f9aa1a commit 6739755
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion transport/src/main/java/io/netty/channel/nio/NioEventLoop.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@
import java.lang.reflect.Field;
import java.nio.channels.CancelledKeyException;
import java.nio.channels.SelectableChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SelectionKey;

import java.nio.channels.spi.SelectorProvider;
import java.security.AccessController;
import java.security.PrivilegedAction;
Expand Down Expand Up @@ -293,6 +294,26 @@ public void register(final SelectableChannel ch, final int interestOps, final Ni
throw new IllegalStateException("event loop shut down");
}

if (inEventLoop()) {
register0(ch, interestOps, task);
} else {
try {
// Offload to the EventLoop as otherwise java.nio.channels.spi.AbstractSelectableChannel.register
// may block for a long time while trying to obtain an internal lock that may be hold while selecting.
submit(new Runnable() {
@Override
public void run() {
register0(ch, interestOps, task);
}
}).sync();
} catch (InterruptedException ignore) {
// Even if interrupted we did schedule it so just mark the Thread as interrupted.
Thread.currentThread().interrupt();
}
}
}

private void register0(SelectableChannel ch, int interestOps, NioTask<?> task) {
try {
ch.register(unwrappedSelector, interestOps, task);
} catch (Exception e) {
Expand Down

0 comments on commit 6739755

Please sign in to comment.