Repository navigation
fix(telemetry): serialize overlapping collects under Swoole - #14732
Conversation
A worker's 60s collect tick can still be exporting when workerStop flushes
again. Both exports share the exporter's cURL handle, and Swoole aborts the
process ("This cURL handle is currently executing in coroutine#N"), so a
KEDA worker Job that already finished its message exits 255 and is marked
Failed.
|
Review complete. 🟡 1 medium 💬 Inline comments (1)
🧹 Nitpicks (1) — 🟢 1 low
This PR makes
Reviewed commit: 956b81f |
🟢 Tier S · Ready to merge
The telemetry adapter now serializes overlapping Swoole collections, with a pooled coroutine-native OTLP transport and retry handling for dropped pooled connections. The package declares PHP 8.3 and the lock dependency, and updates metric export behavior and tests; the latest commits apply readonly declarations to immutable state and type the mock server constant. Latest changes: The latest commits apply PHP 8.3 readonly declarations to immutable exporter, transport, adapter-test, and test-double state, and add a type to the mock server's HOST constant.
📂 Walkthrough · 9
Merged |
Security rulesNo new WARNING or ERROR findings from security rules. 19 existing findings tracked in
|
There was a problem hiding this comment.
Adds coroutine-aware collection locking to the OpenTelemetry telemetry adapter so overlapping collect() calls skip instead of interleaving, with tests.
Key findings
- 🟡
Channel::pushblocks instead of skipping on contention — OpenTelemetry.php:322
| if (!$this->collecting->push(true)) { | ||
| return false; | ||
| } |
There was a problem hiding this comment.
🟡 bug · medium
Channel::push blocks instead of skipping on contention
The mutex at packages/telemetry/src/Adapter/OpenTelemetry.php:322 calls $this->collecting->push(true) with Swoole's default timeout of -1, which suspends the coroutine when the capacity-1 channel is already full rather than returning false. The return false skip branch is therefore unreachable under contention (only a closed channel yields it), and overlapping callers — the 60s Timer::tick export racing another tick or a shutdown flush — queue behind a hung export, one leaked coroutine per tick with no cap. Callers also never observe the documented skip outcome: false conflates skipped-in-flight with export failure.
📋 Prompt for AI Agents
In packages/telemetry/src/Adapter/OpenTelemetry.php lines 322-324, change $this->collecting->push(true) to $this->collecting->push(true, 0) so the capacity-1 channel fails fast when already held instead of suspending the calling coroutine indefinitely, making the return false skip branch reachable. Then in packages/telemetry/tests/Adapter/OpenTelemetryTest.php (testOverlappingCollectsExportOneAtATime), update the overlapping collect to assert it returns false with no additional send while the first collect is still in flight (use a transport whose send blocks until released), keeping the assertion that the first collect exports exactly once.
There was a problem hiding this comment.
Blocking is intended. The case this fixes is shutdown: workerStop must wait for the in-flight tick and then send its final flush; skipping would drop the last interval's metrics. Overlap can't build up either: a tick fires every 60s and an export times out well inside that, so at most one waiter exists. false comes back only when the waiting coroutine is cancelled (e.g. KubernetesJob cancelling stragglers on exit), and nothing was exported in that case.
There was a problem hiding this comment.
The use-case argument is reasonable, but it doesn't fully resolve the original concern. If blocking is intentional for the shutdown path, the current code silently blocks all callers indefinitely — including the timer tick racing another tick — with no timeout and no cap on waiters. The return false branch is still unreachable under contention (it only fires on a closed channel), so the code's own documented skip behavior is never exercised in practice.
If the intent is 'shutdown waits, tick skips', those two callers need different treatment — e.g. pass a $wait flag or use push(true, 0) for ticks and push(true, -1) for shutdown. As written, both callers get the blocking path, meaning a hung export during a tick will queue the next tick behind it indefinitely.
The finding remains open until the diff shows either a non-zero timeout for the tick path or a test demonstrating the skip branch is reachable.
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
…P 8.3 Mutex opens a Swoole Channel in its constructor, so building it eagerly broke the adapter for consumers without ext-swoole. utopia-php/lock needs PHP 8.3, so the package now declares that minimum.
The findings that requested changes are resolved. See the summary comment for what is still open.
…ent-collect fix(telemetry): serialize overlapping collects under Swoole (cherry picked from commit 7e9ce20)
What
OpenTelemetry::collect()now runs one export at a time inside a Swoole coroutine, using aUtopia\Lock\Mutex. A second caller waits for the in-flight export instead of sharing the HTTP client with it. Outside coroutines nothing changes.Why
fra1 prod,
worker-migrations-xwr8x(cloud 1.51.21): the migration succeeded (Backup succeeded, span finished at 58s), then the pod died with exit 255 and the Job was marked Failed:collect()→ OTLP export in flight on the Symfony curl clientworkerStop→Timer::clearAll()(does not stop #6) →collect()on the same handle →Swoole\Error, exit 255Any KEDA run-to-completion worker that lives past a collect tick can hit this. HTTP/realtime processes share the same adapter.
Tests
testOverlappingCollectsExportOneAtATime: two coroutines collect against a transport that yields mid-send. Without the fix it fails (2 is identical to 1overlapping sends); with it, both export, one at a time.bin/monorepo check telemetrypasses.Related: appwrite-labs/cloud#6669