feat(protocol): fixed DTLS source port so reconnects evict orphaned sessions

Root-cause fix for the stale-session stall on always-on appliances (#14).
When the client dies without close_notify (crash, SIGKILL), the device
keeps an orphaned DTLS association keyed to the old 5-tuple; a reconnect
from a fresh ephemeral port presents as a brand-new peer, so the orphan
lingers until the device's own timer reaps it (observed 5-15 min).

RFC 6347 §4.2.8 covers exactly this: a ClientHello arriving on an existing
association's 5-tuple means the peer rebooted, and the server must complete
the new handshake and discard the old association. Add an optional
local_port to DtlsCoapSession that binds the UDP source port, and have the
bridge bind base+appliance-index, so every reconnect re-handshakes over the
same 5-tuple and the orphan is evicted instead of waited out.

Bench-verified on live hardware (2026-07-26): RT-OCF accepts the
same-5-tuple rehandshake (oven, dryer: handshake completes over a
crash-orphaned association, reads work immediately). The oven does not
reproduce the fridge stall even with 11 crash-orphaned OBSERVE
registrations, so fridge-side confirmation of the eviction is still needed.

Co-authored-by: vmvarga <garrysuchiy@gmail.com>
This commit is contained in:
Jack Nagyandvmvarga committed 2026-07-26 20:53:56 +01:00
1 parent 6653de3b2c
commit 8c2108a510
2 files changed
+29 -1

No files matched your search

+10
View File
@@ -60,6 +60,15 @@ UNREACHABLE_RECONNECT_S = 120.0
# session.
OBSERVE_REFRESH_INTERVAL_S = 6 * 3600.0
# Base for the fixed DTLS source port; each appliance binds base+index so
# every reconnect uses the same 5-tuple. If the bridge dies without
# close_notify (crash, SIGKILL), the device holds an orphaned association
# keyed to the old 5-tuple; re-handshaking from the SAME port makes the
# device evict the orphan (RFC 6347 §4.2.8) instead of wedging on it —
# the root cause behind stale sessions on always-on appliances, where the
# orphan otherwise lingers 5-15 min.
DTLS_LOCAL_PORT_BASE = 49700
class PushBridge:
@@ -239,6 +248,7 @@ class PushBridge:
cert_path=self.shared.CERT_PATH,
key_path=self.shared.KEY_PATH,
on_notification=self._on_notification,
local_port=DTLS_LOCAL_PORT_BASE + self.app.index,
)
sess.connect()
self.session = sess