aioble/server: Support singleton re-registration across restart cycles#1082
Open
andrewleech wants to merge 1 commit intomicropython:masterfrom
Open
aioble/server: Support singleton re-registration across restart cycles#1082andrewleech wants to merge 1 commit intomicropython:masterfrom
andrewleech wants to merge 1 commit intomicropython:masterfrom
Conversation
6b70880 to
6cafc0e
Compare
6cafc0e to
87bc782
Compare
40e70da to
99cd4b6
Compare
Enable a pattern where Service and Characteristic objects are created once and re-registered via register_services() after each BLE radio restart, avoiding per-cycle heap allocations on constrained targets. Fix _register() consuming initial= values after first registration. Fix _server_shutdown() not waking tasks blocked in written(). Fix _init_capture() guard mismatch with _server_shutdown() preventing capture infrastructure rebuild after shutdown. Add register_services() auto-rebuild of capture infrastructure. Add ble_reregister multitest covering 3 stop/start cycles. Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
99cd4b6 to
7c22e42
Compare
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
The current aioble pattern requires creating new
ServiceandCharacteristicobjects for each BLE restart cycle. On memory-constrained targets (e.g. STM32WB55 with ~100KB heap), this repeated allocation and deallocation fragments the heap over time, eventually leading toMemoryErrorduring long-running operation.This PR enables a singleton pattern where service and characteristic objects are created once at boot and re-registered via
register_services()after each radio restart. This eliminates per-cycle GATT allocations and keeps the heap stable across BLE lifecycle events.Three issues in
server.pycurrently prevent this pattern from working:sequenceDiagram participant App participant aioble participant BLE Radio Note over App: Create services + characteristics once loop Each BLE restart cycle App->>aioble: register_services(*services) Note over aioble: _register() writes initial= values<br/>to GATT database aioble->>BLE Radio: gatts_register_services() App->>aioble: advertise() Note over App,BLE Radio: Connection / data exchange App->>aioble: aioble.stop() Note over aioble: _server_shutdown() rect rgb(255, 230, 230) Note over aioble: Before this PR:<br/>• initial= values consumed on first cycle<br/>• written() callers blocked forever<br/>• capture task destroyed, never rebuilt end rect rgb(230, 255, 230) Note over aioble: After this PR:<br/>• initial= values preserved for next cycle<br/>• written() callers get DeviceDisconnectedError<br/>• capture infrastructure auto-rebuilt end endIssues fixed
1.
initial=values consumed after first registration_register()writes theinitialvalue to the GATT database then setsself._initial = None. When the same object is re-registered on the next cycle, the value is gone — reads return empty bytes.Fix: don't clear
_initialafter writing. The value is idempotently re-written on each registration.2.
written()callers block forever after shutdown_server_shutdown()clears the characteristic registry but never wakes tasks blocked onawait characteristic.written(). With singleton objects these tasks persist across cycles, holding memory and never completing.Fix: before clearing the registry, iterate all registered characteristics, set their write events (to wake blocked callers), and invalidate their value handles. After waking,
written()detects the invalidated handle and raisesDeviceDisconnectedError, giving callers a clean signal to exit or loop back.3. Capture infrastructure destroyed without rebuild
_server_shutdown()deletes the capture task, queue, and events. On the next cycle,_init_capture()is never called becauseCharacteristic.__init__()only runs at object creation time — which already happened on cycle 1.Fix:
register_services()scans for capture-enabled characteristics after registration and calls_init_capture()if needed.Testing
ble_reregister.pymultitest exercising 3 stop/start cycles with singleton services, verifyinginitial=values persist and writable characteristics accept new values after each re-registration.