Repository navigation
fix(mqtt): never let an exception reach the subscribe loop; wait the API database timeout - #15170
Conversation
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…eout Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Review complete. 🟡 2 medium 💬 Inline comments (1)
📍 Findings outside the diff (1) — 🟡 1 medium — defects on lines GitHub can't attach comments to🟡 Medium — Leaked // src/Appwrite/Mqtt/Event/Handler.php
174 $span = Span::init('mqtt.deliver');
175 $span->set('project.id', $projectId);
176 $span->set('mqtt.topic', $topic);
177 $span->set('mqtt.qos', $qos);
178 $span->set('mqtt.is_broker', true);
179
180 $delivered = $this->broker->deliver($this->server, $projectId, $topic, $message, $qos, $sequence, $publishedAt);
181
182 $span->set('mqtt.subscribers', $delivered);
183 if ($delivered === 0) {
184 $this->mqtt->messagesDropped->add(1, ['reason' => 'no_subscriber']);
185 }
This PR extracts MQTT broker event handling into
Reviewed commit: 14f3604 |
🟢 Tier S · Ready to merge
The MQTT event callback catches failures across dispatch and reports them through failure telemetry and an error span. Broker database handles and permission-verification waits use the API database timeout, replacing the MQTT-specific permission timeout; unit coverage exercises a delivery failure caused by unavailable telemetry. Latest changes: The newest commit clarifies that the permission wait is intended to cover at least one API-timeout-bounded query, rather than the full verification check.
📂 Walkthrough · 6
Reviewed the commits since |
There was a problem hiding this comment.
Adds a dedicated MQTT event-handler class with fail-closed permission verification, telemetry instrumentation, and unit tests, replacing inline logic in app/mqtt.php.
Key findings
- 🟡 Leaked
mqtt.deliverspan when delivery throws — Handler.php:174 - 🟡 Verification timeout loses tuning and conflates query vs total budget — mqtt.php:258
Security rulesNo new WARNING or ERROR findings from security rules. 6 existing findings tracked in
|
Co-Authored-By: Claude Opus 5.5 <[email protected]>
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
25e9d68
into
fix/mqtt-revoke-bounded-fail-closed
Issue / Description
Stacked on #15085. Part of appwrite-labs/cloud#7177 (Finding 8).
The MQTT broker has the same two problems the realtime server had (fixed for realtime in #15166 and #15169).
1. An exception can reach the phpredis
subscribe()loop.Event\Handler::handle()is passed straight to$pubsub->subscribe(['mqtt'], ...).revoke()andunsubscribe()contain their own failures, buthandle()itself anddeliver()do not: thebroker->deliver()call, themessagesDroppedcounter and the span calls can all throw.zend_call_function()returnsSUCCESSwithout running the callback, and phpredis only stops on a non-SUCCESSreturn.2. The permission wait and the database timeout were unrelated.
_APP_MQTT_PERMISSION_TIMEOUT(5 s) for a revoke or unsubscribe check.DatabaseFactorydefaults to0), so an abandoned check's query could keep running unbounded.Solution
Event\Handler::handle()runs everything inside atry. A failure is counted onmqtt.messages.failedwith itsactionand reported as anmqtt.eventspan carrying the error. Reporting is guarded too, so nothing escapes to phpredis.APP_DATABASE_TIMEOUT_MILLISECONDS_API / 1000(15 s), the time an API request gives a query._APP_MQTT_PERMISSION_TIMEOUT(added in Bound the MQTT revoke wait and fail closed on verification timeout #15085) is removed.getConsoleDB()andgetProjectDB()inapp/mqtt.phpsetAPP_DATABASE_TIMEOUT_MILLISECONDS_API, so a check's query is killed when the wait ends.Trade-off: the wait bounds the whole check, which can run several queries. A hung check can now hold the subscriber for up to 15 s instead of 5 s before it fails closed. A slow-but-healthy database that pushes a multi-query check past 15 s also fails closed: connections or subscriptions drop and are re-authorized on reconnect.
_APP_MQTT_PERMISSION_TIMEOUTonly exists in the unmerged #15085, so no deployment sets it.Cloud needs the same database-handle change in its own
app/mqtt.php; that is a separate PR.Tests
Mqtt\Event\HandlerTest::testNeverThrowsWhenAnEventFailsdelivers to a topic with no subscribers while the drop counter throws (UnavailableTelemetryfake).RuntimeException: telemetry unavailableescapeshandle()fromdeliver()(Handler.php:163→:35), and the isolated test process dies (red).handle()returns, counts[1, ['action' => 'deliver']]onmqtt.messages.failed, and records onemqtt.eventspan with the error (green).tests/unit/Mqttandtests/unit/Docker: 117 tests pass.🤖 Generated with Claude Code