Skip to content

follow-up: harden RP2350W BLE notification lifecycle from #3863 #3955

Open

Description

@zackees

Source PR: #3863
Related implementation issue: #3861

Source comments:

Why this matters: the RP2350W transport can corrupt or strand JSON-RPC notifications when responses overlap or a connection drops, and the HIL reader can miss a later valid notification.

CodeRabbit suggestions (verbatim):

Handle each BLE notification independently.

read_lines() yields one notification value per line, but this loop accumulates them. If one complete JSON-RPC line arrives and a later json.loads() cannot parse because the next value appends to the previous line, keep parsing completed lines instead of treating notification_payload as a shared fragment buffer.

Queue complete responses before notification scheduling.

response_sink clears and replaces the single notification_payload while an earlier response can still have unsent chunks. If two RPC responses are produced before the first transfer completes, the client receives a truncated first response and loses its remaining bytes.

Add an outbound FIFO. Advance to the next response only after all chunks of the current response are sent. Add a regression test that produces two responses before the send-ready callback completes.

Reset notification registration state on disconnect.

onDisconnected still leaves state->notification_scheduled and runtime.notification_callback_pending set. If a request registers last and the connection drops before ATT_EVENT_CAN_SEND_NOW, new writes can be blocked and a disconnected callback can hit cleared notification_payload. Set both flags false when disconnecting while a pending registration exists.

Acceptance criteria:

  • RED: add a focused transport test that queues two complete responses before the first BTstack send-ready callback finishes and demonstrates that current code truncates/replaces the first response.
  • GREEN: introduce a bounded outbound FIFO, preserve response order, and advance only after the final ATT-MTU-sized chunk is accepted.
  • RED: add a focused disconnect/reconnect test with a pending notification registration that demonstrates stale pending state blocks the next connection or accesses cleared payload state.
  • GREEN: make disconnect/destroy lifecycle generation-safe, clear application pending state without reusing a still-owned BTstack registration unsafely, and prove a subsequent write/notification succeeds.
  • RED: add a focused Python test with multiple independent notification values where a valid pong follows another complete or malformed value.
  • GREEN: parse each complete notification independently while retaining correct ATT-fragment reassembly at the BleInterface boundary.
  • Preserve singleton-owned RP runtime state, bounded queue-full behavior, ESP32 NimBLE behavior, and unsupported-platform stubs.
  • Run focused BLE tests, strict lint, RP2350W and ESP32-C6 target builds, then record live GATT discovery/ping plus disconnect/reconnect evidence on the Pico 2 W.

Activity

  1. zackees commented on Aug 21, 2026

    @zackees
    MemberAuthor

    Landed via #3958 (9655e7abd9). All three CodeRabbit findings are fixed, plus several defects surfaced by review of the first pass.

    Done

    • Outbound FIFO — BleNotifyQueue (src/fl/net/ble_notify_queue.h) keeps completed responses in order and advances only after the final ATT-MTU chunk is accepted, replacing the single shared buffer that truncated an in-flight response. Bounded at 4; a full queue refuses the newest response rather than evicting one already partly on the wire.
    • Disconnect/destroy lifecycle — onDisconnected now clears notification_scheduled and notification_callback_pending; destroyTransport clears the pending flag too. A failed att_server_notify re-arms instead of stranding the response, with bounded retries so a permanent error (CCC not enabled, bad handle) can't spin forever; undeliverable payloads are dropped so the queue keeps moving.
    • Independent notification parsing — BleNotificationAssembler (ci/util/ble_notify_assembler.py) consumes exactly the bytes of each complete JSON value. It resynchronizes only at a fragment start, never by searching the buffer, so a payload that legitimately embeds the literal REMOTE: can't be chopped in two.
    • ATT-fragment reassembly preserved — BleInterface no longer .strip()s each notification (which dropped whitespace landing on a chunk boundary) and uses an incremental UTF-8 decoder so a multi-byte sequence split across two notifications no longer decodes to U+FFFD on both sides.
    • Thread-safety: the queue is a single-producer/single-consumer ring, so push() (RPC pump) and advance() (BTstack callback) never write the same variable — matching the RX ring already in that transport.
    • ESP32 NimBLE behavior, unsupported-platform stubs, and singleton-owned RP runtime state are untouched.

    Verification: 282/282 host unit tests (9 queue cases), 13 assembler cases, bash lint clean, rp2350w + esp32c6 builds green. The [BLE RP] TX queue full and dropping undeliverable response strings are present in the linked rp2350w firmware, confirming the modified transport is compiled in rather than gated out.

    Still open — why this issue is not closed

    The final acceptance bullet asks for live GATT discovery/ping plus disconnect/reconnect evidence on the Pico 2 W. No RP2350W was attached to the bench, so that evidence was not collected and every fix above is verified by host tests and target builds only. Leaving this open for that HIL pass.

  2. zackees commented on Sep 7, 2026

    @zackees
    MemberAuthor

    The hardware clause on this issue is now unblocked.

    The bench fixture is confirmed a Raspberry Pi Pico 2 W — live evidence, not the historical record:

    2e8a:f00f  'Pico 2W'                                  # rpipico2w application CDC (2E8A:F00F)
    status     -> platform: "Raspberry Pi Pico 2 W (RP2350)"
    wifiStatus -> {"status":"IDLE","success":true}        # UNSUPPORTED on the non-W build
    

    Until now the variant was unestablished, so the W-only acceptance criteria here could not be attempted at all. They can be run now:

    • bash autoresearch rp2350w --rpc-smoke — PASS (71 RPC methods, dual-core concurrency 200/200)
    • bash autoresearch rp2350w --net-peer — deploys both boards; six full cycles of wifiConnect → join the C6 SoftAP → startNetServer → bidirectional HTTP → stopNet → ping

    Deploying the W build onto a board running non-W firmware needs --upload-port — fbuild refuses to guess which board to BOOTSEL and fails clean without flashing.

    One known blocker for long soaks: #4173 — the RP2350W hangs in wifiConnect on the 7th consecutive peer cycle and watchdog-resets. It self-recovers, so it limits repetition rather than stranding the fixture.

    Generated with Claude Code

    https://claude.ai/code/session_01KkufoNxfnNRU9psT3R9F51

  3. zackees commented on Sep 11, 2026

    @zackees
    MemberAuthor

    Partial: the GATT server and TX characteristic exist; the notification lifecycle still cannot be exercised here

    This issue is about hardening the notification lifecycle, which needs a subscribed central. Recording what I could and could not establish on the attached RP2350W (2DCB876B587EA334), so the boundary is precise rather than "BLE blocked".

    Driving the device directly over serial RPC — no host BLE stack involved — the server side is real and repeatable (detail on #3861):

    startBle: success, service 12345678-…-789abc, tx 12345678-…-789ab1, rx …789ab0
    bleStatus: ble_active false -> true, tx_char_exists false -> true
    stopBle:  success
    

    So the pieces this issue's lifecycle depends on — a live GATT server with the TX characteristic registered — are confirmed present, and bleStatus already reports the state a lifecycle test would assert against: connected, connected_count, tx_value_len, ring_head, ring_tail.

    But every one of those is at its zero value, because nothing ever subscribes: connected: false, connected_count: 0, tx_value_len: 0, ring_head: 0, ring_tail: 0. The notification path is therefore entirely unexercised — the ring buffer indices in particular are exactly what a hardening test would need to watch move.

    Blocked on bench provisioning rather than firmware: hci0 is present but bluetooth.service does not exist on this host, so BlueZ userspace is absent and bleak cannot act as a central. Needs root to install and enable; not something to do unattended. Recorded on #3714.

    Worth noting for whoever picks this up: bleStatus returning ring_head/ring_tail/tx_value_len means a notification-lifecycle test does not need a bespoke device build — the observability is already in the firmware. It only needs a host that can subscribe.

  4. zackees commented on Sep 12, 2026

    @zackees
    MemberAuthor

    Status check before anyone reopens this: all three CodeRabbit suggestions are implemented, with the focused tests the acceptance criteria ask for. What is left is the final HIL bullet, and I re-confirmed today that it is still blocked on bench provisioning rather than on firmware.

    What is already in

    suggestion implementation test
    Queue complete responses before notification scheduling src/fl/net/ble_notify_queue.h — bounded outbound FIFO, single-producer/single-consumer, advances only after the final chunk is accepted; used as TransportState::notifications in ble_rp.cpp.hpp tests/fl/net/ble_notify_queue.cpp
    Reset notification registration state on disconnect ble_rp.cpp.hpp onDisconnected() clears notifications, notify_failures, notification_scheduled and runtime.notification_callback_pending; destroyTransport() nulls the registration context and clears the pending flag ci/tests/test_rp2350w_board_config.py
    Handle each BLE notification independently ci/util/ble_notify_assembler.py — BleNotificationAssembler yields each complete JSON value and reassembles ATT fragments per message; ci/autoresearch/ble.py uses it instead of a shared fragment buffer ci/tests/test_ble_notification_assembler.py, 13 cases

    The Python cases cover the specific ones named in the acceptance criteria — test_two_responses_in_one_stream_are_independent, test_valid_pong_after_malformed_value, test_valid_pong_after_unrelated_complete_value, test_partial_value_is_held_not_dropped, test_runaway_garbage_is_bounded.

    The remaining bullet, and why it is still blocked

    record live GATT discovery/ping plus disconnect/reconnect evidence on the Pico 2 W

    Re-checked on this host today, unchanged from the earlier note:

    systemctl status bluetooth   -> Unit bluetooth.service could not be found.
    which bluetoothctl hciconfig -> neither present
    

    bleak is installed, but with no BlueZ userspace it has no adapter to drive, so the host cannot act as a central. Installing and enabling BlueZ needs root — not something to do unattended.

    And the workaround that does not exist

    Worth writing down so nobody spends time on it: the attached ESP32-C6 cannot stand in for the missing central. FastLED's BLE surface is peripheral-only —

    // src/fl/net/ble.h — the entire API
    TransportState* createTransport(const char* deviceName);
    void            destroyTransport(TransportState* state);
    StatusInfo      queryStatus(const TransportState* state);
    fl::pair<...>   getTransportCallbacks(TransportState* state);

    There is no scan, connect, discover or subscribe. Using the C6 as the central would mean building a BLE central role first, which is #3861-scale work and not what this issue is about. The two-MCU pattern that unblocked the network side (--net-peer) has no BLE equivalent to reach for.

    So the boundary is exact: firmware and host-side parsing are done and tested; the evidence bullet needs a host with BlueZ, or a central role that does not exist yet. Everything else on the acceptance list is satisfied by the artifacts above.

    As noted before, bleStatus already reports connected, connected_count, tx_value_len, ring_head and ring_tail, so whoever gets a central attached needs no bespoke device build — only a subscriber.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions