Skip to content

cancel pending delayed will on reconnect and bound it by session expiry - #170

Merged
fabracht merged 2 commits into
mainfrom
will-delay-cancel
Sep 27, 2026
Merged

fabracht merged 2 commits into
mainfrom
will-delay-cancel

Conversation

@fabracht

Copy link
Copy Markdown
Contributor

Fixes #154.

Problem

With a Will Delay Interval, the broker slept in a detached task and then published the Will unconditionally. So:

  • A client that dropped and reconnected within the delay still got a spurious Will. This violates [MQTT-3.1.3-9] and [MQTT-3.1.2-8].
  • Session end was ignored. The Will must be published when the delay elapses or the session ends, whichever comes first. With Session Expiry 0, or an expiry shorter than the delay, it went out late.
  • A published Will was not removed from the stored session ([MQTT-3.1.2-10]).
  • The conformance test for [MQTT-3.1.3-9] passed vacuously, because it stopped watching before the delay elapsed.

Fix

  • MessageRouter tracks pending delayed Wills per ClientID. Any new connection for that ClientID cancels the pending Will in register_client: resume, Clean Start, or takeover. Claim and cancel happen under the same lock, so exactly one of them wins.
  • The Will fires at min(Will Delay, Session Expiry). A published Will, or one deleted by DISCONNECT 0x00, is removed from stored session state.
  • The wasm broker gets the same fix, using gloo timers rather than tokio. It also now detects a client closing its MessagePort. Before, a closed port went unnoticed until keep-alive expiry, and never with keep-alive 0, which leaked the handler.

Tests

  • crates/mqtt5/tests/will_delay.rs: 11 tests. The resume, Clean Start, expiry-0, expiry-shorter, reconnect-then-drop and takeover cases fail on the previous code.
  • crates/mqtt5-wasm/tests/broker_will_delay.rs plus wasm unit tests, driving the real wasm broker.
  • Conformance: the [MQTT-3.1.3-9] test now waits past the delay. A new positive control checks that the Will is published when no reconnect happens.
  • End to end: a real mqttv5 broker binary with file storage and SIGKILLed CLI clients. The fixed build behaved correctly in all five scenarios. The released v0.41.0 binary published spurious Wills after resume and after a Clean Start reconnect, and missed the session-end deadline.

Versions: mqtt5 0.41.1, mqtt5-wasm 2.0.1.

@fabracht
fabracht merged commit eb9b051 into main Sep 27, 2026
16 checks passed
@fabracht
fabracht deleted the will-delay-cancel branch September 27, 2026 19:44
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.

Will Delay Interval: a reconnect within the delay does not suppress the Will (MQTT-3.1.3-9)

1 participant