Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alternative fix for callbacks queued in destructors #100

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
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
34 changes: 22 additions & 12 deletions src/EventLoop/Internal/AbstractDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ abstract class AbstractDriver implements Driver

private readonly \Closure $interruptCallback;
private readonly \Closure $queueCallback;

/** @var \Closure():(null|\Closure(): mixed) */
private readonly \Closure $runCallback;

private readonly \stdClass $internalSuspensionMarker;
Expand Down Expand Up @@ -87,12 +89,23 @@ public function __construct()
/** @psalm-suppress InvalidArgument */
$this->interruptCallback = $this->setInterrupt(...);
$this->queueCallback = $this->queue(...);
$this->runCallback = function () {
if ($this->fiber->isTerminated()) {
$this->createLoopFiber();
}
$this->runCallback = function (): ?\Closure {
do {
if ($this->stopped) {
return null;
}

if ($this->fiber->isTerminated()) {
$this->createLoopFiber();
}

return $this->fiber->isStarted() ? $this->fiber->resume() : $this->fiber->start();
$result = $this->fiber->isStarted() ? $this->fiber->resume() : $this->fiber->start();
if ($result) { // Null indicates the loop fiber terminated without suspending.
return $result;
}
} while (!$this->stopped && \gc_collect_cycles());
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this need an extra && !$this->stopped check after the gc_collect_cycles() call?


return null;
};
}

Expand All @@ -106,17 +119,14 @@ public function run(): void
throw new \Error(\sprintf("Can't call %s() within a fiber (i.e., outside of {main})", __METHOD__));
}

if ($this->fiber->isTerminated()) {
$this->createLoopFiber();
}

/** @noinspection PhpUnhandledExceptionInspection */
$lambda = $this->fiber->isStarted() ? $this->fiber->resume() : $this->fiber->start();
$lambda = ($this->runCallback)();

if ($lambda) {
$lambda();

throw new \Error('Interrupt from event loop must throw an exception: ' . ClosureHelper::getDescription($lambda));
throw new \Error(
'Interrupt from event loop must throw an exception: ' . ClosureHelper::getDescription($lambda)
);
}
}

Expand Down
124 changes: 124 additions & 0 deletions test/EventLoopTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,130 @@

class EventLoopTest extends TestCase
{
public function testSuspensionResumptionWithQueueInGarbageCollection(): void
{
$suspension = EventLoop::getSuspension();

$class = new class ($suspension) {
public function __construct(public Suspension $suspension)
{
}
public function __destruct()
{
$this->suspension->resume(true);
}
};
$cycle = [$class, &$cycle];
unset($class, $cycle);

$ended = $suspension->suspend();

$this->assertTrue($ended);
}

public function testEventLoopResumptionWithQueueInGarbageCollection(): void
{
$suspension = EventLoop::getSuspension();

$class = new class ($suspension) {
public function __construct(public Suspension $suspension)
{
}
public function __destruct()
{
EventLoop::queue($this->suspension->resume(...), true);
}
};
$cycle = [$class, &$cycle];
unset($class, $cycle);

$ended = $suspension->suspend();

$this->assertTrue($ended);
}


public function testSuspensionResumptionWithQueueInGarbageCollectionNested(): void
{
$suspension = EventLoop::getSuspension();

$resumer = new class ($suspension) {
public function __construct(public Suspension $suspension)
{
}
public function __destruct()
{
$this->suspension->resume(true);
}
};

$class = new class ($resumer) {
public static ?object $staticReference = null;
public function __construct(object $resumer)
{
self::$staticReference = $resumer;
}
public function __destruct()
{
EventLoop::queue(function () {
$class = self::$staticReference;
$cycle = [$class, &$cycle];
unset($class, $cycle);

self::$staticReference = null;
});
}
};
$cycle = [$class, &$cycle];
unset($class, $resumer, $cycle);


$ended = $suspension->suspend();

$this->assertTrue($ended);
}

public function testEventLoopResumptionWithQueueInGarbageCollectionNested(): void
{
$suspension = EventLoop::getSuspension();

$resumer = new class ($suspension) {
public function __construct(public Suspension $suspension)
{
}
public function __destruct()
{
EventLoop::queue($this->suspension->resume(...), true);
}
};

$class = new class ($resumer) {
public static ?object $staticReference = null;
public function __construct(object $resumer)
{
self::$staticReference = $resumer;
}
public function __destruct()
{
EventLoop::queue(function () {
$class = self::$staticReference;
$cycle = [$class, &$cycle];
unset($class, $cycle);

self::$staticReference = null;
});
}
};
$cycle = [$class, &$cycle];
unset($class, $resumer, $cycle);


$ended = $suspension->suspend();

$this->assertTrue($ended);
}


public function testDelayWithNegativeDelay(): void
{
$this->expectException(\Error::class);
Expand Down
Loading