Skip to content

Fix Swoole coroutine server lifecycle#231

Merged
ChiragAgg5k merged 1 commit into0.34.xfrom
codex/swoole-coroutine-fixes
Apr 6, 2026
Merged

Fix Swoole coroutine server lifecycle#231
ChiragAgg5k merged 1 commit into0.34.xfrom
codex/swoole-coroutine-fixes

Conversation

@ChiragAgg5k
Copy link
Copy Markdown
Member

Summary

  • stop spawning an extra nested coroutine for every HTTP request
  • clear the request-scoped DI container from coroutine context after each request
  • simplify coroutine server startup so the adapter runs directly in the active coroutine runtime

Why

Appwrite's switch to the coroutine HTTP adapter exposed a few lifecycle issues in the upstream adapter:

  1. Each request was wrapped in an extra go(...), which made request execution less predictable and diverged from the regular Swoole adapter.
  2. The request container stored in coroutine context was never cleared, so request-scoped resources could leak across later work in the same coroutine.
  3. Startup was also wrapped in go(...), even though this adapter is intended to run inside an existing coroutine runtime.

These changes keep the adapter coroutine-only, but make its request lifecycle explicit and consistent.

Validation

  • composer format src/Http/Adapter/SwooleCoroutine/Server.php
  • composer lint src/Http/Adapter/SwooleCoroutine/Server.php
  • php -l src/Http/Adapter/SwooleCoroutine/Server.php
  • vendor/bin/phpstan analyse -c phpstan.neon src/Http/Adapter/SwooleCoroutine/Server.php --memory-limit=512M

@greptile-apps
Copy link
Copy Markdown

greptile-apps bot commented Apr 6, 2026

Greptile Summary

This PR fixes three lifecycle issues in the SwooleCoroutine HTTP adapter introduced in #230: it removes the superfluous nested go() per request, adds a try/finally block to clear the per-request DI container from coroutine context after each request, and drops the unnecessary go() wrapper around server startup.

  • Request handling (onRequest): each request handler now runs directly instead of being dispatched into a new child coroutine, aligning it with the regular Swoole adapter's behaviour and making execution order predictable.
  • Context cleanup (onRequest): the new try/finally ensures the __utopia_http_request_container key is removed from coroutine context even when the callback throws, preventing request-scoped resource leaks across coroutine reuse.
  • Startup (start): the onStartCallback and server->start() are now called directly in the active coroutine rather than being wrapped in a new go(), which matches the stated intent that this adapter should run inside an existing coroutine runtime.
  • Minor: getContainer() omits the Coroutine::getCid() !== -1 guard that the sibling Swoole/Server.php uses, which can produce a PHP warning if called outside a coroutine context.

Confidence Score: 5/5

Safe to merge; all three stated lifecycle fixes are correctly implemented with no blocking defects.

The only remaining finding is a missing defensive getCid() guard in getContainer() — a P2 style suggestion that does not affect normal operation since the adapter is coroutine-only by design.

No files require special attention; the single changed file src/Http/Adapter/SwooleCoroutine/Server.php looks correct overall.

Important Files Changed

Filename Overview
src/Http/Adapter/SwooleCoroutine/Server.php Fixes request lifecycle: removes nested go() wrapping, adds try/finally cleanup for coroutine context, and simplifies startup; getContainer() is missing a defensive Coroutine::getCid() guard present in the sibling adapter

Reviews (1): Last reviewed commit: "Fix Swoole coroutine server lifecycle" | Re-trigger Greptile

@ChiragAgg5k ChiragAgg5k merged commit d3e4143 into 0.34.x Apr 6, 2026
5 checks passed
@ChiragAgg5k ChiragAgg5k deleted the codex/swoole-coroutine-fixes branch April 6, 2026 04:40
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.

1 participant