← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

Coroutines — Cooperative Multitasking

concurrency PHP 8.1+ Advanced
debt(d7/e5/b7/t7)
d7 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'only careful code review or runtime testing' (d7). The detection_hints list Blackfire and PHPStan, but note automated: no — blocking I/O in event loops or mixing sync/async code without proper bridging is not automatically caught by these tools. You typically discover issues through runtime performance problems or code review. Blackfire can show where time is spent but won't flag coroutine misuse directly.

e5 Effort Remediation debt — work required to fix once spotted

Closest to 'touches multiple files / significant refactor in one component' (e5). The quick_fix suggests using async frameworks (Amp v3, ReactPHP) rather than raw Fibers, but retrofitting blocking I/O calls across a codebase to use async alternatives requires touching multiple files and understanding the async runtime model. It's not a one-line fix but also not architectural rework.

b7 Burden Structural debt — long-term weight of choosing wrong

Closest to 'strong gravitational pull' (b7). Coroutines apply to web, cli, and queue-worker contexts per applies_to, and once adopted, every I/O operation must be async-aware. The choice of async framework (Amp, ReactPHP) shapes how all code interacts with I/O, database, HTTP clients, etc. Every future maintainer must understand the cooperative scheduling model and ensure no blocking calls leak in.

t7 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'serious trap' (t7). The misconception states developers believe coroutines provide parallelism when they only provide concurrency — this directly contradicts how threads/processes work elsewhere. Developers coming from thread-based concurrency expect simultaneous execution on multiple CPUs but get interleaved execution on one core. The common_mistakes reinforce this: expecting CPU parallelism, blocking I/O freezing the entire loop, deeply recursive stack overflows.

About DEBT scoring →

Also Known As

coroutine cooperative multitasking Fiber PHP green thread

TL;DR

Functions that explicitly yield control — enabling concurrent I/O without threads, where code decides when to pause rather than being preemptively interrupted.

Explanation

Coroutines pause at explicit yield points and resume from the same point later. Unlike threads (preemptive — OS interrupts at any time), coroutines yield explicitly (cooperative — code decides when to pause). PHP 8.1 Fibers implement stackful coroutines. Coroutines enable concurrent I/O without threads: Fiber A waits for a DB query → suspends → Fiber B makes an HTTP request → suspends → Fiber A resumes when the DB responds. The event loop schedules which coroutine runs next.

Common Misconception

Coroutines provide parallelism — coroutines provide concurrency (interleaved execution on one CPU) not parallelism (simultaneous execution on multiple CPUs); two coroutines still run on one core.

Why It Matters

10,000 coroutines use the same memory as 100 threads — enabling high-concurrency PHP services without the overhead of process or thread-per-request models.

Common Mistakes

  • Blocking I/O inside coroutines — blocks the entire event loop
  • Expecting CPU parallelism from coroutines — use processes for that
  • Not propagating cancellation when parent coroutine is cancelled
  • Stack overflows in deeply recursive stackful coroutines

Code Examples

✗ Vulnerable
// Blocking I/O — all other Fibers wait:
$fiber = new Fiber(function(): void {
    $data = file_get_contents('https://api.example.com'); // BLOCKS!
    // During this HTTP wait, no other Fiber can run
    echo $data;
});
✓ Fixed
// Non-blocking with explicit yield:
$fiber = new Fiber(function() use ($httpClient): void {
    $promise = $httpClient->getAsync('https://api.example.com');
    $result  = Fiber::suspend($promise); // Yield control — event loop runs others
    echo $result; // Resume here when the response arrives
});

Added 16 Mar 2026
Edited 5 Apr 2026
Views 29
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings F 2 pings S 1 ping S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 2 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 0 pings S 0 pings M 1 ping T 0 pings W 0 pings T 0 pings F 1 ping S
No pings yesterday
Amazonbot 9 Perplexity 6 Unknown AI 2 Ahrefs 2 ChatGPT 1 Google 1
crawler 21
DEV INTEL Tools & Severity
🟡 Medium ⚙ Fix effort: High
⚡ Quick Fix
In PHP 8.1+, Fibers are coroutines — use them via an async framework (Amp v3, ReactPHP) rather than directly; in Python/JavaScript, use async/await which compiles to coroutines
📦 Applies To
PHP 8.1+ any web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
Blocking I/O in event loop coroutine; mixing sync and async code without proper bridging; new Fiber() without scheduler
Auto-detectable: ✗ No blackfire phpstan
⚠ Related Problems
🤖 AI Agent
Confidence: Low False Positives: Medium ✗ Manual fix Fix: High Context: File Tests: Update

✓ schema.org compliant