Skip to content

Commit 294d1c9

Browse files
tflorishs96c
authored andcommitted
Use the IANA port range when there are less the 5k ports to choose from
Old Windows systems in particular (but also other platforms) may suggest an ephemeral port range that is too narrow for Selenium to use comfortably. In these cases, fall back to use the IANA port range.
1 parent 681eae6 commit 294d1c9

File tree

2 files changed

+11
-20
lines changed

2 files changed

+11
-20
lines changed

java/src/org/openqa/selenium/net/LinuxEphemeralPortRangeDetector.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public static LinuxEphemeralPortRangeDetector getInstance() {
4747
int lowPort = defaultRange.getLowestEphemeralPort();
4848
int highPort = defaultRange.getHighestEphemeralPort();
4949
try (BufferedReader in = new BufferedReader(inputFil)) {
50-
String[] split = in.readLine().split("\\s+");
50+
String[] split = in.readLine().split("\\s+", 3);
5151
lowPort = Integer.parseInt(split[0]);
5252
highPort = Integer.parseInt(split[1]);
5353
} catch (IOException | NullPointerException ignore) {

java/src/org/openqa/selenium/net/PortProber.java

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717

1818
package org.openqa.selenium.net;
1919

20-
import static java.lang.Math.max;
21-
2220
import org.openqa.selenium.Platform;
2321

2422
import java.io.IOException;
@@ -66,29 +64,22 @@ public static int findFreePort() {
6664
}
6765

6866
/**
69-
* Returns a port that is within a probable free range. <p/> Based on the ports in
70-
* http://en.wikipedia.org/wiki/Ephemeral_ports, this method stays away from all well-known
71-
* ephemeral port ranges, since they can arbitrarily race with the operating system in
72-
* allocations. Due to the port-greedy nature of selenium this happens fairly frequently.
73-
* Staying within the known safe range increases the probability tests will run green quite
74-
* significantly.
67+
* Returns a random port within the systems ephemeral port range <p/>
68+
* See https://en.wikipedia.org/wiki/Ephemeral_ports for more information. <p/>
69+
* If the system provides a too short range (mostly on old windows systems)
70+
* the port range suggested from Internet Assigned Numbers Authority will be used.
7571
*
7672
* @return a random port number
7773
*/
7874
private static int createAcceptablePort() {
7975
synchronized (random) {
80-
final int FIRST_PORT;
81-
final int LAST_PORT;
82-
83-
int freeAbove = HIGHEST_PORT - ephemeralRangeDetector.getHighestEphemeralPort();
84-
int freeBelow = max(0, ephemeralRangeDetector.getLowestEphemeralPort() - START_OF_USER_PORTS);
76+
int FIRST_PORT = Math.max(START_OF_USER_PORTS, ephemeralRangeDetector.getLowestEphemeralPort());
77+
int LAST_PORT = Math.min(HIGHEST_PORT, ephemeralRangeDetector.getHighestEphemeralPort());
8578

86-
if (freeAbove > freeBelow) {
87-
FIRST_PORT = ephemeralRangeDetector.getHighestEphemeralPort();
88-
LAST_PORT = 65535;
89-
} else {
90-
FIRST_PORT = 1024;
91-
LAST_PORT = ephemeralRangeDetector.getLowestEphemeralPort();
79+
if (LAST_PORT - FIRST_PORT < 5000) {
80+
EphemeralPortRangeDetector ianaRange = new FixedIANAPortRange();
81+
FIRST_PORT = ianaRange.getLowestEphemeralPort();
82+
LAST_PORT = ianaRange.getHighestEphemeralPort();
9283
}
9384

9485
if (FIRST_PORT == LAST_PORT) {

0 commit comments

Comments
 (0)