fix(client): serialize create_onvif_service to avoid leaking sessions#294
Draft
bluetoothbot wants to merge 2 commits into
Draft
fix(client): serialize create_onvif_service to avoid leaking sessions#294bluetoothbot wants to merge 2 commits into
bluetoothbot wants to merge 2 commits into
Conversation
Two concurrent create_onvif_service calls for the same binding both miss the services cache and each build an ONVIFService (opening an aiohttp session); the second store overwrites the first, orphaning the loser's session. This test fails against the current code (two services built).
Concurrent create_onvif_service calls for the same binding both missed the services cache and each built an ONVIFService (each opening an aiohttp session); the later store overwrote the earlier one, orphaning the loser's session forever. Guard the check-create-store with a lazily-created per-camera asyncio.Lock and re-check the cache after acquiring it, matching the existing shared-SqliteCache double-checked pattern.
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.
What
Serialize
ONVIFCamera.create_onvif_serviceso concurrent calls for the same binding build exactly one service instead of leaking an aiohttp session.Why
create_onvif_servicechecksself.services, thenawaitsservice.setup()before storing the result. Two coroutines creating the same service concurrently (e.g.create_media_service()fired in parallel during device setup) both miss the cache, both construct anONVIFService— each of which opens an aiohttpClientSession— and the second store overwrites the first. The loser is never placed inself.services, soclose()never reaches it: an orphaned socket leaks for the lifetime of the camera object.Resource-safety fix — untracked aiohttp session per concurrent duplicate create.
How
asyncio.Lock(_services_lock).async with self._services_lock, with the cache re-checked after the lock is held so the second caller returns the winner's instance._get_shared_sqlite_cachepattern already in this module. Lazy init is race-free: noawaitbetween theNonecheck and the assignment.Testing
test_concurrent_create_onvif_service_builds_one_servicedrives two concurrent creates and asserts one service is built and both callers get the same instance. Committed first so it fails against the unpatched code (2 services built), passes after the fix.Quality Report
Changes: 87 files changed, 9619 insertions(+), 1474 deletions(-)
Code scan: 20 issue(s) found
README.rst:61— debug print statementREADME.rst:111— debug print statementexamples/events.py:20— debug print statementexamples/events.py:24— debug print statementexamples/events.py:25— debug print statementexamples/events.py:27— debug print statementexamples/events.py:29— debug print statementexamples/events.py:62— debug print statementexamples/events.py:74— debug print statementexamples/events.py:84— debug print statementTests: failed (FAILED)
Branch hygiene: clean
Generated by Kōan