-
Notifications
You must be signed in to change notification settings - Fork 141
fix: add 30s timeout to WebRTC connection_ready.wait() to prevent indefinite hang #1446
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -111,10 +111,46 @@ def start_background_loop() -> None: | |
| self.task = self.loop.create_task(async_connect()) | ||
| self.loop.run_forever() | ||
|
|
||
| CONNECT_TIMEOUT = 30 # seconds | ||
|
|
||
| self.loop = asyncio.new_event_loop() | ||
| self.thread = threading.Thread(target=start_background_loop, daemon=True) | ||
| self.thread.start() | ||
| self.connection_ready.wait() | ||
|
|
||
| if not self.connection_ready.wait(timeout=CONNECT_TIMEOUT): | ||
| # Cancel the async connection task and disconnect to avoid leaving | ||
| # a half-open WebRTC session on the robot side (which would cause | ||
| # the next connection attempt to hang as well). | ||
| async def _cleanup() -> None: | ||
| if self.task is not None: | ||
| self.task.cancel() | ||
| try: | ||
| await self.task | ||
| except (asyncio.CancelledError, Exception): | ||
| pass | ||
| if hasattr(self, "conn") and self.conn is not None: | ||
| try: | ||
| await self.conn.disconnect() | ||
| except Exception: | ||
| pass | ||
|
|
||
| if self.loop.is_running(): | ||
| future = asyncio.run_coroutine_threadsafe(_cleanup(), self.loop) | ||
| try: | ||
| future.result(timeout=5.0) | ||
| except Exception: | ||
| pass | ||
| self.loop.call_soon_threadsafe(self.loop.stop) | ||
| if self.thread.is_alive(): | ||
| self.thread.join(timeout=2.0) | ||
|
Comment on lines
+144
to
+145
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Background thread may still be alive when
If a caller catches the if self.thread.is_alive():
self.thread.join(timeout=2.0)
if self.thread.is_alive():
import warnings
warnings.warn(
"Background WebRTC thread did not stop within 2s after timeout cleanup.",
RuntimeWarning,
stacklevel=2,
) |
||
| raise TimeoutError( | ||
| f"WebRTC connection to {self.ip} timed out after {CONNECT_TIMEOUT}s. " | ||
| "Common causes:\n" | ||
| " - Another WebRTC client is connected (close the Unitree mobile app)\n" | ||
| " - Robot is unreachable on the network\n" | ||
| " - Port 9991 (encrypted SDP) is not responding\n" | ||
| "Tip: only one WebRTC client can connect to the Go2 at a time." | ||
| ) | ||
|
Comment on lines
+120
to
+153
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing When the timeout fires, the cleanup stops the event loop but never cancels the pending Without calling The cleanup block should mirror the pattern used in if not self.connection_ready.wait(timeout=CONNECT_TIMEOUT):
if self.loop.is_running():
if self.task is not None:
self.loop.call_soon_threadsafe(self.task.cancel)
async def async_disconnect() -> None:
try:
await self.conn.disconnect()
except Exception:
pass
asyncio.run_coroutine_threadsafe(async_disconnect(), self.loop)
self.loop.call_soon_threadsafe(self.loop.stop)
if self.thread.is_alive():
self.thread.join(timeout=2.0)
raise TimeoutError(...) |
||
|
|
||
| def start(self) -> None: | ||
| pass | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CONNECT_TIMEOUTshould be a class-level attributeDefining the timeout as a method-local constant makes it invisible at the class API level and impossible for callers to customise without patching the source. Users on slower or congested networks might need a longer window; users in CI want a shorter one.
A class attribute is both more discoverable and overridable:
Or better, promote it to the class body:
and reference it as
self.CONNECT_TIMEOUTinsideconnect().