Repository navigation
Conversation
Blocked on a utopia-php/websocket release carrying utopia-php/websocket#31; needs the composer bump before merge. A client that stops draining makes the Swoole reactor buffer every undelivered frame in process memory without limit, while push() keeps returning true. Reproduced in the production image: 300 non-draining connections fanned 2930MB and grew RSS by 2.27GB, all of it in the reactor, none in the PHP worker (peak PHP heap 4MB). That is what OOMKills realtime containers, and it is invisible to every application-level metric -- during a real step the message rate was 21/s, below a 74/s peak that cost nothing. Capping bounds the worst case at size x connections. 512KB against ~1200 connections per container is ~600MB of buffers, which fits a 1GiB limit alongside a ~190MB baseline. Exposed as _APP_REALTIME_SOCKET_BUFFER_SIZE so it can be tuned during an incident without a release: raise it if legitimate slow-link clients are being disconnected, lower it if containers still run out of memory. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
Follows utopia-php/websocket#31 moving it from a setter to a constructor argument: Swoole reads the config once at start(), so a later setter would silently do nothing. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The library now defaults the cap to 512KB, so the env override falls back to Adapter\Swoole::DEFAULT_SOCKET_BUFFER_SIZE rather than repeating the number here and risking drift. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2 tasks done
2 tasks done
1 of 2 tasks
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Draft — blocked on a
utopia-php/websocketrelease carrying utopia-php/websocket#31, plusthe composer bump. The code is otherwise complete.
Root cause fix for repeated Appwrite Cloud realtime OOMKills (fra1: OnCall #1098, #1175, and
further kills on 2026-08-28 and 2026-08-31).
The bug
A websocket client that stops draining makes the Swoole reactor buffer its undelivered frames
in process memory while
push()keeps returningtrue. Swoole caps that atSW_SOCKET_BUFFER_SIZE— 8MB per connection. For the ~1200 connections a realtimecontainer holds, that is a 9.6GB ceiling against a 1GiB limit.
Measured in the production image (PHP 8.5.9, Swoole 6.2.2, Alpine), 300 connections that never
recv():All of it in the reactor, none in the PHP worker: peak PHP heap 4MB against a 128M
memory_limit. That is why it never looked like a leak and why no application-level changemade any difference.
It was also invisible to the metrics we had. During a real production step the message rate
was 21/s — below a 74/s peak 70 minutes earlier that cost nothing. What moved was bytes
fanned out to clients that could not keep up.
The fix
Pass a per-connection cap at construction. Exposure becomes
size × connections, andpush()returns
falsefor a connection that is over — which utopia-php/websocket#31 turns into aclose, so the client reconnects and resubscribes from a known state.
Verified end to end through
Server+Adapter\Swoole, 300 non-draining connections, 200events of 50KB:
0(Swoole's 8MB)Sizing
512KB × ~1200 connections bounds buffers at ~600MB, which fits a 1GiB limit alongside the
~190MB steady-state baseline. That is the worst case with every connection simultaneously at
the cap; realistically only a slow subset ever is.
The default comes from
Adapter\Swoole::DEFAULT_SOCKET_BUFFER_SIZErather than being repeatedhere, so the two repos cannot drift.
_APP_REALTIME_SOCKET_BUFFER_SIZEoverrides it so thevalue can be tuned during an incident without a release — raise it if legitimate slow-link
clients are being disconnected, lower it if containers still run out of memory. Documented in
app/config/variables.phpand passed through indocker-compose.yml.Behaviour change
A client far enough behind is now disconnected instead of having up to 8MB held on its behalf.
For a realtime protocol that is the recoverable failure — it reconnects and resubscribes. The
alternative is silent event loss: in a capped-but-not-closed run, 37500 events vanished from
clients that stayed connected and never learned.
Testing
docker compose configvalidates. The measurements drive a realSwoole\WebSocket\Serverinthe production image — the failure lives entirely in the reactor's handling of a socket that
will not accept writes, which a stubbed server cannot exhibit. There is no e2e coverage for
slow-consumer behaviour in
tests/e2e/Services/Realtime/*; adding one needs a client thatdeliberately stops reading, which is worth doing separately.
🤖 Generated with Claude Code