Skip to content

fix(mqtt): close the connection on a second CONNECT - #14809

Merged
ChiragAgg5k merged 3 commits into
mainfrom
fix/mqtt-second-connect
Oct 22, 2026
Merged

ChiragAgg5k merged 3 commits into
mainfrom
fix/mqtt-second-connect

Conversation

@ChiragAgg5k

@ChiragAgg5k ChiragAgg5k commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

Summary

MQTT 3.1.1 and 5.0 require the server to treat a second CONNECT on the same network connection as a protocol error and close it. Utopia\Mqtt\Server passed it to the handler again, which could rebind the client id and identity of an established session.

  • Connection::$connected records the first CONNECT; a second one disconnects with reason Disconnect::PROTOCOL_ERROR (0x82) on 5.0 and closes the socket on 3.1.1, without reaching the handler.
  • receive() no longer re-arms the keep-alive timer for a connection that closed while its packet was handled.

Test plan

  • bin/monorepo check mqtt
  • bin/monorepo test mqtt (unit and e2e tiers); the two new ConnectionTest cases fail without the fix
  • tests/unit/Mqtt: RevokeTest used to expect a second CONNECT on one connection to re-index it under a new user and project. It now asserts the protocol-error DISCONNECT and that neither identity stays indexed.

Related: appwrite-labs/cloud#6666

MQTT 3.1.1 and 5.0 both treat a second CONNECT on one network connection as a protocol error. The server handed it to the handler again, which could rebind the client id and identity of a live session. It now disconnects with reason 0x82 (5.0) or closes the socket (3.1.1), and no longer re-arms keep-alive for a connection that closed while its packet was handled.
@luxor-code-review

luxor-code-review Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Review complete. No blocking issues — approved ✅; 2 nitpicks below.

🧹 Nitpicks (2) — 🟢 2 low
  • 🟢 Fix inverted failure message on clientId assertion (ConnectionTest.php:361) — The assertion at packages/mqtt/tests/ConnectionTest.php:361 checks that the handler still recorded the first CONNECT's id (client-9), but its failure message reads "the second CONNECT replaced the client id".
  • 🟢 Rename the 3.1.1 test to match its assertion (ConnectionTest.php:364) — The test testSecondConnectOnA311ClientClosesWithoutSendingAPacket asserts that a CONNACK is sent (assertCount(1, $adapter->sent)), yet its name claims no packet is sent (packages/mqtt/tests/ConnectionTest.php:364).

This PR makes the MQTT broker reject a second CONNECT received on an already-established connection: V5 clients get a DISCONNECT packet with reason code 0x82 (protocol error) before close, while 3.1.1 clients are simply disconnected. The change spans the packet model, connection dispatch, and server-side handling, and adds a recording test fixture plus version-specific regression tests.

Files Change
packages/mqtt/src/Packet/Disconnect.php Support encoding a DISCONNECT packet with an explicit reason code (0x82).
packages/mqtt/src/Connection.php Track connection state so a repeated CONNECT is detected during dispatch.
packages/mqtt/src/Server.php On a second CONNECT, send a V5 DISCONNECT with reason 0x82 (or close silently for V3) and tear down the connection.
packages/mqtt/tests/AcceptingHandler.php Add a recording adapter/handler fixture that captures connects and close ordering.
packages/mqtt/tests/ConnectionTest.php Add tests asserting the second CONNECT is rejected and never reaches the handler, for both V5 and 3.1.1 clients.

Reviewed commit: 8c9c1a4

@ChiragAgg5k ChiragAgg5k self-assigned this Oct 22, 2026
@ghost

ghost commented Oct 22, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The MQTT server tracks whether a network connection has completed CONNECT and rejects duplicate CONNECT packets with the appropriate MQTT 5.0 protocol-error DISCONNECT or MQTT 3.1.1 socket close. It also avoids rearming keep-alive after packet handling closes a connection. Regression coverage verifies handler identity and indexing behavior, including the broker revoke flow.

Latest changes: The newest commit updates the revoke regression test to assert that a duplicate CONNECT closes the connection with a Protocol Error and leaves neither identity indexed, rather than expecting the second identity to replace the first.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 6
File Change
packages/mqtt/src/Connection.php Adds per-connection state recording whether CONNECT has arrived.
packages/mqtt/src/Packet/Disconnect.php Defines the MQTT 5.0 Protocol Error reason code.
packages/mqtt/src/Server.php Rejects duplicate CONNECT packets and avoids refreshing keep-alive after a connection closes during packet handling.
packages/mqtt/tests/AcceptingHandler.php Adds a test handler that records CONNECT calls and client identity.
packages/mqtt/tests/ConnectionTest.php Covers duplicate CONNECT behavior for MQTT 5.0 and 3.1.1 and verifies the handler retains the initial identity.
tests/unit/Mqtt/RevokeTest.php Replaces the old identity-reindex expectation with assertions for protocol-error closure and clean revoke indexes after duplicate CONNECT.

Reviewed the commits since 0dd0ac7 · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Tier S · Looks good to merge. Summary

@ChiragAgg5k

Copy link
Copy Markdown
Member Author

Addressed both nitpicks in the latest commit: the failure message now states the expectation (the client id stays the first CONNECT's), and the 3.1.1 test is renamed to testSecondConnectOnA311ClientClosesWithoutADisconnectPacket, since the CONNACK for the first CONNECT is still sent.

@github-actions

github-actions Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/mqtt-second-connect (after).

Metric Before After Change
🚀 Requests/sec 215.55 223.22 ⚪ +3.6%
⏱️ Latency P50 79.23 ms 77.06 ms ⚪ -2.7%
⏱️ Latency P95 191.45 ms 183.25 ms ⚪ -4.3%
Per-scenario breakdown & investigation details

Metrics below reflect the current branch (after). Δ P95 compares against the base.

Scenario P50 (ms) P95 (ms) Requests RPS Δ P95 (ms)
API total 77.06 183.25 13,680 223.22 -8.2
Account 149.93 296.7 720 12.48 +34.64
TablesDB 75.03 150.53 7,440 124.39 -0.21
Storage 68.94 161.98 3,600 61.97 -14.18
Functions 106.42 204.9 1,920 33.72 -25.1

Top API waits (after)

API request Max wait (ms)
account.name.update 474.39
functions.create 374.14
account.prefs.update 359.32
tablesdb.rows.create 351.84
storage.buckets.create 350.91

@github-actions

Copy link
Copy Markdown

Security rules

No new WARNING or ERROR findings from security rules.

6 existing findings tracked in .semgrep/baseline.json
  • php.appwrite.guest-write-without-abuse-limit (3)
  • php.appwrite.secret-compare-timing (1)
  • php.appwrite.weak-secret-env-default (2)

Posted by Checks / Rules. Re-runs update this comment in place. Rule details and baseline: .semgrep/README.md.

The realtime revoke test relied on a second CONNECT rebinding the connection to another user and project. The broker now refuses it as a protocol error, so the test asserts the DISCONNECT and that neither identity stays indexed.
@ChiragAgg5k
ChiragAgg5k merged commit 5d68088 into main Oct 22, 2026
66 checks passed
@ChiragAgg5k
ChiragAgg5k deleted the fix/mqtt-second-connect branch October 22, 2026 18:43
Tapraj-Pathak pushed a commit to Tapraj-Pathak/appwrite that referenced this pull request Oct 24, 2026
fix(mqtt): close the connection on a second CONNECT

(cherry picked from commit 5d68088)
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