Clean up WebRTCSession resources after startup failures - #2808
Open
voropaevv wants to merge 1 commit into
Open
Conversation
Ensure failed startup attempts enter a terminal state and reuse the existing single-flight teardown path. Track the loop-owned startup task so cleanup waits for it to stop before reading partial resources. Publish peer connections as soon as they are created, close the event loop in its owner thread, and preserve the original startup error. Cover pre- and post-peer failures, interruptions, concurrent callers, scheduler failures, pending tasks, and teardown failures.
voropaevv
force-pushed
the
codex/fix-webrtc-startup-cleanup
branch
from
August 16, 2026 13:49
dbb7c18 to
304308a
Compare
voropaevv
marked this pull request as ready for review
August 16, 2026 14:29
voropaevv
requested review from
PawelPeczek-Roboflow,
dkosowski87,
grzegorz-roboflow,
hansent,
probicheaux,
rafel-roboflow and
yeldarby
as code owners
August 16, 2026 14:29
Collaborator
|
Hi there |
1 similar comment
Collaborator
|
Hi there |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
A failed first connection attempt left
WebRTCSessionin theSTARTEDstate while its owned asyncio loop and daemon thread remained alive. Failures afterRTCPeerConnectionconstruction could also leak the partial peer becauseself._pcwas assigned only after later setup and signaling completed.There was an additional interruption race in the first draft: canceling the
concurrent.futures.Futurereturned byrun_coroutine_threadsafe()only requests cancellation on the event loop. Teardown could readself._pcbefore the startup task reached a terminal state, then startup could publish a peer afterward.Fixes #2807.
What changed
CLOSEDstate before releasing the state lockclose()path after releasing that lockself._pcimmediately after peer construction so partial initialization remains cleanableTask, not only its concurrent futureNo public API, retry semantics, callback behavior, queue behavior, or server-side protocol behavior changed.
Verification
STARTED, with an open/running loop, a live loop thread, and no source cleanuppeer.close()uncalleddbb7c1845d90f2ac316bd0384dd47894ea76b5fd:run_coroutine_threadsafe()future requested cancellation before teardownself._pcpeer.close_calls == 0PYTHONASYNCIODEBUG=1 PYTHONPATH="$PWD" python -m pytest tests/inference_sdk/unit_tests/webrtc/test_session_lifecycle.py -q -W error::ResourceWarning -W error::RuntimeWarningPYTHONASYNCIODEBUG=1 PYTHONPATH="$PWD" python -m pytest tests/inference_sdk/unit_tests/webrtc -q -W error::ResourceWarning -W error::RuntimeWarningPYTHONPATH="$PWD" python -m pytest tests/inference_sdk/unit_tests -qmake check_code_qualitygit diff --checkpassedpeer.close_calls == 0Not verified
Risks / rollback
The change is limited to client-side WebRTC lifecycle ownership. Cleanup now waits for the loop-owned startup task to terminate before inspecting partial resources; successful startup observes an already-completed task, so normal close does not add a blocking startup wait.
Deterministic tests cover failures before and after peer creation, concurrent callers, an external interruption with real cross-thread future semantics, publication during cancellation, scheduling failures, pending tasks, cleanup failures, and successful startup.
Reverting commit
304308af0ce32af7123d9d6bc39aa1f8abec3cccrestores the previous behavior.Migrations / external effects
None.
Screenshots / preview
Not applicable; this changes internal SDK lifecycle behavior.