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

An object reference sticks in the library #394

Closed
bileslav opened this issue Jul 7, 2022 · 1 comment
Closed

An object reference sticks in the library #394

bileslav opened this issue Jul 7, 2022 · 1 comment
Labels

Comments

@bileslav
Copy link

bileslav commented Jul 7, 2022

The code below works as I would expect: it completes. But if I replace the bar with baz on line 42, the program no longer completes, because in this case a reference to the Foo remains somewhere inside the library. This doesn't seem to be the expected behavior.

<?php
declare (strict_types = 1);

use Revolt\EventLoop;

require 'vendor/autoload.php';

final class Foo
{
	private readonly string $id;

	public function __construct()
	{
		/** @var \Closure */
		$f = Amp\weakClosure(function (): void {
			echo "...\n";
		});

		$this->id = EventLoop::repeat(1, $f);
	}

	public function __destruct()
	{
		EventLoop::cancel($this->id);
	}

	public function bar(): string
	{
		return 'OK';
	}

	public function baz(): never
	{
		throw new Exception();
	}
}

(function (): void {
	$foo = new Foo();

	var_dump(Amp\Future\awaitAll([
		Amp\async($foo->bar(...)),
	]));
})();

EventLoop::run();
@bileslav bileslav changed the title An object reference stuck in the library (?) An object reference sticks in the library (?) Jul 7, 2022
@bileslav bileslav changed the title An object reference sticks in the library (?) An object reference sticks in the library Jul 7, 2022
kelunik added a commit that referenced this issue Jul 11, 2022
@kelunik kelunik added v3.x and removed question labels Jul 11, 2022
@kelunik
Copy link
Member

kelunik commented Jul 11, 2022

Thanks for reporting! This is due to exceptions in PHP keeping the arguments of the stack trace by default, which can be disabled using the zend.exception_ignore_args INI option. A circular reference will prevent the reference from immediately being cleaned up, but a gc_collect_cycles() will clean up the reference. We're able to avoid the circular reference in this case, see e56a64d.

@kelunik kelunik closed this as completed Jul 11, 2022
trowski added a commit that referenced this issue Aug 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants