Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions reflex/utils/processes.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def get_num_workers() -> int:
return (os.cpu_count() or 1) * 2 + 1


def _is_address_responsive(
def _can_bind_at_port(
address_family: socket.AddressFamily | int, address: str, port: int
) -> bool:
"""Check if a given address and port are responsive.
Expand All @@ -68,9 +68,12 @@ def _is_address_responsive(
"""
try:
with closing(socket.socket(address_family, socket.SOCK_STREAM)) as sock:
return sock.connect_ex((address, port)) == 0
sock.bind((address, port))
except PermissionError:
return False
except OSError:
return False
return True


def is_process_on_port(port: int) -> bool:
Expand All @@ -82,9 +85,9 @@ def is_process_on_port(port: int) -> bool:
Returns:
Whether a process is running on the given port.
"""
return _is_address_responsive( # Test IPv4 localhost (127.0.0.1)
return not _can_bind_at_port( # Test IPv4 localhost (127.0.0.1)
socket.AF_INET, "127.0.0.1", port
) or _is_address_responsive(
) or not _can_bind_at_port(
socket.AF_INET6, "::1", port
) # Test IPv6 localhost (::1)

Expand Down