Skip to content

Conversation

@nielsdos
Copy link
Member

@nielsdos nielsdos commented Apr 2, 2025

The ctrl_handler is never destroyed. We have to destroy it at request end so we avoid leaking it and also avoid keeping a reference to previous request memory in a next request. The latter can result in a crash and can be demonstrated with this script and --repeat 2:

class Test {
	public function set() {
		sapi_windows_set_ctrl_handler(self::cb(...));
	}
	public function cb() {
	}
}

$test = new Test;
$test->set();
sleep(3);

When you hit CTRL+C in the second request you can crash.

This patch resolves both the leak and crash by destroying the ctrl_handler after a request.

The ctrl_handler is never destroyed. We have to destroy it at request
end so we avoid leaking it and also avoid keeping a reference to
previous request memory in a next request. The latter can result in a
crash and can be demonstrated with this script and `--repeat 2`:

```php
class Test {
	public function set() {
		sapi_windows_set_ctrl_handler(self::cb(...));
	}
	public function cb() {
	}
}

$test = new Test;
$test->set();
sleep(3);
```
When you hit CTRL+C in the second request you can crash.

This patch resolves both the leak and crash by destroying the
ctrl_handler after a request.
}

zval_ptr_dtor_nogc(&ctrl_handler);
zval_ptr_dtor(&ctrl_handler);
Copy link
Member Author

Choose a reason for hiding this comment

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

This change is also a fix, this is needed because the handler closure object can be cyclic.

Copy link
Member

@arnaud-lb arnaud-lb left a comment

Choose a reason for hiding this comment

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

Looks good to me!

@nielsdos nielsdos closed this in fb3536f May 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants