refactor(deadline): move two more wait loops onto poll_until

Review of the stack found two more copies of the check-until-deadline
loop in files it already touched: gateway._wait_for_tcp_port_free and
voice_mode._sd_play_blocking. Same shape, same fix. The TCP wait now
also checks once more at the deadline; the playback wait already
re-checked after every sleep, so for it only the last sleep is cut to
the time left.

gateway_migrate._wait_for_served is left as is: it returns the last
partial value on timeout, which poll_until does not.
This commit is contained in:
kshitijk4poor authored and kshitij committed 2026-10-27 16:30:14 +05:30
1 parent 18ee986b6f
commit a4baee29de
2 files changed
+8 -9

No files matched your search

+5 -6
View File
@@ -4008,19 +4008,18 @@ def _wait_for_tcp_port_free(host: str, port: int, *, timeout: float = 10.0) -> b
the race logs EADDRINUSE and keeps running with no API. Connection-refused means the
listener is gone; a timed-out connect is a live listener with a slow accept queue.
"""
deadline = time.monotonic() + timeout
while time.monotonic() < deadline:
def _port_free() -> bool:
try:
with socket.create_connection((host, port), timeout=0.2):
pass
return False
except ConnectionRefusedError:
return True
except TimeoutError:
pass # a slow accept queue is still a live listener
return False # a slow accept queue is still a live listener
except OSError:
return True # unresolvable/unreachable address: nothing to wait for; the bind retry covers it
time.sleep(0.1)
return False
return poll_until(_port_free, timeout, 0.1)
def _wait_for_api_server_port_free(*, timeout: float = 10.0) -> bool:
+3 -3
View File
@@ -371,9 +371,9 @@ def _sd_play_blocking(sd, audio, sample_rate: int, *, timeout: float, blocksize:
"""``sd.play`` then poll until idle or *timeout* (``sd.wait()`` has no timeout and
hangs forever if the device stalls)."""
sd.play(audio, samplerate=sample_rate, blocksize=blocksize)
deadline = time.monotonic() + timeout
while sd.get_stream() and sd.get_stream().active and time.monotonic() < deadline:
time.sleep(0.01)
from agent.deadline import poll_until
poll_until(lambda: not (sd.get_stream() and sd.get_stream().active), timeout, 0.01)
sd.stop()