test(io): fix freeport function to prevent failures

Sometimes, a healthcheck related test fails as occurred in
https://github.com/PostgREST/postgrest/actions/runs/19771357953/job/56655949002.
This happens due to freeport function accidently picking up a used port.

Signed-off-by: Taimoor Zaeem <taimoorzaeem@gmail.com>
(cherry picked from commit 50eec773bf)
This commit is contained in:
Taimoor Zaeem
2025-11-30 13:38:55 -05:00
committed by Steve Chavez
parent 05074f41c2
commit e8cb0e33eb
2 changed files with 10 additions and 6 deletions
+4 -4
View File
@@ -113,7 +113,7 @@ def run(
env["PGRST_SERVER_UNIX_SOCKET"] = str(socketfile)
baseurl = "http+unix://" + urllib.parse.quote_plus(str(socketfile))
adminport = freeport(port)
adminport = freeport(used_ports=[port])
env["PGRST_ADMIN_SERVER_PORT"] = str(adminport)
adminhost = f"[{host}]" if host and is_ipv6(host) else localhost
adminurl = f"http://{adminhost}:{adminport}"
@@ -165,14 +165,14 @@ def run(
process.wait()
def freeport(used_port=None):
"Find a free port on localhost."
def freeport(used_ports=None):
"Find an unused free port on localhost."
while True:
with contextlib.closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
s.bind(("", 0))
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
port = s.getsockname()[1]
if port != used_port:
if used_ports is None or port not in used_ports:
return port