Skip to content

Commit 0ea20cd

Browse files
committed
add message to why port is skipped
1 parent 60b1baf commit 0ea20cd

1 file changed

Lines changed: 21 additions & 33 deletions

File tree

reflex/utils/processes.py

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,8 @@ def _can_bind_at_port(
6969
try:
7070
with closing(socket.socket(address_family, socket.SOCK_STREAM)) as sock:
7171
sock.bind((address, port))
72-
except OverflowError:
73-
return False
74-
except PermissionError:
75-
return False
76-
except OSError:
72+
except (OverflowError, PermissionError, OSError) as e:
73+
console.warn(f"Unable to bind to {address}:{port} due to: {e}.")
7774
return False
7875
return True
7976

@@ -94,31 +91,7 @@ def is_process_on_port(port: int) -> bool:
9491
) # Test IPv6 localhost (::1)
9592

9693

97-
def change_port(port: int, _type: str) -> int:
98-
"""Change the port.
99-
100-
Args:
101-
port: The port.
102-
_type: The type of the port.
103-
104-
Returns:
105-
The new port.
106-
107-
Raises:
108-
Exit: If the port is invalid or if the new port is occupied.
109-
"""
110-
new_port = port + 1
111-
if new_port < 0 or new_port > 65535:
112-
console.error(
113-
f"The {_type} port: {port} is invalid. It must be between 0 and 65535."
114-
)
115-
raise click.exceptions.Exit(1)
116-
if is_process_on_port(new_port):
117-
return change_port(new_port, _type)
118-
console.info(
119-
f"The {_type} will run on port [bold underline]{new_port}[/bold underline]."
120-
)
121-
return new_port
94+
MAXIMUM_PORT = 2**16 - 1
12295

12396

12497
def handle_port(service_name: str, port: int, auto_increment: bool) -> int:
@@ -137,13 +110,28 @@ def handle_port(service_name: str, port: int, auto_increment: bool) -> int:
137110
Exit:when the port is in use.
138111
"""
139112
console.debug(f"Checking if {service_name.capitalize()} port: {port} is in use.")
113+
140114
if not is_process_on_port(port):
141115
console.debug(f"{service_name.capitalize()} port: {port} is not in use.")
142116
return port
117+
143118
if auto_increment:
144-
return change_port(port, service_name)
145-
console.error(f"{service_name.capitalize()} port: {port} is already in use.")
146-
raise click.exceptions.Exit
119+
for new_port in range(port + 1, MAXIMUM_PORT + 1):
120+
if not is_process_on_port(new_port):
121+
console.info(
122+
f"The {service_name} will run on port [bold underline]{new_port}[/bold underline]."
123+
)
124+
return new_port
125+
console.debug(
126+
f"{service_name.capitalize()} port: {new_port} is already in use."
127+
)
128+
129+
# If we reach here, it means we couldn't find an available port.
130+
console.error(f"Unable to find an available port for {service_name}")
131+
else:
132+
console.error(f"{service_name.capitalize()} port: {port} is already in use.")
133+
134+
raise click.exceptions.Exit(1)
147135

148136

149137
@overload

0 commit comments

Comments
 (0)