Repository navigation
Cap in-flight realtime permission checks per worker - #15094
ArnabChatterjee20k wants to merge 4 commits into
Conversation
A timed-out check is never reaped: it keeps its coroutine and database connection until the read returns, so under a slow database stuck checks could drain the worker's pool and starve everything else. The handler now counts running checks and, at the cap, fails closed immediately with realtime.verification = check_limit instead of starting another. The cap is half the worker's database pool (at least one), derived from the console pool's size, so stuck checks always leave connections free. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Review complete. 🟡 1 medium 📍 Findings outside the diff (1) — 🟡 1 medium — defects on lines GitHub can't attach comments to🟡 Medium — Hung checks permanently consume slots with no reaping · Handler.php:478–503 · // src/Appwrite/Realtime/Event/Handler.php
478 $started = \go(function () use ($check, $done): void {
479 try {
480 $done->push(['closing' => $check()]);
481 } catch (\Throwable $th) {
482 $done->push(['error' => $th]);
483 } finally {
484 $this->checks--;
485 }
486 });
487
488 // Never inline: that brings back the unbounded wait.
489 if ($started === false) {A check coroutine that never completes holds its slot forever: 🧹 Nitpicks (1) — 🟢 1 low
This PR caps concurrent permission checks in the realtime worker at half the console database pool size, failing closed (closing the client connection) when the cap is reached instead of queueing an unbounded number of checks.
Reviewed commit: d9ec7f7 |
🟡 Tier B · Needs changes before merging
The realtime worker now caps concurrent permission checks based on half of the console database pool, with a minimum of one, and fails closed when the cap is reached. It also records event counts, exposes the current processing-stage duration, and emits event spans; new unit coverage checks these telemetry behaviors. Latest changes: The newest commits add event counters, a stage-duration gauge, per-event spans, and unit tests with recording helpers.
Fix with agent prompt### Issue 1
src/Appwrite/Realtime/Event/Handler.php:119
**Avoid exporting a span for every realtime event**
With the default self-hosted exporter in `app/init/span.php`, every successful pub/sub message now synchronously writes a `realtime.event` span to stdout; under normal firehose traffic this can create unbounded log volume and slow the worker. The previous handler deliberately emitted spans only for failures, so routine spans need sampling or opt-in export.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.📂 Walkthrough · 5
🔇 Filtered out · 1Findings Hansi considered but did not post.
Reviewed the commits since |
Security rulesNo new WARNING or ERROR findings from security rules. 6 existing findings tracked in
|
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…t-stacked feat(realtime): telemetry for the pub/sub event handler
| } | ||
|
|
||
| $span->set('realtime.resolved_at', \microtime(true)); | ||
| $span->finish(error: $error); |
There was a problem hiding this comment.
Avoid exporting a span for every realtime event
With the default self-hosted exporter in app/init/span.php, every successful pub/sub message now synchronously writes a realtime.event span to stdout; under normal firehose traffic this can create unbounded log volume and slow the worker. The previous handler deliberately emitted spans only for failures, so routine spans need sampling or opt-in export.
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/Appwrite/Realtime/Event/Handler.php
Line: 119
Comment:
**Avoid exporting a span for every realtime event**
With the default self-hosted exporter in `app/init/span.php`, every successful pub/sub message now synchronously writes a `realtime.event` span to stdout; under normal firehose traffic this can create unbounded log volume and slow the worker. The previous handler deliberately emitted spans only for failures, so routine spans need sampling or opt-in export.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.🟠 Major · performance · Reply if this doesn't apply.
Issue / Description
Tracking: appwrite-labs/cloud#7108
Stacked on #15086 (base:
fix/realtime-permission-ingest-fail-closed). It lands there when the stack is merged.#15086 bounds how long the realtime pub/sub subscriber waits for a permission check: after the timeout it fails closed and moves on. The timed-out check itself is never reaped, though. Its child coroutine keeps waiting on the database read, holding the database pool connection it took, until that read returns. Under a slow or partly dead database, every timed-out check keeps a connection. Nothing in the process capped that below Swoole's coroutine limit, so stuck checks could drain the worker's pool and starve connection handshakes and everything else on that worker.
Solution
Appwrite\Realtime\Event\Handler::verify()now counts the permission checks running on its worker. The count goes up before the child coroutine starts and down in the child'sfinally, so a stuck check keeps its slot for as long as it is stuck. When the count is at the cap, no new child is started. The check fails closed immediately, like the timeout and coroutine-limit paths: the span recordsrealtime.verification = check_limitwith aGENERAL_SERVER_ERROR("Permission verification skipped: check limit reached") attached, and the affected connections receive the 401 and close.The cap is derived from the worker's real database pool, with no new setting:
max(1, intdiv(console pool size, 2)).sizeis a public property ofUtopia\Pools\Pool, and every database pool inapp/init/registers.phpuses the same size. Stuck checks can therefore hold at most half the pool and always leave connections for everything else. This matches the MQTT broker's version in #15093.Limits
app/init/registers.php). The cap is then 1, and one stuck check already holds the whole pool. The cap prevents a second check from adding to it, but it cannot keep a connection free on a pool of one. Deployments with larger pools get the headroom described above.Tests
Verified locally against a real Swoole realtime server and Redis, using the same process harness as #15086 with a temporary test that is not committed. The pool-sizing settings (
_APP_CPU_NUM=1,_APP_WORKER_PER_CORE=1,_APP_POOL_CLIENTS=1,_APP_CONNECTIONS_MAX=2) give a console pool of 2, so the derived cap is 1. Scenario: one user's permission check gets stuck on a database read that never returns. Then a permission change arrives for a second user, followed by a normal event, then a new connection.5015e9ede0, no cap)d9ec7f7db7)timed_out, 2s), leaving two stuck checkscheck_limit, 0s), connection closed with 401The fake database in the harness holds no real pool connection, so this run cannot show the pool draining and handshakes stalling. It shows that, without the cap, every stuck check starts another child. In production each of those children holds a pool connection.
After (green), d9ec7f7
Before (red), 5015e9e
ANSI colour codes stripped. Nothing else edited.
composer lintandcomposer analyzeare clean on the changed files, andtests/unit/Realtimepasses (50 tests).🤖 Generated with Claude Code