Skip to content

fix(binding-mqtt): keep the session for mqtt 3.1.1 clean session 0 - #3147

Merged
jfallows merged 4 commits into
fix/3132-mqtt-kafka-retained-tombstonefrom
fix/3133-mqtt-v4-clean-session-persistence
Nov 12, 2026
Merged

jfallows merged 4 commits into
fix/3132-mqtt-kafka-retained-tombstonefrom
fix/3133-mqtt-v4-clean-session-persistence

Conversation

@jfallows

@jfallows jfallows commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Description

What and why. MQTT 3.1.1 section 3.1.2.4 (Clean Session) and section 4.1 say that with Clean Session=0 the server resumes the session from stored state and keeps it until the client connects with Clean Session=1. Until now the server never set a session expiry for 3.1.1, so every 3.1.1 session had expiry 0. mqtt-kafka treats expiry 0 as "ends when the connection closes": it sends an expiry signal with delay 0, and the session record is removed. A 3.1.1 client with Clean Session=0 therefore lost its subscriptions. I confirmed this with a failing test before changing anything.

MQTT 5.0 section 3.1.2.11.2 defines a Session Expiry Interval of 0xFFFFFFFF as "the session does not expire". The old conversion (int) SECONDS.toMillis(expiry) overflowed above about 24.8 days.

Changes

  • A 3.1.1 CONNECT with Clean Session=0 now sends session expiry 0xFFFFFFFF (never expires) to the application. Clean Session=1 keeps expiry 0, which still ends the session at disconnect.
  • MqttSessionBeginEx.expiry stays int32 on the wire and is read as unsigned. -1 means never expires, so there is no IDL change.
  • mqtt-kafka (MqttKafkaSessionFactory):
    • It keeps the expiry as received and echoes it back unchanged.
    • Milliseconds are computed in a long.
    • A session that never expires sends no expiry signal at connect ("expire later"), close or abort. The existing cancel signal at connect is still sent.
    • The will delay clamp treats never expires as unbounded.
  • The DISCONNECT Session Expiry Interval validation in MqttServerFactory now compares as unsigned. It does not apply the DISCONNECT expiry, which is still out of scope.

What is NOT delivered. The expiry signals (MqttExpirySignal, MqttWillSignal) are persisted in the Kafka sessions topic and have no version field, so I did not change their layout. delay stays int32 milliseconds and saturates at Integer.MAX_VALUE. The exact deadline travels in the int64 expireAt of the at-close signal. The cross-instance "expire later" path (expireAt = -1, reader computes now + delay) is therefore capped at about 24.8 days. Widening the signal layout needs a compatible design and is a follow-up.

Fixes #3133

Stack

# PR Branch Base
1 #3135 fix/3129-mqtt-per-connection-limits develop
2 #3136 fix/3130-mqtt-kafka-connack-maximum-qos fix/3129-mqtt-per-connection-limits
3 #3137 fix/3131-mqtt-kafka-message-expiry-seconds fix/3130-mqtt-kafka-connack-maximum-qos
4 #3146 fix/3132-mqtt-kafka-retained-tombstone fix/3131-mqtt-kafka-message-expiry-seconds
5 this PR fix/3133-mqtt-v4-clean-session-persistence fix/3132-mqtt-kafka-retained-tombstone

This branch is restacked by rebase onto the current head of #3146 (18b0225e5), replaying only this PR's four commits. The rebase had no conflicts.

Test evidence

Tests were committed first (test(...) commits precede the fix(...) commit).

  • binding-mqtt (spec and runtime):
    • v4 session.connect.non.clean.start (application expiry -1), v4 session.connect.clean.start (expiry 0), v5 session.connect.with.session.expiry.never (0xFFFFFFFF becomes -1).
    • On the pre-fix code the v4 non-clean-start test failed: the application saw expiry 0 instead of -1. The v4 clean-start test and the v5 never test already passed there, as expected (unchanged behavior; the v5 int cast passes -1 through).
  • mqtt-kafka (spec and proxy IT):
    • session.close.reconnect.non.expiring: connect, store subscription state, close, reconnect gets the state back; no expiry signals.
    • session.close.expire.session.state.large.interval: 3000000 s sends delay = 2147483647 and expireAt = 3000001000.
    • session.close.expire.session.state.immediately: expiry 0 sends delay 0 at close (unchanged behavior).
    • session.close.reconnect.clean.start: a Clean Session=1 reconnect clears the state stored by a non-expiring session.
    • session.will.message.abort.deliver.will.non.expiring: a will delivered on abort for a never-expiring session.
    • On the pre-fix code the proxy IT failed 4 tests: the non-expiring reconnect, the large interval (the begin echo came back as expiry -1294967), the clean-start reconnect and the will test. The immediately test passed, as expected.
    • The four session close scripts also assert publishQosMax(1) on the session reply. The session matcher ignores publishQosMax unless a script sets it, so this keeps them valid now that fix(binding-mqtt-kafka): advertise maximum qos when qos 2 publish is disabled #3136 enforces it. No other script needed changing.
  • Full verify (./mvnw clean verify -am, checkstyle, license and jacoco on) for specs/binding-mqtt.spec, runtime/binding-mqtt, specs/binding-mqtt-kafka.spec and runtime/binding-mqtt-kafka, on this branch rebased onto fix(binding-mqtt-kafka): remove the retained message on a zero-byte retained publish #3146: BUILD SUCCESS, 0 failures (binding-mqtt runtime 452 tests, binding-mqtt-kafka runtime 376 tests, binding-mqtt spec 441, binding-mqtt-kafka spec 234). My four modules ran their tests; only unchanged upstream modules came from the build cache. The earlier red server.v5.SubscribeIT in CI came from the lower layer (fix(binding-mqtt): scope maximum packet size and publish qos max to the connection #3135) and passes here.
  • Not run: a full reactor build with the build cache bypassed and checks on, which fails in the unrelated build module (license mapping for manager and zpm-maven-plugin). That is an environment issue.

Notes and follow-ups

  • Widen the expiry signal layout compatibly (the will signal delay is int32 as well), so cross-instance expiry beyond about 24.8 days works.
  • Clean up the per-session #offsets record and consumer-group offsets at expiry. A never-expiring session keeps them indefinitely, so this matters more now.
  • Apply a Session Expiry Interval sent in DISCONNECT.
  • The v5 will delay in seconds is still int32 milliseconds after the clamp.
  • A merged PR (fix(binding-mqtt-kafka): guard expiry signal when the kafka session stream is not established #3138, the isKafkaOpen guard in MqttKafkaSessionFactory.KafkaSessionStream) is not in this stack yet. Expect to reconcile it when the stack is rebased onto develop.

🤖 Generated with Claude Code

https://claude.ai/code/session_01SiHVoW6wezgVqyaf2mPbd8

Copy link
Copy Markdown
Contributor Author

CI on 4fecf10fd is red in Build (25) on runtime/binding-mqtt: server.v5.SubscribeIT.shouldCompleteDroppedQoS2MessageExceedingMaximumPacketSize (line 574). Run 1 is a script mismatch and the rerun is a TestTimedOut; 109 tests ran with 1 error. The Examples check is a gate job that was skipped because Build failed.

This is not caused by this PR:

  • The base branch fix(binding-mqtt-kafka): remove the retained message on a zero-byte retained publish #3146 fails on its own head with the same test and the same Run 1 / Run 2 failure (Build (25) of run 43300982547, which was reported red in that run).
  • This PR's diff does not touch v5 subscribe handling or that test. It changes the v4 CONNECT session expiry, the DISCONNECT expiry comparison, and MqttKafkaSessionFactory.
  • I could not reproduce it locally: v5 SubscribeIT passes 49 of 49 in an uncached run on this branch. So I only know it is red on the base, not what causes it.

The test was introduced lower in the stack (#3135), so I'm leaving it to that layer rather than changing a test here. I'm not skipping, disabling or retrying anything. This PR will pick up whatever fix lands there when the stack is restacked.


Generated by Claude Code

…sion expiry

Add network and application scripts asserting the session expiry carried to
the application stream for MQTT 3.1.1 Clean Session=0 (never expires),
Clean Session=1 (zero) and the MQTT 5 Session Expiry Interval 0xFFFFFFFF.

Add mqtt-kafka scripts for a non expiring session that is reconnected with
its subscriptions and publishes no expiry signal, an expiry interval above
2^31 milliseconds, and expiry zero at close.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoW6wezgVqyaf2mPbd8
MQTT 3.1.1 section 3.1.2.4 keeps the session until the client connects with
Clean Session=1, so a connect with Clean Session=0 now carries a session
expiry of 0xFFFFFFFF (never expires) to the application, while Clean
Session=1 keeps expiry 0.

The mqtt-kafka session no longer sends an expiry signal at close, abort or
connect for a session that never expires, and echoes the received expiry
unchanged instead of converting through milliseconds. Expiry seconds are read
as unsigned and converted to milliseconds in a long; the int32 signal delay
saturates at Integer.MAX_VALUE with the exact deadline in expireAt. The will
delay clamp treats never expires as unbounded. A DISCONNECT Session Expiry
Interval is compared as unsigned.

Fixes #3133

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoW6wezgVqyaf2mPbd8
…ion and will with no session expiry

Add a clean start reconnect that clears the state stored by a non expiring
session, and a will delivered on abort for a session that never expires.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoW6wezgVqyaf2mPbd8
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.

mqtt: MQTT 3.1.1 Clean Session=0 does not keep session state, and session expiry conversion overflows

2 participants