Skip to content

Commit 4e74555

Browse files
authored
Allow GC of arguments during microtask execution (#60)
1 parent cce7416 commit 4e74555

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/EventLoop/Internal/AbstractDriver.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,8 @@ private function invokeMicrotasks(): void
418418
[$callback, $args] = $this->microtaskQueue->dequeue();
419419

420420
try {
421-
$callback(...$args);
421+
// Clear $args to allow garbage collection
422+
$callback(...$args, ...($args = []));
422423
} catch (\Throwable $exception) {
423424
$this->error($callback, $exception);
424425
} finally {

test/Driver/DriverTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,6 +1387,24 @@ public function testMicrotaskExecutedImmediatelyAfterCallback(): void
13871387
});
13881388
}
13891389

1390+
public function testMicrotaskExecutionDoesNotKeepReferenceToArgs(): void
1391+
{
1392+
$this->expectOutputString('123');
1393+
1394+
$this->loop->queue(function (object $object): void {
1395+
print 1;
1396+
unset($object);
1397+
print 3;
1398+
}, new class () {
1399+
public function __destruct()
1400+
{
1401+
print 2;
1402+
}
1403+
});
1404+
1405+
$this->loop->run();
1406+
}
1407+
13901408
public function testMicrotaskThrowingStillExecutesNextMicrotask(): void
13911409
{
13921410
$exception = new \Exception();

0 commit comments

Comments
 (0)