From d35ac1bb1c024d3c5bc6ccf63ff08f68e7f9b28a Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Fri, 7 Jun 2019 18:12:30 +0200 Subject: [PATCH] Automatically enable async signal handling when possible --- lib/Loop/NativeDriver.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/Loop/NativeDriver.php b/lib/Loop/NativeDriver.php index a31d120..df3bebd 100644 --- a/lib/Loop/NativeDriver.php +++ b/lib/Loop/NativeDriver.php @@ -43,10 +43,14 @@ class NativeDriver extends Driver /** @var bool */ private $signalHandling; + /** @var bool */ + private $manualSignalHandling; + public function __construct() { $this->timerQueue = new Internal\TimerQueue; $this->signalHandling = \extension_loaded("pcntl"); + $this->manualSignalHandling = !(\PHP_MAJOR_VERSION >= 7 && \PHP_MINOR_VERSION >= 1); $this->nowOffset = getCurrentTime(); $this->now = \random_int(0, $this->nowOffset); $this->nowOffset -= $this->now; @@ -63,6 +67,10 @@ class NativeDriver extends Driver throw new UnsupportedFeatureException("Signal handling requires the pcntl extension"); } + if (!$this->manualSignalHandling && !pcntl_async_signals()) { + pcntl_async_signals(true); + } + return parent::onSignal($signo, $callback, $data); } @@ -143,7 +151,7 @@ class NativeDriver extends Driver } } - if ($this->signalHandling) { + if ($this->signalHandling && $this->manualSignalHandling) { \pcntl_signal_dispatch(); } }