Repository navigation
Stop replaced callback fibers from running the event loop queues - #140
Merged
Merged
Conversation
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
reviewed
Nov 3, 2026
kelunik
left a comment
Member
There was a problem hiding this comment.
Thanks! Most callbacks shouldn't suspend anyway, so the performance change is only if they suspend, no?
…::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%.
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. |
kelunik
reviewed
Nov 3, 2026
kelunik
approved these changes
Nov 3, 2026
…t run inside the resumed fiber
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #139
Problem
When a callback suspends,
invokeCallbacks()replaces$callbackFiberwith a new fiber, but the suspended fiber isn'ttold about it. Once resumed and done with its callback, it stays in the loop of
createCallbackFiber()and runs thenext 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
$callbackFibernow terminates after its callback, and control returns to the fiber thatresumed 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 theinterrupt 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)toschedule()ong0, and a gevent greenlet returns to the hub, which is always its parent. Kotlin'sexecuteUnconfinedqueues a nested resume into the already active loop to avoid stack overflow. Ruby'sasyncavoidsthe nesting with
Fiber#transfer, which PHP doesn't have.Measurements
Script from #139, PHP 8.5.12,
zend.exception_ignore_args=1. Memory ismemory_get_usage()with all exceptions held.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.
defer()callbacks, none suspendsCallbacks 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 firsttwo 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
test/EventLoopTest.php:testResumedQueueFibersDoNotNestInEachOther,testResumedCallbackFibersDoNotNestInEachOtherandtestResumedQueueFiberLeavesCallbacksToCurrentCallbackFiberfail without the fix.testResumedFibersKeepMicrotaskOrderandtestExceptionFromResumedFiberStopsNextMicrotaskpass with and withoutit. They pin down the behavior that must not change.
ev,eventanduvdriver tests were skipped locally because thoseextensions weren't installed, so they still need a CI run.