Skip to content

feat(realtime): let a deployment run per-worker cleanup on worker exit - #14705

Merged
levivannoort merged 2 commits into
mainfrom
feat/realtime-worker-exit-hook
Oct 21, 2026
Merged

levivannoort merged 2 commits into
mainfrom
feat/realtime-worker-exit-hook

Conversation

@levivannoort

@levivannoort levivannoort commented Oct 20, 2026 •

Copy link
Copy Markdown
Member

What does this PR do?

Adds an overridable realtimeWorkerExit(int $workerId) to app/realtime.php, called once from the worker-exit callback after the worker's connections are closed. It does nothing in CE.

An app that embeds the realtime server (Cloud) can already override per-worker setup through getTelemetry(), triggerStats() and the other function_exists hooks, but has no way to run anything when a worker exits. The adapter's onWorkerExit() calls Swoole's $server->on('workerExit', …), which keeps one callback, and realtime's own connection close-out holds it.

Cloud needs it to drain a per-worker buffered usage publisher before the worker goes: it batches realtime's stats-usage events into one queue message per batch instead of one per event. An exception from the hook is logged, so it cannot stop the close-out that runs before it.

Test Plan

  • php -l app/realtime.php.
  • No behaviour change in CE: the default hook is empty, and it runs after the existing close-out, inside the same once-only $exitSwept guard.
  • The Cloud override is tested there against this hook.

Related PRs and Issues

  • None in this repository.

Checklist

  • Have you read the Contributing Guidelines on issues?
  • If the PR includes a change to an API's metadata (desc, label, params, etc.), does it also include updated API specs and example docs? (No API change.)

Swoole keeps one workerExit callback, and realtime's own close-out
holds it, so an embedding app has nowhere to drain per-worker state.
@luxor-code-review

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

Copy link
Copy Markdown

Review complete. No issues found — approved ✅.


This minimal change wires a worker-exit hook into the realtime entrypoint so per-worker cleanup runs exactly once when a Swoole realtime worker exits. The hook is defined behind a function_exists guard and invoked inside the existing onWorkerExit closure, wrapped in a Throwable catch, on first exit only after the connection sweep has run.

Files Change
app/realtime.php Adds realtimeWorkerExit() teardown helper and calls it from onWorkerExit after the connection sweep, guarded and error-tolerant.

The hook ordering versus Swoole's own worker-stop semantics, the Throwable catch scope, and the function_exists shadowing risk were all audited; no correctness, concurrency, or teardown-ordering defects were confirmed.

Reviewed commit: 2c494f3

@ghost

ghost commented Oct 20, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

Adds an overridable realtimeWorkerExit(int $workerId) hook and invokes it from the worker-exit callback after initiating connection close-out. The default implementation is empty, and hook exceptions are logged without disrupting shutdown.

Latest changes: The newest commit updates the hook documentation to clarify that connection onClose callbacks can run after the hook and that stopped buffered publishers should continue to handle them directly.

Verdict New comments Fixed Still open
💬 Commented 0 0 0
📂 Walkthrough · 1
File Change
app/realtime.php Defines the overridable worker-exit hook and invokes it in the guarded worker-exit callback.

Reviewed the commits since 2c494f3 · 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 A · See the inline comments. Summary

Comment thread app/realtime.php Resolved
@github-actions

Copy link
Copy Markdown

Security rules

No new WARNING or ERROR findings from security rules.

19 existing findings tracked in .semgrep/baseline.json
  • php.appwrite.guest-write-without-abuse-limit (12)
  • php.appwrite.secret-compare-timing (5)
  • 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 Oct 20, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → feat/realtime-worker-exit-hook (after).

Metric Before After Change
🚀 Requests/sec 212.16 211.9 ⚪ -0.1%
⏱️ Latency P50 79.91 ms 80.55 ms ⚪ +0.8%
⏱️ Latency P95 192.19 ms 196.24 ms ⚪ +2.1%
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 80.55 196.24 13,623 211.9 +4.05
Account 151.11 296.49 717 11.93 +29.42
TablesDB 76.33 140 7,409 118.82 -5.4
Storage 78.38 179.83 3,585 58.7 -13.34
Functions 125.38 240.88 1,912 31.91 +4.08

Top API waits (after)

API request Max wait (ms)
account.name.update 519.77
functions.variables.update 495.95
account.prefs.update 379.24
functions.delete 368.28
functions.create 352.07

@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

@TorstenDittmann TorstenDittmann left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

12ew

@levivannoort
levivannoort merged commit 2a0f2f2 into main Oct 21, 2026
34 checks passed
@ChiragAgg5k ChiragAgg5k mentioned this pull request Oct 21, 2026
6 tasks
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.

2 participants