Repository navigation
fix(network): resolve hostnames natively in coroutines to stop RemoteObject leak - #13952
Conversation
…Object leak Under SWOOLE_HOOK_ALL, Swoole 6.2 routes dns_get_record() through a RemoteObject client that is created per call and kept forever in a static registry, retaining ~140 KiB per lookup. PublicHostname runs on every webhook delivery, which OOM-killed worker-webhooks repeatedly in cloud nyc3.
🟢 Tier S · Ready to merge
Hostname resolution inside Swoole coroutines now uses native Latest changes: The newest commit isolates the coroutine tests in separate processes, allows either private localhost address in the resolution assertion, and measures memory growth per lookup.
📂 Walkthrough · 2
Reviewed the commits since |
|
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
AGENTS.md forbids Swoole coroutine work in the shared unit process, and the exact loopback address depends on the host resolver configuration. Bound the leak per lookup so allocator noise cannot fail the test.
|
An additional data point from 2.3.0 (released before this fix), self-hosted, Besides the memory growth, every webhook delivery keeps one
Only restarting the worker recovers. On 2.3.0 this happens long before the memory limit, after a single burst of ~1,000 events. It may be worth a note in the release notes or a backport to 2.3.x. |
What does this PR do?
PublicHostname::resolve()now usesSwoole\Coroutine\System::getaddrinfo()when it runs inside a coroutine, instead ofdns_get_record(). The fix is for a memory leak that has been OOM-killingworker-webhooksin Cloud nyc3 (13 kills in 7 days; the pod reaches its 1 GiB limit within about 3–12 hours).Under
SWOOLE_HOOK_ALL, Swoole 6.2 hooksdns_get_record()toswoole_dns_get_record(). That function callsswoole_get_default_remote_object_client(), which runsnew Swoole\RemoteObject\Client(...)on every call. The client's constructor stores itself in the staticClient::$clientsarray, and nothing removes it. Each lookup therefore keeps a client and its coroutine HTTP client alive, about 140 KiB, until the process exits.PublicHostnameruns before every production webhook delivery (Webhooks::execute). It also runs in the avatars image, favicon and screenshot endpoints. Memory grows with the number of hostname lookups, not with job count, so in production it looked as if the leak depended on the destination. In nyc3 one project's webhook bursts (585 / 1,448 / 544 jobs per 15 min) lined up with memory jumps of +216 / +258 / +66 MiB. The rareSwoole\RemoteObject\Exception: No such file or directorydelivery failures come from the same proxy.getaddrinfo()resolves in Swoole's thread pool, returns every A/AAAA address (the SSRF check still sees all of them), and keeps no memory. It needs a non-empty service argument: with''it returnsfalsewithEAI_SERVICE(-8), so the code passes'80', which only selects a port and doesn't affect which addresses come back. Outside a coroutine the existingdns_get_record()path is unchanged.Benchmark
Environment:
appwrite/base:2.1.0(PHP 8.5, Swoole 6.2.2),SWOOLE_HOOK_ALL, 30 concurrent coroutines (matching_APP_WORKER_MAX_COROUTINES=30), onenew PublicHostname()->isValid('example.com')per simulated delivery, as the webhooks worker does. The heap and RSS deltas are measured aftergc_collect_cycles().Before (main)
After (this PR)
Before the fix, heap grows linearly at about 140 KiB per lookup and one retained client per call. After it, nothing is retained and throughput is equal or better. Resolution results are identical: every lookup validated in both runs. (The 1000 row shows 1020 because the loop runs in batches of 30.)
Benchmark script
Run:
docker run --rm -v $PWD:/app:ro -v <dir>:/s -w /app --entrypoint php appwrite/base:2.1.0 -d memory_limit=-1 /s/bench.php 6000 example.comTest Plan
testResolvingInsideCoroutineRetainsNoMemoryPerLookupdoes 200 lookups inside a hooked coroutine and asserts that less than 4 KiB is retained per lookup (the leak is about 140 KiB). I watched it fail against main's implementation (Failed asserting that 142636.8 is less than 4096.) and pass with the fix.testResolvesHostnameInsideCoroutinechecks that the coroutine path still resolves and rejects a hostname that points at a private address (localhost).#[RunInSeparateProcess], so Swoole hook flags never leak into the shared unit process (AGENTS.md).tests/unit/Network/Validators/PublicHostnameTest.php: 40 tests, 91 assertions, all passing inappwrite/base:2.1.0.pint,phpstanandrector --dry-runare clean on the changed files.worker-webhooksmemory in Cloud nyc3 after this reaches Cloud throughcomposer update appwrite/server-ce.Related PRs and Issues
swoole_get_default_remote_object_client()builds a new client per call, andRemoteObject\Clientnever removes itself fromClient::$clients. That also affects hookedgethostbyaddr,checkdnsrr,getmxrrandmail, none of which Appwrite calls in coroutines today.Checklist