Skip to content

Commit

Permalink
Make everserver port selection random
Browse files Browse the repository at this point in the history
  • Loading branch information
frode-aarstad committed Feb 7, 2025
1 parent 4bf7fea commit a459d69
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/everest/detached/jobs/everserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import json
import logging
import os
import random
import socket
import ssl
import threading
Expand Down Expand Up @@ -273,7 +274,15 @@ def get_shared_data(


def _find_open_port(host, lower, upper) -> int:
for port in range(lower, upper):
# Making the port selection random does not fix the problem that an
# everserver might be assigned a port that another everserver in the process
# of shutting down already have.
#
# Since this problem is very unlikely in the normal usage of everest this change
# is mainly for alowing testing to run in paralell.

for _ in range(10):
port = random.randint(lower, upper)
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind((host, port))
Expand All @@ -283,7 +292,7 @@ def _find_open_port(host, lower, upper) -> int:
logging.getLogger("everserver").info(
f"Port {port} for host {host} is taken"
)
msg = f"No open port for host {host} in the range {lower}-{upper}"
msg = f"Failed 10 times to get a random port in the range {lower}-{upper} on {host}. Giving up."
logging.getLogger("everserver").exception(msg)
raise Exception(msg)

Expand Down

0 comments on commit a459d69

Please sign in to comment.