Skip to content

Commit

Permalink
Don't set SO_REUSEPORT on Windows (#23947) [5.3.z] (#25397)
Browse files Browse the repository at this point in the history
SO_REUSEADDR flag works as expected on POSIX platforms, but will cause indeterminate behaviors on Windows sockets.

See:
- jetty/jetty.project#6661 (comment)
- https://learn.microsoft.com/en-us/windows/win32/winsock/using-so-reuseaddr-and-so-exclusiveaddruse#using-so_reuseaddr

Fixes #23947
Fixes https://hazelcast.atlassian.net/browse/HZ-2213
  • Loading branch information
ldziedziul committed Sep 7, 2023
1 parent 0aa2713 commit 19ac23a
Showing 1 changed file with 17 additions and 2 deletions.
Expand Up @@ -16,14 +16,17 @@

package com.hazelcast.internal.tpcengine.nio;

import com.hazelcast.internal.tpcengine.net.AsyncSocketOptions;
import com.hazelcast.internal.tpcengine.Option;
import com.hazelcast.internal.tpcengine.net.AsyncSocketOptions;
import com.hazelcast.internal.tpcengine.util.OS;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.SocketOption;
import java.net.StandardSocketOptions;
import java.nio.channels.ServerSocketChannel;
import java.util.HashSet;
import java.util.Set;

import static com.hazelcast.internal.tpcengine.util.Preconditions.checkNotNull;
import static com.hazelcast.internal.tpcengine.util.ReflectionUtil.findStaticFieldValue;
Expand All @@ -36,6 +39,12 @@ public class NioAsyncServerSocketOptions implements AsyncSocketOptions {
// This option is available since Java 9, so we need to use reflection.
private static final SocketOption<Boolean> STD_SOCK_OPT_SO_REUSEPORT
= findStaticFieldValue(StandardSocketOptions.class, "SO_REUSEPORT");
private static final Set<SocketOption<?>> WINDOWS_UNSUPPORTED_OPTIONS;

static {
WINDOWS_UNSUPPORTED_OPTIONS = new HashSet<>();
WINDOWS_UNSUPPORTED_OPTIONS.add(STD_SOCK_OPT_SO_REUSEPORT);
}

private final ServerSocketChannel serverSocketChannel;

Expand Down Expand Up @@ -64,7 +73,13 @@ public boolean isSupported(Option option) {
}

private boolean isSupported(SocketOption socketOption) {
return socketOption != null && serverSocketChannel.supportedOptions().contains(socketOption);
if (socketOption == null) {
return false;
}
if (OS.isWindows() && WINDOWS_UNSUPPORTED_OPTIONS.contains(socketOption)) {
return false;
}
return serverSocketChannel.supportedOptions().contains(socketOption);
}

@Override
Expand Down

0 comments on commit 19ac23a

Please sign in to comment.