Skip to content

fix(server): notify characteristics on client disconnect#393

Open
m-mcgowan wants to merge 1 commit intoh2zero:masterfrom
m-mcgowan:fix/notify-characteristics-on-disconnect
Open

fix(server): notify characteristics on client disconnect#393
m-mcgowan wants to merge 1 commit intoh2zero:masterfrom
m-mcgowan:fix/notify-characteristics-on-disconnect

Conversation

@m-mcgowan
Copy link

@m-mcgowan m-mcgowan commented Feb 1, 2026

Summary

When a BLE client disconnects, characteristics with active subscriptions are not notified. This leaves applications with stale subscription state, causing issues with reconnection.

This PR adds code to the BLE_GAP_EVENT_DISCONNECT handler to iterate through all characteristics and call processSubRequest(peerInfo, 0) for any that had active subscriptions from the disconnecting client.

Problem

Applications using wrappers like NuS-NimBLE-Serial track subscriber counts via the onSubscribe callback. Without this fix:

  1. Client connects and subscribes → onSubscribe(subValue=1) called → count = 1
  2. Client disconnects abruptly → no callback → count stays at 1
  3. Client reconnects → subscription state is stale → reconnection fails

Solution

In BLE_GAP_EVENT_DISCONNECT, before calling onDisconnect:

  • Iterate through all services and characteristics
  • For each characteristic with an active subscription from the disconnecting client
  • Call processSubRequest(peerInfo, 0) to trigger the onSubscribe callback with subValue=0

This ensures:

  1. The onSubscribe callback is called with subValue=0
  2. Applications tracking subscriber counts get properly notified
  3. Subscription state is cleaned up for reliable reconnection

Testing

Tested with ESP32-S3 using the ESP-IDF framework (with Arduino as a component) and NuS-NimBLE-Serial. Before the fix, reconnection after disconnect would fail. After the fix, reconnection works reliably.

Note

An equivalent fix has been submitted to NimBLE-Arduino (#1081 in h2zero/NimBLE-Arduino) for Arduino-only builds.

When a client disconnects, iterate through all services and
characteristics and call processSubRequest with subValue=0 for
any that had subscriptions from the disconnecting client.

This ensures that application callbacks (onSubscribe) are properly
notified when a client disconnects, allowing them to clean up their
subscription state. Previously, the onSubscribe callback was only
called when the client explicitly unsubscribed by writing to the CCCD,
not when the connection was terminated.

This fixes issues where:
- Subscriber counts would not decrement on disconnect
- Blocking reads would hang forever waiting for unsubscribe notification
- Applications could not detect client disconnection via characteristic callbacks
@m-mcgowan m-mcgowan marked this pull request as ready for review February 1, 2026 00:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant