Skip to content

fix(binding-mqtt): scope maximum packet size and publish qos max to the connection - #3135

Merged
jfallows merged 9 commits into
developfrom
fix/3129-mqtt-per-connection-limits
Nov 12, 2026
Merged

jfallows merged 9 commits into
developfrom
fix/3129-mqtt-per-connection-limits

Conversation

@jfallows

@jfallows jfallows commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Description

MqttServerFactory kept two per-connection limits in fields shared by every connection on a worker:

  • maximumPacketSize was lowered by every CONNECT carrying Maximum Packet Size and never restored, then applied to inbound packet checks and the outbound PUBLISH drop of all later connections.
  • publishQosMax was overwritten by every session BEGIN, so a connection saw whatever the latest session on the worker reported.

MQTT 5.0 section 3.1.2.11.4: the Maximum Packet Size in CONNECT is the largest packet the client will accept, so it limits packets sent to that client only. It affects no other connection and does not limit what the server accepts from that client.

Changes:

  • The client's declared Maximum Packet Size and the session's publish QoS maximum are now state of the MqttServer stream.
  • Inbound packet checks use the server's own limit only (none unless configured); the client's value is used only for the outbound drop.
  • A repeated Maximum Packet Size property in CONNECT is now detected with its own mask (it used the Topic Alias Maximum mask) and gets the protocol error.

Also fixed here, found while auditing the same code:

  • Receive Maximum shared the Maximum Packet Size switch case and was read as a 32-bit packet size. It is now decoded as its own 16-bit property (MQTT 5.0 section 3.1.2.11.3) with a protocol error for a zero or repeated value, and no longer touches the packet size limit.
  • Outbound size check compared only the payload size to the client's Maximum Packet Size. It now compares the encoded packet size, including payload deferred to later frames, and drops the continuation frames of a dropped message so a partial packet is never written.
  • Dropped QoS 1/2 messages are now completed. A message too large for the client is discarded and the server behaves as if it had completed sending it (section 3.1.2.11.4), but the old code still registered its packet id for an acknowledgement the client would never send and never returned window credit, so the application waited forever and the subscribe stream could stall. A dropped message now returns the window credit and acknowledges to the application the way the client's ack would have: the QoS 1 acknowledgement for QoS 1, and a direct completion for QoS 2. QoS 2 deliberately skips the INCOMPLETE step, because the application answers INCOMPLETE and the binding would then send PUBREL to the client for a packet it never received.
  • Publish helper retained flag was never cleared by reset(). The helper is one object shared by every connection on the worker, so a retained PUBLISH made later PUBLISH packets look retained, and a connection whose session has no retain capability was then disconnected for a non-retained PUBLISH.

Fixes #3129

Stack

# Issue Branch
1 (this PR) #3129 fix/3129-mqtt-per-connection-limits (base develop)
2 #3130 fix/3130-mqtt-kafka-connack-maximum-qos
3 #3131 fix/3131-mqtt-kafka-message-expiry-seconds
4 #3132 fix/3132-mqtt-kafka-retained-tombstone
5 #3133 fix/3133-mqtt-v4-clean-session-persistence

Test evidence

Test-first: the scripts and IT methods were committed before each fix, so a test commit is red on its own until the fix commit that follows it. In particular ffd8302dd (QoS 2 scenario) and b2fa16165 (its fix) go together, as do 9bce44c9e / b3075fd42 for QoS 1. Added paired network and application scripts (v5), run by specs/binding-mqtt.spec network and application ITs and by the runtime ITs:

  • connect.max.packet.size.other.connection.publish: first connection declares Maximum Packet Size 50, second declares none and sends a larger PUBLISH.
  • connect.max.packet.size.other.connection.subscribe: same pair, larger PUBLISH delivered to the second connection.
  • session.publish.qos.max.per.connection: two sessions whose application BEGIN reports publish QoS maximum 0 and 2; QoS 1 PUBLISH on the first gets DISCONNECT 0x9b.
  • connect.reject.maximum.packet.size.repeated: repeated Maximum Packet Size gets CONNACK 0x82.
  • connect.reject.receive.maximum.zero and connect.reject.receive.maximum.repeated: CONNACK 0x82.
  • connect.max.packet.size.exceeded.by.packet.overhead: limit 25; a 7-byte payload in a 26-byte packet is dropped and an 18-byte packet is delivered.
  • publish.retained.per.connection: a non-retained PUBLISH on a session without retain capability after a retained PUBLISH on another connection is accepted.
  • subscribe.qos1.drop.exceeds.max.packet.size: a QoS 1 message exceeding the limit is dropped, the application still receives its acknowledgement, and the next message is delivered and acknowledged.
  • subscribe.qos2.drop.exceeds.max.packet.size: a QoS 2 message exceeding the limit is dropped, the application receives a direct completion for it, no PUBREL is sent, and the next message completes the normal PUBREC/PUBREL/PUBCOMP exchange.

Before the fixes, these failed for the expected reasons: the large PUBLISH was rejected (the publish stream never began), the large delivery was dropped, the QoS 1 PUBLISH was accepted instead of rejected, a repeated Maximum Packet Size and a zero Receive Maximum were accepted, the 26-byte packet was delivered, the non-retained PUBLISH was rejected, and the application never received the acknowledgement for a dropped QoS 1 or 2 message so the next message was never sent. connect.reject.receive.maximum.repeated already passed before its fix, by accident of the shared mask, and stays as a regression guard.

The first QoS 2 fix (b3075fd42) sent INCOMPLETE then COMPLETE for a dropped QoS 2 message; the QoS 2 scenario, written afterwards, showed the INCOMPLETE step is wrong, and b2fa16165 corrects it.

After the fixes (head b2fa16165):

  • ./mvnw clean verify -pl specs/binding-mqtt.spec,runtime/binding-mqtt -am: BUILD SUCCESS, checkstyle passed.
  • runtime/binding-mqtt: 44 unit tests, 369 ITs, 0 failures, 6 skipped.
  • specs/binding-mqtt.spec: 439 run, 0 failures, 2 skipped, from an earlier run on the same scripts; the module was restored from the build cache in the final run, so its tests did not re-run.

Notes and follow-ups (not fixed here)

  • Receive Maximum is validated but not enforced: the server does not limit concurrent unacknowledged outbound QoS 1/2 PUBLISH packets to the client's value. The byte-denominated window cannot express a message count; enforcing it needs a message-count axis in flow control rather than a binding-local workaround.
  • No v4 scripts: Maximum Packet Size, Receive Maximum and the repeated-property checks exist only in v5.
  • Shared-subscription code was not touched.

🤖 Generated with Claude Code

https://claude.ai/code/session_01SiHVoUbETLeakPq2hrmVEg

…ish qos max

Add paired network and application scripts and IT methods for two
connections on one binding where the first declares a small Maximum
Packet Size, two sessions reporting different publish QoS maximums, and
a CONNECT with a repeated Maximum Packet Size property.

These are expected to fail against the current code, which keeps both
limits in factory fields shared by every connection on a worker.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoUbETLeakPq2hrmVEg
…he connection

The client's Maximum Packet Size limits only packets sent to that client
(MQTT 5.0 section 3.1.2.11.4), and publish QoS maximum reported by a
session applies to that session. Both were factory fields shared by every
connection on a worker, so one connection lowered or overwrote the limit
for all others.

Keep the client's Maximum Packet Size and the session's publish QoS
maximum as state of the MqttServer stream, check inbound packets against
the server's own limit only, and detect a repeated Maximum Packet Size
property with its own mask instead of the Topic Alias Maximum mask.

Fixes #3129

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoUbETLeakPq2hrmVEg
…tain per connection

Add paired network and application scripts and IT methods for a zero or
repeated Receive Maximum in CONNECT, a PUBLISH whose payload fits the
client's Maximum Packet Size but whose encoded packet does not, and a
non-retained PUBLISH on one connection after a retained PUBLISH on
another whose session has no retain capability.

These are expected to fail against the current code.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoUbETLeakPq2hrmVEg
…ize, reset retain

Receive Maximum shared a switch case with Maximum Packet Size and was read
as a 32-bit packet size. Decode it as its own 16-bit property, with a
protocol error for a zero or repeated value.

The outbound drop compared only the payload size to the client's Maximum
Packet Size. Compare the encoded packet size instead, including payload
deferred to later frames, and drop the continuation frames of a dropped
message so a partial packet is never written.

The publish helper shared by every connection never cleared its retained
flag, so a retained PUBLISH on one connection made later PUBLISH packets
on the worker appear retained.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoUbETLeakPq2hrmVEg
…packet size

A message too large for the client's Maximum Packet Size is discarded and
the server behaves as if it had completed sending it (MQTT 5.0 section
3.1.2.11.4), so the application must still receive the acknowledgement
for the dropped QoS 1 message and delivery continues with the next one.

Expected to fail against the current code, which leaves the dropped
message waiting for an acknowledgement the client never sends.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoUbETLeakPq2hrmVEg
…ing maximum packet size

A message discarded because it exceeds the client's Maximum Packet Size is
treated as if sending had completed (MQTT 5.0 section 3.1.2.11.4). Do not
register its packet id for an acknowledgement the client will never send;
return the window credit and acknowledge the message to the application as
a PUBACK, or PUBREC and PUBCOMP, would have.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoUbETLeakPq2hrmVEg
…packet size

A QoS 2 message too large for the client's Maximum Packet Size is
discarded and the server behaves as if it had completed sending it, so the
application must receive the completion for the dropped message, no
PUBREL may be sent for it, and delivery continues with the next message.

Expected to fail against the current code.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoUbETLeakPq2hrmVEg
…pubrel

Acknowledging a QoS 2 message dropped for exceeding the client's Maximum
Packet Size with an INCOMPLETE flush made the application confirm it, which
sends PUBREL to the client for a packet that was never sent. Complete the
message directly instead.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoUbETLeakPq2hrmVEg
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: Maximum Packet Size and publish QoS limit leak across connections on a worker

2 participants