Skip to content

fix(messaging): send every multi request instead of failing past the pool size - #14238

Merged
ChiragAgg5k merged 2 commits into
mainfrom
fix/messaging-pool-exhaustion
Oct 7, 2026
Merged

ChiragAgg5k merged 2 commits into
mainfrom
fix/messaging-pool-exhaustion

Conversation

@ChiragAgg5k

@ChiragAgg5k ChiragAgg5k commented Oct 7, 2026 •

Copy link
Copy Markdown
Member

What does this PR do?

requestMulti() spawned one coroutine per request (up to 5000 per FCM batch) against a pool capped at MAX_CONCURRENT_REQUESTS = 25 with timeout: 0.0. Every request after the 25th failed instantly with a pool-exhausted error, so large push messages delivered exactly 25 per batch and marked the rest as failed.

It now runs at most 25 worker coroutines that drain a shared cursor. Each worker holds one pooled client at a time, so acquisition never waits and memory no longer scales with the recipient count.

Production impact (cloud fra1): about 100 push jobs a day since at least 2026-09-16 report delivered_total of exactly 25 or 50, with thousands of errors. On 2026-10-07, worker-messaging was OOMKilled at 512Mi after a few pushes to about 6.5k tokens.

before: 5000 coroutines ──▶ pool(25, timeout 0) ──▶ 25 sent, 4975 "pool exhausted"
after:    25 workers   ──▶ pool(25)            ──▶ each worker loops until all 5000 sent

Benchmark

Run on the production image (appwrite/cloud:1.50.146, linux/amd64, 1 CPU) in a throwaway staging pod. Mock FCM endpoint on localhost with 80 ms latency, SWOOLE_HOOK_ALL, batching the same as the worker (5000 per requestMulti call), and a fresh process per case. before is the Adapter.php currently in that image.

Recipients Delivered before Delivered after Peak RSS before Peak RSS after PHP peak before PHP peak after Wall before Wall after
100 25 100 62.8 MB 62.9 MB 8 MB 8 MB 0.12 s 0.37 s
1,000 25 1,000 80.0 MB 67.9 MB 20 MB 12 MB 0.20 s 3.54 s
5,000 25 5,000 160.3 MB 94.0 MB 78 MB 38 MB 0.73 s 16.99 s
6,500 50 6,500 160.6 MB 100.1 MB 78 MB 44 MB 0.94 s 21.88 s

Every failure before the fix was Pool 'messaging' could not provide a connection within 0s. The old version is only faster because it never sends 99% of the requests. The new wall time matches 25 concurrent requests at 80 ms (6500 / 25 × 0.08 s ≈ 20.8 s).

Four 6,500-recipient jobs in one process: RSS settles at about 116 MB before the fix and about 100 MB after, with no growth from job to job. The cost per worker process is about +100 MB before the fix and about +40 MB after, over a ~60 MB baseline. Before the fix, each of the 3 processes on a pod pays it, which matches the steps seen in production before the OOM (167 → 267 → 365 MB).

Test Plan

  • Benchmark above, run on the production image against a mock FCM endpoint.
  • bin/monorepo check messaging and bin/monorepo test messaging --linked pass locally.

Related PRs and Issues

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?

Related: #14239

…pool size

requestMulti spawned one coroutine per request against a 25-slot pool with a zero acquisition timeout, so every request after the 25th failed instantly. Large FCM pushes delivered 25 per 5000-token batch and the failed coroutines held enough memory to OOMKill worker-messaging.
@luxor-code-review

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

Copy link
Copy Markdown

Review complete. 🟡 1 medium

💬 Inline comments (1)


The change replaces the one-coroutine-per-request fan-out in Adapter::requestMulti with a fixed pool of worker coroutines that pull indexes from a shared counter, then sorts the collected results with ksort to restore deterministic ordering. A new RequestMultiTest drives 60 requests through a slow fake client and asserts all are delivered with 200s and bounded peak concurrency.

Files Change
packages/messaging/src/Adapter.php Rewrites requestMulti from per-request coroutines to MAX_CONCURRENT_REQUESTS worker coroutines draining a shared $next index counter, with ksort($results) after wait() for deterministic ordering.
packages/messaging/tests/Adapter/RequestMultiTest.php Adds a regression test using a SlowClient fake and a MultiAdapter subclass to verify 60 requests all complete, succeed, and never exceed the pool's concurrency cap.

Reviewed commit: fc516b6

@ghost

ghost commented Oct 7, 2026

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The worker loop preserves per-request error handling and matches the pool capacity; no actionable defects were found.

Changes requestMulti() to use at most 25 worker coroutines that drain a shared request cursor, rather than creating one coroutine per request. Results are sorted by request index after all workers finish. Adds a separate-process regression test covering 60 requests, successful results, index ordering, and bounded concurrency.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 2
File Change
packages/messaging/src/Adapter.php Replaces per-request coroutines with bounded workers and sorts completed results by index.
packages/messaging/tests/Adapter/RequestMultiTest.php Adds a 60-request coroutine regression test using a slow PSR-18 client.

Reviewed fc516b6 · 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 Oct 7, 2026 •

Copy link
Copy Markdown

Security rules

No new WARNING or ERROR findings from security rules.

32 existing findings tracked in .semgrep/baseline.json
  • php.appwrite.guest-write-without-abuse-limit (17)
  • php.appwrite.permissive-write-permission (8)
  • 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.

@luxor-code-review luxor-code-review Bot 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.

Refactors requestMulti to a bounded worker-pool coroutine pattern so more than MAX_CONCURRENT_REQUESTS requests all get sent, and adds a regression test.

Key findings

$this->assertSame(\array_fill(0, 60, 200), \array_column($results, 'statusCode'));
$this->assertSame(\array_fill(0, 60, ''), \array_column($results, 'error'));
$this->assertSame(60, $client->sent);
$this->assertLessThanOrEqual(25, $client->peak);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 suggestion · medium

Add a lower bound proving test concurrency

The only concurrency assertion is assertLessThanOrEqual(25, $client->peak) (packages/messaging/tests/Adapter/RequestMultiTest.php:32), which an implementation that sends all 60 requests serially (peak == 1) also satisfies. The test verifies counts and indexes but nothing distinguishes concurrent from sequential execution, so a regression that serializes requestMulti() would pass. A lower bound on peak is what actually proves fan-out.

📋 Prompt for AI Agents

In packages/messaging/tests/Adapter/RequestMultiTest.php at line 32, change assertLessThanOrEqual(25, $client->peak) to assertSame(25, $client->peak) — the 25 worker coroutines each increment SlowClient::$active before the first Coroutine::sleep(0.01) wakes, so the peak is deterministic. Without a lower bound the test passes even if requestMulti() regresses to fully serial sending.

@github-actions

github-actions Bot commented Oct 7, 2026

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/messaging-pool-exhaustion (after).

Metric Before After Change
🚀 Requests/sec 203.37 210.78 ⚪ +3.6%
⏱️ Latency P50 83.13 ms 82.42 ms ⚪ -0.9%
⏱️ Latency P95 204.59 ms 190.1 ms 🟢 -7.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 82.42 190.1 13,395 210.78 -14.49
Account 153.34 288 705 11.73 -22.71
TablesDB 80.22 153.09 7,285 117.9 -19.79
Storage 75.02 162.4 3,525 58.44 -9.09
Functions 116.49 225.26 1,880 31.75 -3.7

Top API waits (after)

API request Max wait (ms)
account.name.update 437.1
tokens.delete 416.22
account.prefs.update 387.94
functions.create 384.67
functions.variables.update 374.2

@ChiragAgg5k
ChiragAgg5k merged commit 0c08f3d into main Oct 7, 2026
104 of 105 checks passed
@ChiragAgg5k
ChiragAgg5k deleted the fix/messaging-pool-exhaustion branch October 7, 2026 18:54
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.

3 participants