Skip to content

Cap in-flight MQTT permission checks - #15093

Closed
ArnabChatterjee20k wants to merge 1 commit into
fix/mqtt-revoke-bounded-fail-closedfrom
fix/mqtt-permission-check-cap
Closed

ArnabChatterjee20k wants to merge 1 commit into
fix/mqtt-revoke-bounded-fail-closedfrom
fix/mqtt-permission-check-cap

Conversation

@ArnabChatterjee20k

@ArnabChatterjee20k ArnabChatterjee20k commented Oct 29, 2026 •

Copy link
Copy Markdown
Member

Issue / Description

Stacked on #15085. When a revoke or unsubscribe permission check times out, the broker fails closed and moves on, but nothing reaps the check. It keeps its coroutine and its database pool connection until the read returns. The pool per worker is small, so under a slow database the stuck checks could drain it and starve everything else on the worker.

Tracking: appwrite-labs/cloud#7108

Solution

  • Revoke and unsubscribe share one bounded verify() step in Appwrite\Mqtt\Event\Handler, which counts the checks in flight on the worker.
  • The count goes up before go() and down in the child's finally. A stuck check keeps its slot until its read returns, so stuck checks can never hold more than the cap.
  • At the cap the event fails closed at once, the same way as a timeout: expel() for revoke and drop() for unsubscribe. The span records mqtt.verification = check_limit with an attached RuntimeException ("Permission verification skipped: check limit reached"), the same exception choice as the coroutine-limit path. A legitimate user reconnects and CONNECT re-authenticates them.
  • The cap comes from the real pool: half the console database pool's size, and at least 1. It is read in app/mqtt.php with $register->get('pools')->get('console')->size. There is no new environment variable.

Limits

  • The cap is half the console database pool size, and at least 1.
  • With default settings the per-worker pool is 1. app/init/registers.php splits 151 / 15 connections across CPU × 6 workers, which rounds down to 1. So the cap is also 1. At that size one stuck check already holds the whole pool, and the cap can't keep a connection free for other work.
  • The cap only matters after a timeout. The subscriber handles events one at a time, so a second check only starts once the first has finished or timed out.
  • The real fix for a stuck read is a database read timeout, tracked in appwrite-labs/cloud#7100. This PR only limits how many timed-out checks can pile up.

Tests

testChecksPastTheCapFailClosedImmediately in tests/unit/Mqtt/Event/HandlerTest.php runs in its own process, like the other stuck-check tests.

  • Setup: cap = 1. A revoke check that is stuck on a read that never returns times out but keeps the only slot.
  • Second revoke: a revoke for another user must fail closed at once. It must take less than half the timeout, disconnect the user NOT_AUTHORIZED, and record check_limit with the error.
  • Delivery: a delivery that follows is still processed.
  • Slot release: once the stuck read returns, the slot is free and the next revoke is verified.

Red on #15085's head: the second revoke waited out the full timeout (0.202s against a 0.2s timeout) and recorded timed_out, starting a second stuck check, instead of failing fast with check_limit.

Green on this branch: tests/unit/Mqtt passes (91 tests).

🤖 Generated with Claude Code

A timed-out revoke or unsubscribe check is never reaped: it keeps its
coroutine and database connection until the read returns, so a slow
database could drain the pool. Checks in flight are now capped per
worker at half the console database pool (at least 1); past the cap
the event fails closed at once, recorded as mqtt.verification =
check_limit with an attached error. Revoke and unsubscribe share one
bounded verify() step.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@luxor-code-review

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

Copy link
Copy Markdown

Review complete. 🟡 1 medium

📍 Findings outside the diff (1) — 🟡 1 medium — defects on lines GitHub can't attach comments to

🟡 Medium — Thrown permission check fails open, keeping access · Handler.php:67–72 · unchanged line

// src/Appwrite/Mqtt/Event/Handler.php
67	            if ($outcome instanceof \Throwable) {
68	                $span->set('mqtt.verification', 'failed');
69	                $span->setError($outcome);
70	
71	                return;
72	            }

When the permission check throws, the revoke handler treats the error as a span annotation only: it sets mqtt.verification to failed, records the throwable, and returns without calling broker->expel() (src/Appwrite/Mqtt/Event/Handler.php:67-72). The timeout and limit paths all fail closed by expelling the user, so the fail-closed contract documented on the method is broken exactly when the check errors out. Impact: a revoked user whose verification hit a transient database exception keeps their connection and subscriptions, and the revoke event is consumed with no retry.


This change replaces the ad-hoc coroutine/channel scaffolding inside revoke() and unsubscribe() with a shared verify() helper that enforces an in-flight check limit, distinguishes three fail-closed outcomes (check_limit, coroutine_limit, timed_out), and standardizes span error reporting via unverified(). The cap is sized to half the console database pool so a slow database cannot be drained entirely by permission checks. Tests in tests/unit/Mqtt/Event/HandlerTest.php cover the new accounting behavior. One medium finding remains: a thrown (not timed-out) permission check currently fails open, keeping the user connected instead of disconnecting them.

Files Change
src/Appwrite/Mqtt/Event/Handler.php Adds verify() with an in-flight cap plus unverified() error mapping; revoke() and unsubscribe() now route through it and fail closed on incomplete checks.
app/mqtt.php Passes a check-cap of half the console database pool size (minimum 1) to the MQTT event handler.
tests/unit/Mqtt/Event/HandlerTest.php Extends unit coverage for the new verify() limit and outcome classification.

Reviewed commit: a83e103

@ghost

ghost commented Oct 29, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

A timed-out check retains its slot until its coroutine exits, so later checks fail closed at the configured cap and the slot is safely reusable afterward.

This change adds a shared in-flight limit for MQTT revoke and unsubscribe permission checks, derived from the console database pool size. Checks beyond the limit fail closed immediately, and timed-out checks retain their slot until the underlying coroutine completes. The unit tests cover cap behavior, continued delivery, and slot release.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 3
File Change
app/mqtt.php Derives the MQTT permission-check cap from the console database pool size.
src/Appwrite/Mqtt/Event/Handler.php Centralizes revoke and unsubscribe verification, tracks in-flight checks, and fails closed when the cap is reached.
tests/unit/Mqtt/Event/HandlerTest.php Adds a regression test for failing fast at the cap, continuing delivery, and releasing the slot after the stuck check completes.

Reviewed a83e103 · 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

@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.

@github-actions

Copy link
Copy Markdown

✨ Benchmark results

Comparing fix/mqtt-revoke-bounded-fail-closed (before) → fix/mqtt-permission-check-cap (after).

Metric Before After Change
🚀 Requests/sec 199.79 182.73 🔴 -8.5%
⏱️ Latency P50 85.7 ms 95.91 ms 🔴 +11.9%
⏱️ Latency P95 206.7 ms 217.66 ms 🔴 +5.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 95.91 217.66 11,400 182.73 +10.96
Account 177.62 315.04 600 10.13 +12.35
TablesDB 93.34 175.28 6,200 100.71 +14.08
Storage 88.45 200.24 3,000 50.78 +12.2
Functions 127.44 250.8 1,600 27.83 +11.25

Top API waits (after)

API request Max wait (ms)
account.name.update 555.23
functions.variables.update 543
storage.buckets.create 422.77
account.prefs.update 401.91
functions.create 376.17

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