Skip to content
Merged
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
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 = []));
Copy link
Contributor

Choose a reason for hiding this comment

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

So how does this work? You first pass all the elements of the $args array into the argument list for $callback, and then you overwrite $args with an empty array and also "push" that to the same argument list?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it overwrites the $args array (and pushes zero arguments to the argument list) after pushing the original arguments and before invoking the callback.

Copy link
Contributor

Choose a reason for hiding this comment

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

Near trick, it's a tad confusing when you first read the line tbh.

Choose a reason for hiding this comment

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

A better documentation what happens here would be good especially for people like me that doesn't see what's happening at the first glance and why it's needed.

} 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