Skip to content

Commit

Permalink
Increase EventDriver::__destruct robustness (#358)
Browse files Browse the repository at this point in the history
Preventing a double free of an event due to destructors freeing the event during freeing in EventDriver destructor.
  • Loading branch information
bzikarsky authored Aug 26, 2021
1 parent 59a8414 commit 8c5a6bc
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions lib/Loop/EventDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,16 +164,17 @@ public static function isSupported(): bool
*/
public function __destruct()
{
foreach ($this->events as $event) {
// Unset here, otherwise $event->del() in the loop may fail with a warning, because __destruct order isn't defined.
// Related https://github.com/amphp/amp/issues/159.
$events = $this->events;
$this->events = [];

foreach ($events as $event) {
if ($event !== null) { // Events may have been nulled in extension depending on destruct order.
$event->free();
}
}

// Unset here, otherwise $event->del() fails with a warning, because __destruct order isn't defined.
// See https://github.com/amphp/amp/issues/159.
$this->events = [];

// Manually free the loop handle to fully release loop resources.
// See https://github.com/amphp/amp/issues/177.
if ($this->handle !== null) {
Expand Down

0 comments on commit 8c5a6bc

Please sign in to comment.