feat: add per-adapter cross-process file locking for BLE serialization#222
Closed
cgoudie wants to merge 2 commits intoBluetooth-Devices:mainfrom
Closed
feat: add per-adapter cross-process file locking for BLE serialization#222cgoudie wants to merge 2 commits intoBluetooth-Devices:mainfrom
cgoudie wants to merge 2 commits intoBluetooth-Devices:mainfrom
Conversation
On multi-service systems (e.g. Venus OS / Cerbo GX) several processes may compete for the same BLE adapter, causing InProgress errors on ~40% of connection attempts. This adds optional per-adapter file locking via fcntl.flock to serialize BLE operations across processes. New components: - LockConfig dataclass (const.py): configures lock directory, template, and timeout. Per-adapter lock files allow full parallelism across adapters while preventing InProgress from concurrent scans on the same adapter. - lock.py: acquire_lock() / release_lock() helpers using non-blocking fcntl.flock with async retry. Graceful degradation on timeout or missing directory. - establish_connection() gains lock_config and in_process_semaphore parameters. Lock is acquired before each connection attempt and released in a finally block, allowing other processes to interleave between retries. Key design decisions: - Opt-in via LockConfig(enabled=True) — existing callers unaffected - Per-adapter, not global — hci0 and hci1 are independent radios - Non-blocking flock with asyncio.sleep retry — never blocks event loop - Graceful degradation on timeout — prevents deadlock - fcntl.flock auto-releases on fd close / process exit - in_process_semaphore for same-process serialization (asyncio tasks) Co-authored-by: Cursor <cursoragent@cursor.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #222 +/- ##
==========================================
+ Coverage 79.64% 81.10% +1.45%
==========================================
Files 6 7 +1
Lines 570 635 +65
Branches 112 118 +6
==========================================
+ Hits 454 515 +61
- Misses 70 74 +4
Partials 46 46 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
/run is the standard location for runtime state files — cleared on reboot so stale locks cannot survive, and not subject to tmpwatch or noexec mount concerns that affect /tmp. Co-authored-by: Cursor <cursoragent@cursor.com>
Member
|
Closing this. Please do not open PRs without first opening an issue to discuss the problem you're trying to solve and the proposed approach. These PRs appear to be bulk AI-generated without understanding the project's architecture or real-world usage. Before contributing, please:
|
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.
Summary
LockConfigdataclass for configuring per-adapter file locks that serialize BLE operations across processeslock.pymodule with async-safeacquire_lock()/release_lock()using non-blockingfcntl.flocklock_configandin_process_semaphoreparameters toestablish_connection()for cross-process and in-process serializationfinallyblock, allowing other processes to interleave between retriesProblem
On multi-service systems (e.g. Venus OS / Cerbo GX), several processes compete for the same BLE adapter simultaneously:
dbus-serialbattery(1 per BLE battery)dbus-power-watchdogdbus-shyion-switchdbus-ble-sensors(Victron)vesmart-server(Victron)Without coordination, 3-5 processes compete for a single BlueZ adapter, producing
org.bluez.Error.InProgresserrors on ~40% of connection attempts.Design decisions
hci0andhci1are independent radios. A global lock unnecessarily serializes operations that could run in parallel on different adaptersLockConfig(enabled=True))LOCK_NB+asyncio.sleepretry)LOCK_EXwould freeze the asyncio event looplock_timeoutseconds to prevent deadlock if a lock holder crashesfcntl.flockauto-releasein_process_semaphoreparameterfcntl.flockis per-process, not per-thread. Multiple asyncio tasks in the same process need anasyncio.Semaphorefor serialization. Both can be used togetherclient.connect(), not during backoff. Other processes can interleave between retry attemptsStuck states this addresses
Usage
Test plan
test_lock_config_path_for_adapter— correct path generation per adaptertest_lock_config_custom_template— custom lock templates worktest_acquire_release_lock— basic acquire/release cycletest_acquire_lock_disabled— no lock when config disabledtest_acquire_lock_no_fcntl— graceful degradation without fcntltest_release_lock_none— safe no-op on Nonetest_lock_contention_timeout— second acquirer times outtest_lock_released_after_holder_closes— re-acquire after releasetest_lock_per_adapter_independent— hci0 and hci1 locks are independenttest_lock_bad_directory— graceful degradation on missing directorytest_establish_connection_with_lock_config— lock acquired/released around connectiontest_establish_connection_with_semaphore— semaphore acquired/releasedtest_establish_connection_lock_released_on_failure— lock+semaphore released on errortest_establish_connection_no_lock_by_default— no lock when not configuredMade with Cursor