Skip to content

Commit

Permalink
Allow GC of arguments during microtask execution (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nevay authored Aug 1, 2022
1 parent cce7416 commit 4e74555
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/EventLoop/Internal/AbstractDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,8 @@ private function invokeMicrotasks(): void
[$callback, $args] = $this->microtaskQueue->dequeue();

try {
$callback(...$args);
// Clear $args to allow garbage collection
$callback(...$args, ...($args = []));
} catch (\Throwable $exception) {
$this->error($callback, $exception);
} finally {
Expand Down
18 changes: 18 additions & 0 deletions test/Driver/DriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1387,6 +1387,24 @@ public function testMicrotaskExecutedImmediatelyAfterCallback(): void
});
}

public function testMicrotaskExecutionDoesNotKeepReferenceToArgs(): void
{
$this->expectOutputString('123');

$this->loop->queue(function (object $object): void {
print 1;
unset($object);
print 3;
}, new class () {
public function __destruct()
{
print 2;
}
});

$this->loop->run();
}

public function testMicrotaskThrowingStillExecutesNextMicrotask(): void
{
$exception = new \Exception();
Expand Down

0 comments on commit 4e74555

Please sign in to comment.