@@ -53,7 +53,7 @@ def get_num_workers() -> int:
5353 return (os .cpu_count () or 1 ) * 2 + 1
5454
5555
56- def _is_address_responsive (
56+ def _can_bind_at_port (
5757 address_family : socket .AddressFamily | int , address : str , port : int
5858) -> bool :
5959 """Check if a given address and port are responsive.
@@ -68,9 +68,12 @@ def _is_address_responsive(
6868 """
6969 try :
7070 with closing (socket .socket (address_family , socket .SOCK_STREAM )) as sock :
71- return sock .connect_ex ((address , port )) == 0
71+ sock .bind ((address , port ))
72+ except PermissionError :
73+ return False
7274 except OSError :
7375 return False
76+ return True
7477
7578
7679def is_process_on_port (port : int ) -> bool :
@@ -82,9 +85,9 @@ def is_process_on_port(port: int) -> bool:
8285 Returns:
8386 Whether a process is running on the given port.
8487 """
85- return _is_address_responsive ( # Test IPv4 localhost (127.0.0.1)
88+ return not _can_bind_at_port ( # Test IPv4 localhost (127.0.0.1)
8689 socket .AF_INET , "127.0.0.1" , port
87- ) or _is_address_responsive (
90+ ) or not _can_bind_at_port (
8891 socket .AF_INET6 , "::1" , port
8992 ) # Test IPv6 localhost (::1)
9093
0 commit comments