diff --git a/lib/Loop/NativeDriver.php b/lib/Loop/NativeDriver.php index 4468b43..a31d120 100644 --- a/lib/Loop/NativeDriver.php +++ b/lib/Loop/NativeDriver.php @@ -2,6 +2,7 @@ namespace Amp\Loop; +use Amp\CallableMaker; use Amp\Coroutine; use Amp\Promise; use React\Promise\PromiseInterface as ReactPromise; @@ -10,6 +11,8 @@ use function Amp\Promise\rethrow; class NativeDriver extends Driver { + use CallableMaker; + /** @var resource[] */ private $readStreams = []; @@ -297,7 +300,7 @@ class NativeDriver extends Driver case Watcher::SIGNAL: if (!isset($this->signalWatchers[$watcher->value])) { - if (!@\pcntl_signal($watcher->value, [$this, 'handleSignal'])) { + if (!@\pcntl_signal($watcher->value, $this->callableFromInstanceMethod('handleSignal'))) { $message = "Failed to register signal handler"; if ($error = \error_get_last()) { $message .= \sprintf("; Errno: %d; %s", $error["type"], $error["message"]); diff --git a/test/Loop/NativeDriverTest.php b/test/Loop/NativeDriverTest.php index 3955af5..04cf4cf 100644 --- a/test/Loop/NativeDriverTest.php +++ b/test/Loop/NativeDriverTest.php @@ -2,6 +2,7 @@ namespace Amp\Test\Loop; +use Amp\Loop\Driver; use Amp\Loop\NativeDriver; class NativeDriverTest extends DriverTest @@ -17,4 +18,28 @@ class NativeDriverTest extends DriverTest { $this->assertNull($this->loop->getHandle()); } + + /** + * @requires PHP 7.1 + */ + public function testAsyncSignals() + { + \pcntl_async_signals(true); + + try { + $this->start(function (Driver $loop) use (&$invoked) { + $watcher = $loop->onSignal(SIGUSR1, function () use (&$invoked) { + $invoked = true; + }); + $loop->unreference($watcher); + $loop->defer(function () { + \posix_kill(\getmypid(), \SIGUSR1); + }); + }); + } finally { + \pcntl_async_signals(false); + } + + $this->assertTrue($invoked); + } }