Repository navigation
fix(realtime): detect dead presence sockets via heartbeat + boot reconciliation - #14661
Conversation
…piry A silently dead realtime client (network loss, or a peer that vanished without a FIN) never ran onClose, so its presence lingered until the 30-day expiry (#14343). Configure Swoole's heartbeat so a socket idle past the idle time is closed and runs the normal onClose/release path; clients ping every ~20s, well within the window, so live connections are untouched. Shorten the realtime presence expiry from 30 days to 1 day as the backstop for a hard crash that fires no onClose at all. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Review complete. 🟡 1 medium 💬 Inline comments (1)
This change tightens realtime presence cleanup: presence documents now expire after one day instead of thirty, and the Swoole WebSocket adapter configures heartbeat checking so zombie connections are reaped and their presence holds released.
One notable gap: the heartbeat path ( Reviewed commit: 0b77495 |
🟢 Tier S · Ready to merge
Realtime now closes sockets that exceed Swoole's heartbeat idle window, allowing normal presence cleanup to run, while retaining the 30-day expiry as a backstop. Self-hosted startup also reconciles same-host presence records in the console and project databases and routes deleted records through offline cleanup side effects. Latest changes: The newest commits add a boot-time
📂 Walkthrough · 2
Reviewed the commits since |
There was a problem hiding this comment.
Shortens the realtime presence record expiry from 30 days to 1 day and adds a Swoole heartbeat timer so dead WebSocket connections are closed and their presence rows cleaned up.
Key findings
- 🟡 Presence expiresAt never refreshed on live connections — Presence.php:86
Security rulesNo new WARNING or ERROR findings from security rules. 19 existing findings tracked in
|
A hard crash fires no onClose, so a dead instance's presence rows linger until expiry. On a clean boot every presenceLogs row tagged with this hostname is such an orphan (new connections are only accepted after onStart), so delete them across the console and every project, purging the list cache where rows were removed. CE only -- a Cloud host is shared across tenants on sharded databases, where a per-host sweep does not apply (#14343). Co-Authored-By: Claude Opus 4.8 <[email protected]>
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
Realtime presence is upserted once per connection and never refreshed (a per-ping refresh is a DB write per connection, which doesn't scale). So a short expiry deletes live long-lived connections, since the row can't tell a held-but-stale presence from an orphan. #14343's real cases are handled without the expiry -- the heartbeat closes silently-dead sockets and boot reconciliation clears same-host crash orphans -- so the expiry stays a long, harmless far backstop. Co-Authored-By: Claude Opus 4.8 <[email protected]>
…ffline Addresses two review findings on the hostname sweep: - Race: the detached go() runs while the server accepts connections, so a fresh presence upserted on this hostname mid-sweep could be deleted as an orphan. Capture a `$since` at boot and only sweep rows created before it; anything a live connection writes during the sweep is created after `$since` and left alone. - Side effects: the raw bulk delete bypassed the offline path, so usage was not decremented and no delete event reached other workers. Each orphan is now deleted individually and run through triggerPresenceDeleted (usage -1 + the presences.[presenceId].delete event), resiliently per row. Co-Authored-By: Claude Opus 4.8 <[email protected]>
5d0b547
into
fix-realtime-presence-connections-counter
Issue
Fixes #14343 — a realtime presence stays "online" for days after a silent network loss or a realtime crash. Neither the WebSocket adapter nor
app/realtime.phpsets a Swoole heartbeat or TCP keepalive, so a client that vanishes without a FIN is never detected,onClosenever runs, and the row lingers until its 30-dayexpiresAt.Solution
heartbeat_check_interval = 30/heartbeat_idle_time = 60. A socket that sends nothing for longer than the idle time is closed by Swoole and runs the normalonClose→releaseConnectionForUserpath, so the presence is released and thedeleteevent fires. Clients ping every ~20s (there's aping→ponghandler), well within the idle window, so a live connection is never cut. No extra DB work — only the existing close path, on an actual disconnect. This covers the issue's primary repro (silent network loss).onClose, so a dead instance's presence rows linger. On a clean boot everypresenceLogsrow tagged with thishostnameis such an orphan (new connections are only accepted afteronStart), so they're deleted across the console and every project, purging the list cache where rows were removed. CE only — a Cloud host is shared across tenants on sharded databases, where a per-host sweep doesn't apply.Alternatives Discovered
Known residual
A host that crashes and is replaced by a different hostname (reschedule / autoscale) leaves orphans the same-hostname boot sweep won't catch; those lean on the 30-day expiry. Tightening it needs a cross-container liveness check (against the live-container stats), not a shorter expiry — a follow-up, still no per-connection writes.