Skip to content

Commit

Permalink
Use the IANA port range when there are less the 5k ports to choose from
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tflori authored and shs96c committed Oct 4, 2021
1 parent 681eae6 commit 294d1c9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static LinuxEphemeralPortRangeDetector getInstance() {
int lowPort = defaultRange.getLowestEphemeralPort();
int highPort = defaultRange.getHighestEphemeralPort();
try (BufferedReader in = new BufferedReader(inputFil)) {
String[] split = in.readLine().split("\\s+");
String[] split = in.readLine().split("\\s+", 3);
lowPort = Integer.parseInt(split[0]);
highPort = Integer.parseInt(split[1]);
} catch (IOException | NullPointerException ignore) {
Expand Down
29 changes: 10 additions & 19 deletions java/src/org/openqa/selenium/net/PortProber.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

package org.openqa.selenium.net;

import static java.lang.Math.max;

import org.openqa.selenium.Platform;

import java.io.IOException;
Expand Down Expand Up @@ -66,29 +64,22 @@ public static int findFreePort() {
}

/**
* Returns a port that is within a probable free range. <p/> Based on the ports in
* http://en.wikipedia.org/wiki/Ephemeral_ports, this method stays away from all well-known
* ephemeral port ranges, since they can arbitrarily race with the operating system in
* allocations. Due to the port-greedy nature of selenium this happens fairly frequently.
* Staying within the known safe range increases the probability tests will run green quite
* significantly.
* Returns a random port within the systems ephemeral port range <p/>
* See https://en.wikipedia.org/wiki/Ephemeral_ports for more information. <p/>
* If the system provides a too short range (mostly on old windows systems)
* the port range suggested from Internet Assigned Numbers Authority will be used.
*
* @return a random port number
*/
private static int createAcceptablePort() {
synchronized (random) {
final int FIRST_PORT;
final int LAST_PORT;

int freeAbove = HIGHEST_PORT - ephemeralRangeDetector.getHighestEphemeralPort();
int freeBelow = max(0, ephemeralRangeDetector.getLowestEphemeralPort() - START_OF_USER_PORTS);
int FIRST_PORT = Math.max(START_OF_USER_PORTS, ephemeralRangeDetector.getLowestEphemeralPort());
int LAST_PORT = Math.min(HIGHEST_PORT, ephemeralRangeDetector.getHighestEphemeralPort());

if (freeAbove > freeBelow) {
FIRST_PORT = ephemeralRangeDetector.getHighestEphemeralPort();
LAST_PORT = 65535;
} else {
FIRST_PORT = 1024;
LAST_PORT = ephemeralRangeDetector.getLowestEphemeralPort();
if (LAST_PORT - FIRST_PORT < 5000) {
EphemeralPortRangeDetector ianaRange = new FixedIANAPortRange();
FIRST_PORT = ianaRange.getLowestEphemeralPort();
LAST_PORT = ianaRange.getHighestEphemeralPort();
}

if (FIRST_PORT == LAST_PORT) {
Expand Down

0 comments on commit 294d1c9

Please sign in to comment.