Skip to content

fix(realtime): never let an exception reach the pub/sub subscribe loop - #15166

Merged
ArnabChatterjee20k merged 1 commit into
mainfrom
fix/realtime-subscriber-boundary
Nov 2, 2026
Merged

ArnabChatterjee20k merged 1 commit into
mainfrom
fix/realtime-subscriber-boundary

Conversation

@ArnabChatterjee20k

Copy link
Copy Markdown
Member

Issue / Description

Root cause: appwrite-labs/cloud#7177

The FRA realtime outages of 2026-10-28, 10-29 and 10-30 were caused by an exception reaching the phpredis subscribe() loop. With an exception pending, phpredis 6.3.0 keeps reading pub/sub messages, but PHP's zend_call_function() returns SUCCESS without running the callback (Zend/zend_execute_API.c: "we would result in an unstable executor otherwise"), and phpredis only stops on a non-SUCCESS return. The worker then reads and drops every event, subscribe() never returns, nothing is logged, and only a restart recovers it. Reproduced on staging with the prod binaries (PHP 8.5.12, phpredis 6.3.0, Swoole 6.2.3); details in the issue.

#15086 put the event handling behind a try/catch, but two paths still reach phpredis:

  • Handler::handle() counts the event and decodes it before its try.
  • The subscribe callback in app/realtime.php builds the event span, calls shouldLog() and logs the span outside any try.

An exception on either path freezes the worker the same way.

Solution

  • Handler::handle() runs everything inside its guard, including the received counter and the decode. Counting the failure is guarded too, so a broken telemetry call cannot throw past it.
  • The subscribe callback catches any Throwable that still escapes and logs it, so nothing can reach phpredis.

Tests

  • New HandlerTest::testNeverThrowsWhenTelemetryFails: the received counter throws (a ThrowingCounter fake). Before the fix the exception escaped handle() at Handler.php:71 (red); after it, handle() returns failed with the error and counts the failure at the decode stage (green).
  • tests/unit/Realtime: 61 tests pass.
  • Pint and PHPStan pass on the changed files.

🤖 Generated with Claude Code

With an exception pending, phpredis keeps reading pub/sub messages but
zend_call_function() refuses to run the callback, so the worker silently
stops delivering until it restarts. handle() still ran its received
counter and decode before its try, and the entrypoint built and logged
the event span outside any try.

handle() now runs everything inside its guard (counting a failure cannot
throw either), and the subscribe callback catches anything that still
escapes.

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

luxor-code-review Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

Review complete. No issues found — approved ✅.


This PR wires realtime event processing through a new Appwrite\Realtime\Event\Handler and hooks it into app/realtime.php, adding unit tests that cover exception behavior via a ThrowingCounter test double.

Files Change
src/Appwrite/Realtime/Event/Handler.php, app/realtime.php Adds the event handler and integrates it into the realtime process entrypoint.
tests/unit/Realtime/Event/HandlerTest.php, tests/unit/Realtime/Event/ThrowingCounter.php Adds unit tests covering handler behavior, including exception propagation via a throwing counter.

Review found no defects meeting the reporting threshold; the change is a clean, well-tested refactor of the realtime event path.

Reviewed commit: ab73d4a

@ghost

ghost commented Nov 2, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The receipt, processing, and callback paths now contain the relevant exceptions, and the telemetry-failure regression test verifies the handler remains non-throwing.

The realtime event handler now guards event counting and decoding alongside the existing processing path, and protects failure telemetry from throwing out of the handler. The pub/sub callback also catches exceptions from event handling and span processing, with a regression test covering a telemetry failure during receipt counting.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 4
File Change
app/realtime.php Wraps pub/sub callback event handling and span logging in a Throwable guard.
src/Appwrite/Realtime/Event/Handler.php Moves counting and decoding inside the handler guard and guards failure-counter reporting.
tests/unit/Realtime/Event/HandlerTest.php Adds a test for a receipt-counter exception and factors handler construction for injecting the fake.
tests/unit/Realtime/Event/ThrowingCounter.php Adds a counter fake that throws on add().

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

github-actions Bot commented Nov 2, 2026

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

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/realtime-subscriber-boundary (after).

Metric Before After Change
🚀 Requests/sec 195.06 211.47 🟢 +8.4%
⏱️ Latency P50 89.79 ms 79.83 ms 🟢 -11.1%
⏱️ Latency P95 204.06 ms 194.62 ms ⚪ -4.6%
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 79.83 194.62 13,395 211.47 -9.44
Account 155.29 316.96 705 11.68 +11.39
TablesDB 76.55 143.55 7,285 118.19 -23.67
Storage 75.92 174.03 3,525 58.67 -15.39
Functions 119.11 240.54 1,880 31.99 +15.37

Top API waits (after)

API request Max wait (ms)
account.name.update 525.65
tablesdb.rows.create 454.64
functions.variables.update 448.5
functions.create 399.84
account.prefs.update 399.61

@ArnabChatterjee20k
ArnabChatterjee20k merged commit 32873f1 into main Nov 2, 2026
33 checks passed
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