Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<testsuites>
<testsuite name="Main">
<directory>test</directory>
<directory suffix=".phpt">test</directory>
</testsuite>
</testsuites>
</phpunit>
11 changes: 8 additions & 3 deletions src/EventLoop/Internal/AbstractDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -498,9 +498,14 @@ private function invokeCallbacks(): void
{
while (!$this->microtaskQueue->isEmpty() || !$this->callbackQueue->isEmpty()) {
/** @noinspection PhpUnhandledExceptionInspection */
$yielded = $this->callbackFiber->isStarted()
? $this->callbackFiber->resume()
: $this->callbackFiber->start();
if ($this->callbackFiber->isSuspended()) {
$yielded = $this->callbackFiber->resume();
} else {
if ($this->callbackFiber->isTerminated()) {
$this->createCallbackFiber();
}
$yielded = $this->callbackFiber->start();
}

if ($yielded !== $this->internalSuspensionMarker) {
$this->createCallbackFiber();
Expand Down
38 changes: 38 additions & 0 deletions test/event_loop_destruction_order.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
--TEST--
Issue #105: Ensure the callback fiber is always alive as long as the event loop lives
--FILE--
<?php

use Revolt\EventLoop;

require 'vendor/autoload.php';

final class a {
private static self $a;
public static function getInstance(): self {
return self::$a ??= new self;
}

public function __destruct()
{
echo "Destroying ", self::class, "\n";
$suspension = EventLoop::getSuspension();
EventLoop::delay(1.0, $suspension->resume(...));
$suspension->suspend();
echo "Finished " . self::class, "\n";
}
}

EventLoop::defer(function () {
echo "start\n";
});

a::getInstance();

EventLoop::run();

?>
--EXPECT--
start
Destroying a
Finished a
Loading