From 84c98b6333e3e4fa3c882955298d9ab0e8246dc6 Mon Sep 17 00:00:00 2001
From: Jelte Fennema-Nio <jelte.fennema@microsoft.com>
Date: Thu, 6 Jun 2024 23:50:51 +0200
Subject: [PATCH v1] Don't use ephemeral port range

---
 src/test/perl/PostgreSQL/Test/Cluster.pm | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/src/test/perl/PostgreSQL/Test/Cluster.pm b/src/test/perl/PostgreSQL/Test/Cluster.pm
index 83f385a4870..359cfc3715e 100644
--- a/src/test/perl/PostgreSQL/Test/Cluster.pm
+++ b/src/test/perl/PostgreSQL/Test/Cluster.pm
@@ -125,6 +125,13 @@ our $min_compat = 12;
 # list of file reservations made by get_free_port
 my @port_reservation_files;
 
+# this is out of ephemeral port range for many systems hence
+# it is a lower chance that it will conflict with "in-use" ports
+my $port_lower_bound = 10200;
+
+# ephemeral port start on many Linux systems
+my $port_upper_bound = 32768;
+
 INIT
 {
 
@@ -149,7 +156,7 @@ INIT
 	$ENV{PGDATABASE} = 'postgres';
 
 	# Tracking of last port value assigned to accelerate free port lookup.
-	$last_port_assigned = int(rand() * 16384) + 49152;
+	$last_port_assigned = int(rand() * ($port_upper_bound - $port_lower_bound - 1)) + $port_lower_bound;
 
 	# Set the port lock directory
 
@@ -1687,7 +1694,7 @@ sub get_free_port
 	{
 
 		# advance $port, wrapping correctly around range end
-		$port = 49152 if ++$port >= 65536;
+		$port = $port_lower_bound if ++$port >= $port_upper_bound;
 		print "# Checking port $port\n";
 
 		# Check first that candidate port number is not included in

base-commit: 7b71e5bbccd6c86bc12ba0124e7282cfb3aa3226
-- 
2.34.1

