Skip to content

Commit

Permalink
Make sure there are two copies of the callable around because of http…
Browse files Browse the repository at this point in the history
  • Loading branch information
WyriHaximus committed Oct 20, 2017
1 parent ab6c584 commit 3f5457f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 28 deletions.
17 changes: 10 additions & 7 deletions src/ExtEventLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,21 @@ public function __construct(EventBaseConfig $config = null)

$this->signals = new SignalsHandler(
function ($signal) {
$this->signalEvents[$signal] = Event::signal($this->eventBase, $signal, function () use ($signal) {
$this->signalEvents[$signal] = Event::signal($this->eventBase, $signal, $f = function () use ($signal, &$f) {
$this->signals->call($signal);
// Ensure there are two copies of the callable around until it has been executed.
// For more information see: https://bugs.php.net/bug.php?id=62452
// Only an issue for PHP 5, this hack can be removed once PHP 5 suppose has been dropped.
$g = $f;
$f = $g;
});
$this->signalEvents[$signal]->add();
},
function ($signal) {
$this->futureTick(function () use ($signal) {
if ($this->signals->count($signal) === 0) {
$this->signalEvents[$signal]->del();
unset($this->signalEvents[$signal]);
}
});
if ($this->signals->count($signal) === 0) {
$this->signalEvents[$signal]->del();
unset($this->signalEvents[$signal]);
}
}
);

Expand Down
17 changes: 10 additions & 7 deletions src/LibEvLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,21 @@ public function __construct()

$this->signals = new SignalsHandler(
function ($signal) {
$this->signalEvents[$signal] = new SignalEvent(function () use ($signal) {
$this->signalEvents[$signal] = new SignalEvent($f = function () use ($signal, &$f) {
$this->signals->call($signal);
// Ensure there are two copies of the callable around until it has been executed.
// For more information see: https://bugs.php.net/bug.php?id=62452
// Only an issue for PHP 5, this hack can be removed once PHP 5 suppose has been dropped.
$g = $f;
$f = $g;
}, $signal);
$this->loop->add($this->signalEvents[$signal]);
},
function ($signal) {
$this->futureTick(function () use ($signal) {
if ($this->signals->count($signal) === 0) {
$this->loop->remove($this->signalEvents[$signal]);
unset($this->signalEvents[$signal]);
}
});
if ($this->signals->count($signal) === 0) {
$this->loop->remove($this->signalEvents[$signal]);
unset($this->signalEvents[$signal]);
}
}
);
}
Expand Down
19 changes: 11 additions & 8 deletions src/LibEventLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,23 @@ public function __construct()
$this->signals = new SignalsHandler(
function ($signal) {
$this->signalEvents[$signal] = event_new();
event_set($this->signalEvents[$signal], $signal, EV_PERSIST | EV_SIGNAL, function () use ($signal) {
event_set($this->signalEvents[$signal], $signal, EV_PERSIST | EV_SIGNAL, $f = function () use ($signal, &$f) {
$this->signals->call($signal);
// Ensure there are two copies of the callable around until it has been executed.
// For more information see: https://bugs.php.net/bug.php?id=62452
// Only an issue for PHP 5, this hack can be removed once PHP 5 suppose has been dropped.
$g = $f;
$f = $g;
});
event_base_set($this->signalEvents[$signal], $this->eventBase);
event_add($this->signalEvents[$signal]);
},
function ($signal) {
$this->futureTick(function () use ($signal) {
if ($this->signals->count($signal) === 0) {
event_del($this->signalEvents[$signal]);
event_free($this->signalEvents[$signal]);
unset($this->signalEvents[$signal]);
}
});
if ($this->signals->count($signal) === 0) {
event_del($this->signalEvents[$signal]);
event_free($this->signalEvents[$signal]);
unset($this->signalEvents[$signal]);
}
}
);

Expand Down
15 changes: 9 additions & 6 deletions src/StreamSelectLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,19 @@ public function __construct()
$this->pcntl = extension_loaded('pcntl');
$this->signals = new SignalsHandler(
function ($signal) {
\pcntl_signal($signal, function ($signal) {
\pcntl_signal($signal, $f = function ($signal) use (&$f) {
$this->signals->call($signal);
// Ensure there are two copies of the callable around until it has been executed.
// For more information see: https://bugs.php.net/bug.php?id=62452
// Only an issue for PHP 5, this hack can be removed once PHP 5 suppose has been dropped.
$g = $f;
$f = $g;
});
},
function ($signal) {
$this->futureTick(function () use ($signal) {
if ($this->signals->count($signal) === 0) {
\pcntl_signal($signal, SIG_DFL);
}
});
if ($this->signals->count($signal) === 0) {
\pcntl_signal($signal, SIG_DFL);
}
}
);
}
Expand Down

0 comments on commit 3f5457f

Please sign in to comment.