Skip to content

Stop replaced callback fibers from running the event loop queues - #140

Merged
kelunik merged 7 commits into
revoltphp:mainfrom
lav45:fix/issue-139-flat-resume-trace
Nov 3, 2026
Merged

kelunik merged 7 commits into
revoltphp:mainfrom
lav45:fix/issue-139-flat-resume-trace

Conversation

@lav45

@lav45 lav45 commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #139

Problem

When a callback suspends, invokeCallbacks() replaces $callbackFiber with a new fiber, but the suspended fiber isn't
told about it. Once resumed and done with its callback, it stays in the loop of createCallbackFiber() and runs the
next microtask itself. That microtask resumes the next fiber from inside this one, so N fibers resumed in the same tick
nest N levels deep.

PHP backtraces follow the chain of resuming fibers, so an exception created in the last fiber has about 4N frames. If
each fiber keeps its exception across a suspension, N exceptions of O(N) frames are alive at once and memory grows as
O(N²).

Fix

A fiber that is no longer $callbackFiber now terminates after its callback, and control returns to the fiber that
resumed it. That fiber carries on with the queues, so there is only ever one fiber draining them.

The check is the one from 4b68978 (\Fiber::getCurrent() !== $this->callbackFiber). That commit used it on the
interrupt path only, and 9ea748a reverted it because it wasn't the source of a circular reference, not because it caused
a problem.

Other schedulers work the same way: a finished task doesn't start the next one on its own stack. Go exits through
mcall(goexit0) to schedule() on g0, and a gevent greenlet returns to the hub, which is always its parent. Kotlin's
executeUnconfined queues a nested resume into the already active loop to avoid stack overflow. Ruby's async avoids
the nesting with Fiber#transfer, which PHP doesn't have.

Measurements

Script from #139, PHP 8.5.12, zend.exception_ignore_args=1. Memory is memory_get_usage() with all exceptions held.

Fibers Trace depth, first Trace depth, last (before) Trace depth, last (after) Memory (before) Memory (after)
100 14 410 14 11.5 MB 3.7 MB
250 14 1010 14 52.7 MB 4.6 MB
500 14 2010 14 197.1 MB 6.1 MB
1000 14 4010 14 770.4 MB 9.1 MB

Cost

PHP 8.5, median of 7 interleaved runs per case, in ms. The callbacks are empty, so the loop's own overhead is all that's
measured.

Case main After fix Change
1 000 000 microtasks, none suspends 216 221 +2.3%
1 000 000 defer() callbacks, none suspends 554 569 +2.7%
One fiber suspended and resumed 200 000 times, one by one 382 400 +4.7%
1000 fibers resumed in the same tick, 100 rounds 1932 2009 +4.0%

Callbacks that don't suspend pay for one comparison of the callback fiber after each callback. The callback fiber keeps
itself in a local variable for that, because calling Fiber::getCurrent() in a method each time cost 9-12% in the first
two cases. Fibers resumed in the same tick also pay for one more fiber switch each, because control returns to the
resuming fiber between resumes.

Tests

  • Added to test/EventLoopTest.php:
    • testResumedQueueFibersDoNotNestInEachOther, testResumedCallbackFibersDoNotNestInEachOther and
      testResumedQueueFiberLeavesCallbacksToCurrentCallbackFiber fail without the fix.
    • testResumedFibersKeepMicrotaskOrder and testExceptionFromResumedFiberStopsNextMicrotask pass with and without
      it. They pin down the behavior that must not change.
  • The full suite passes on PHP 8.1 and 8.5. The ev, event and uv driver tests were skipped locally because those
    extensions weren't installed, so they still need a CI run.

A fiber suspended by a callback is replaced as the callback fiber, but
it stayed in the callback fiber loop once resumed. After finishing its
callback it ran the next microtask itself, so fibers resumed in the same
tick nested inside each other. A trace in the last one grew by about 4
frames per fiber, and exceptions held across a suspension cost O(N^2)
memory.

A replaced fiber now terminates after its callback and returns to the
fiber that resumed it, which carries on with the queues. Microtask and
callback order is unchanged.

Fixes revoltphp#139

@kelunik kelunik left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks! Most callbacks shouldn't suspend anyway, so the performance change is only if they suspend, no?

lav45 added 2 commits November 3, 2026 11:06
…::getCurrent()

The identity check runs after every microtask and callback. As a method
calling Fiber::getCurrent() it cost 9-12% on callbacks that don't
suspend. The callback fiber now keeps itself in a local variable and
compares it with the callback fiber property, which brings the cost
down to 2-3%.
@lav45

lav45 commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

@kelunik Not only. Measuring it showed the identity check itself cost 9-12% on empty callbacks that don't suspend, because it called Fiber::getCurrent() in a method after every callback. 4e9acb6 captures the callback fiber once in a local variable and compares it with the property, which brings that down to 2-3%. Fibers resumed in the same tick additionally pay one extra switch each. Numbers are in the updated description.

Comment thread test/EventLoopTest.php Resolved
lav45 and others added 4 commits November 3, 2026 22:57
Cover replaced fibers resumed directly via Fiber::resume() from {main}
and from another callback, an exception thrown after such a resume, and
a fiber replaced by a suspending destructor of a cancelled callback.
Also assert that all resumed fiber trace depths are equal.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kelunik
kelunik merged commit b6baa63 into revoltphp:main Nov 3, 2026
7 checks passed
@lav45
lav45 deleted the fix/issue-139-flat-resume-trace branch November 3, 2026 20:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Exception trace depth grows with the number of fibers resumed in the same tick, so holding those exceptions costs O(N²) memory

2 participants