Skip to content

Commit f0e2a81

Browse files
author
maebahesioru
committed
fix: early exit on EADDRINUSE, improve free port detection
- Break immediately on EADDRINUSE instead of waiting full 90s timeout - Use 127.0.0.1 bind and SO_REUSEADDR=0 for more accurate port availability check - Increase max_tries to 20 to skip wider reserved port ranges
1 parent 13e812e commit f0e2a81

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

src/launch_camoufox.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -719,15 +719,16 @@ def determine_proxy_configuration(internal_camoufox_proxy_arg=None):
719719
camoufox_popen_kwargs['creationflags'] = subprocess.CREATE_NO_WINDOW
720720
try:
721721
logger.info(f" 将执行 Camoufox 内部启动命令: {' '.join(camoufox_internal_cmd_args)}")
722-
def _find_free_port(start_port: int, max_tries: int = 10) -> int:
722+
def _find_free_port(start_port: int, max_tries: int = 20) -> int:
723723
for p in range(start_port, start_port + max_tries):
724724
try:
725725
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
726-
s.bind(('', p))
726+
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 0)
727+
s.bind(('127.0.0.1', p))
727728
return p
728729
except OSError:
729730
continue
730-
return start_port
731+
return start_port + max_tries
731732
MAX_CAMOUFOX_RETRIES = 3
732733
for camoufox_attempt in range(MAX_CAMOUFOX_RETRIES):
733734
# Always find a free port (avoids EADDRINUSE on first and retry attempts)
@@ -767,6 +768,10 @@ def _find_free_port(start_port: int, max_tries: int = 10) -> int:
767768
logger.warning(log_line_content)
768769
else:
769770
logger.info(log_line_content)
771+
# Early exit on port conflict - no need to wait full timeout
772+
if 'EADDRINUSE' in line_from_camoufox:
773+
logger.warning(f' ⚡ 检测到端口冲突,立即终止并重试...')
774+
break
770775
ws_match = ws_regex.search(line_from_camoufox)
771776
if ws_match:
772777
captured_ws_endpoint = ws_match.group(1)

0 commit comments

Comments
 (0)