Skip to content

Commit 4dd1b3a

Browse files
committed
8330940: Impossible to create a socket backlog greater than 200 on Windows 8+
Reviewed-by: michaelm, dfuchs, alanb
1 parent 240541e commit 4dd1b3a

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

src/java.base/windows/native/libnio/ch/Net.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,13 @@ Java_sun_nio_ch_Net_bind0(JNIEnv *env, jclass clazz, jobject fdo, jboolean prefe
214214
JNIEXPORT void JNICALL
215215
Java_sun_nio_ch_Net_listen(JNIEnv *env, jclass cl, jobject fdo, jint backlog)
216216
{
217+
/*
218+
* Use SOMAXCONN_HINT when backlog larger than 200. It will adjust the value
219+
* to be within the range (200, 65535).
220+
*/
221+
if (backlog > 200) {
222+
backlog = SOMAXCONN_HINT(backlog);
223+
}
217224
if (listen(fdval(env,fdo), backlog) == SOCKET_ERROR) {
218225
NET_ThrowNew(env, WSAGetLastError(), "listen");
219226
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
import java.io.IOException;
25+
import java.net.InetAddress;
26+
import java.net.InetSocketAddress;
27+
import java.net.ServerSocket;
28+
import java.net.Socket;
29+
import java.nio.channels.AsynchronousServerSocketChannel;
30+
import java.nio.channels.ServerSocketChannel;
31+
32+
import org.junit.jupiter.api.Test;
33+
import static org.junit.jupiter.api.Assertions.fail;
34+
35+
/*
36+
* @test
37+
* @bug 8330940
38+
* @summary verify that java.net.ServerSocket and the server socket channels in java.nio.channels
39+
* when configured with a backlog of >=200 on Windows, will allow for those many
40+
* backlogged Socket connections
41+
* @requires os.family == "windows"
42+
* @run junit LargeBacklogTest
43+
*/
44+
class LargeBacklogTest {
45+
46+
@Test
47+
void testServerSocket() throws Exception {
48+
final int backlog = 242;
49+
// Create a ServerSocket configured with the given backlog.
50+
// The ServerSocket never accept()s a connection so each connect() attempt
51+
// will be backlogged.
52+
try (var server = new ServerSocket(0, backlog, InetAddress.getLoopbackAddress())) {
53+
final int serverPort = server.getLocalPort();
54+
testBackloggedConnects(backlog, serverPort);
55+
}
56+
}
57+
58+
@Test
59+
void testServerSocketChannel() throws Exception {
60+
final int backlog = 213;
61+
// Create a ServerSocketChannel configured with the given backlog.
62+
// The channel never accept()s a connection so each connect() attempt
63+
// will be backlogged.
64+
try (var serverChannel = ServerSocketChannel.open()) {
65+
serverChannel.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), backlog);
66+
final int serverPort = ((InetSocketAddress) serverChannel.getLocalAddress()).getPort();
67+
testBackloggedConnects(backlog, serverPort);
68+
}
69+
}
70+
71+
@Test
72+
void testAsynchronousServerSocketChannel() throws Exception {
73+
final int backlog = 209;
74+
// Create a AsynchronousServerSocketChannel configured with the given backlog.
75+
// The channel never accept()s a connection so each connect() attempt
76+
// will be backlogged.
77+
try (var serverChannel = AsynchronousServerSocketChannel.open()) {
78+
serverChannel.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), backlog);
79+
final int serverPort = ((InetSocketAddress) serverChannel.getLocalAddress()).getPort();
80+
testBackloggedConnects(backlog, serverPort);
81+
}
82+
}
83+
84+
private static void testBackloggedConnects(final int backlog, final int serverPort) {
85+
int numSuccessfulConnects = 0;
86+
System.err.println("attempting " + backlog + " connections to port " + serverPort);
87+
// attempt the Socket connections
88+
for (int i = 1; i <= backlog; i++) {
89+
try (final Socket sock = new Socket(InetAddress.getLoopbackAddress(), serverPort)) {
90+
numSuccessfulConnects++;
91+
System.err.println("connection " + i + " established " + sock);
92+
} catch (IOException ioe) {
93+
System.err.println("connection attempt " + i + " failed: " + ioe);
94+
// do not attempt any more connections
95+
break;
96+
}
97+
}
98+
System.err.println(numSuccessfulConnects + " connections successfully established");
99+
// ideally we expect the number of successful connections to be equal to the backlog value.
100+
// however in certain environments, it's possible that some other process attempts a
101+
// connection to the server's port. so we allow for a small number of connection attempts
102+
// to fail (due to exceeding the backlog)
103+
final int minimumExpectedSuccessfulConns = backlog - 5;
104+
if (numSuccessfulConnects < minimumExpectedSuccessfulConns) {
105+
fail("expected at least " + minimumExpectedSuccessfulConns
106+
+ " successful connections for a backlog of " + backlog + ", but only "
107+
+ numSuccessfulConnects + " were successful");
108+
}
109+
}
110+
}

0 commit comments

Comments
 (0)