From 2cfca7537e4fcd822ca9a1e71c60d9b553f7e71f Mon Sep 17 00:00:00 2001 From: Aaron Piotrowski Date: Sun, 26 Jan 2025 08:43:56 -0600 Subject: [PATCH] Revert ability to reference TimeoutCancellation and SignalCancellation --- src/SignalCancellation.php | 58 +------------------ src/TimeoutCancellation.php | 52 +---------------- test/Cancellation/SignalCancellationTest.php | 17 ------ test/Cancellation/TimeoutCancellationTest.php | 13 ----- 4 files changed, 6 insertions(+), 134 deletions(-) diff --git a/src/SignalCancellation.php b/src/SignalCancellation.php index d2058950..f345bbe6 100644 --- a/src/SignalCancellation.php +++ b/src/SignalCancellation.php @@ -20,13 +20,9 @@ final class SignalCancellation implements Cancellation /** * @param int|int[] $signals Signal number or array of signal numbers. * @param string $message Message for SignalException. Default is "Operation cancelled by signal". - * @param bool $reference If false, unreference the underlying event-loop callback. */ - public function __construct( - int|array $signals, - string $message = "Operation cancelled by signal", - private bool $reference = false, - ) { + public function __construct(int|array $signals, string $message = "Operation cancelled by signal") + { if (\is_int($signals)) { $signals = [$signals]; } @@ -53,11 +49,7 @@ public function __construct( }; foreach ($signals as $signal) { - $callbackIds[] = $callbackId = EventLoop::onSignal($signal, $callback); - - if (!$reference) { - EventLoop::unreference($callbackId); - } + $callbackIds[] = EventLoop::unreference(EventLoop::onSignal($signal, $callback)); } $this->callbackIds = $callbackIds; @@ -92,48 +84,4 @@ public function throwIfRequested(): void { $this->cancellation->throwIfRequested(); } - - /** - * @return bool True if the internal event-loop callback is referenced, false if not or if the cancellation has - * occurred. - */ - public function isReferenced(): bool - { - return $this->reference && !$this->cancellation->isRequested(); - } - - /** - * References the internal event-loop callback, keeping the loop running while the timeout is applicable. - * If the timeout has expired (cancellation has been requested), this method is a no-op. - * - * @return $this - */ - public function reference(): self - { - if (!$this->cancellation->isRequested()) { - foreach ($this->callbackIds as $callbackId) { - EventLoop::reference($callbackId); - } - } - - $this->reference = true; - - return $this; - } - - /** - * Unreferences the internal event-loop callback, allowing the loop to stop while the repeat loop is enabled. - * - * @return $this - */ - public function unreference(): self - { - foreach ($this->callbackIds as $callbackId) { - EventLoop::unreference($callbackId); - } - - $this->reference = false; - - return $this; - } } diff --git a/src/TimeoutCancellation.php b/src/TimeoutCancellation.php index fb23347a..a80668e1 100644 --- a/src/TimeoutCancellation.php +++ b/src/TimeoutCancellation.php @@ -19,13 +19,9 @@ final class TimeoutCancellation implements Cancellation /** * @param float $timeout Seconds until cancellation is requested. * @param string $message Message for TimeoutException. Default is "Operation timed out". - * @param bool $reference If false, unreference the underlying event-loop callback. */ - public function __construct( - float $timeout, - string $message = "Operation timed out", - bool $reference = false, - ) { + public function __construct(float $timeout, string $message = "Operation timed out") + { $this->cancellation = $source = new Internal\Cancellable; $trace = null; // Defined in case assertions are disabled. @@ -41,9 +37,7 @@ public function __construct( $source->cancel(new TimeoutException($message)); }); - if (!$reference) { - EventLoop::unreference($this->callbackId); - } + EventLoop::unreference($this->callbackId); } /** @@ -73,44 +67,4 @@ public function throwIfRequested(): void { $this->cancellation->throwIfRequested(); } - - /** - * @return bool True if the internal event-loop callback is referenced, false if not or if the cancellation has - * occurred. - */ - public function isReferenced(): bool - { - if ($this->cancellation->isRequested()) { - return false; - } - - return EventLoop::isReferenced($this->callbackId); - } - - /** - * References the internal event-loop callback, keeping the loop running while the timeout is applicable. - * If the timeout has expired (cancellation has been requested), this method is a no-op. - * - * @return $this - */ - public function reference(): self - { - if (!$this->cancellation->isRequested()) { - EventLoop::reference($this->callbackId); - } - - return $this; - } - - /** - * Unreferences the internal event-loop callback, allowing the loop to stop while the repeat loop is enabled. - * - * @return $this - */ - public function unreference(): self - { - EventLoop::unreference($this->callbackId); - - return $this; - } } diff --git a/test/Cancellation/SignalCancellationTest.php b/test/Cancellation/SignalCancellationTest.php index 352712c9..df057e4c 100644 --- a/test/Cancellation/SignalCancellationTest.php +++ b/test/Cancellation/SignalCancellationTest.php @@ -3,7 +3,6 @@ namespace Amp\Cancellation; use Amp\CancelledException; -use Amp\DeferredFuture; use Amp\SignalCancellation; use Amp\SignalException; use Amp\TestCase; @@ -53,20 +52,4 @@ public function testWatcherCancellation(): void unset($cancellation); self::assertSame($identifiers, EventLoop::getIdentifiers()); } - - public function testWatcherUnreference(): void - { - $this->expectException(CancelledException::class); - - $cancellation = new SignalCancellation(\SIGUSR1, reference: true); - - self::assertTrue($cancellation->isReferenced()); - - EventLoop::defer(function (): void { - \posix_kill(\getmypid(), \SIGUSR1); - }); - - $deferred = new DeferredFuture(); - $deferred->getFuture()->await($cancellation); - } } diff --git a/test/Cancellation/TimeoutCancellationTest.php b/test/Cancellation/TimeoutCancellationTest.php index f261c465..3b8e229f 100644 --- a/test/Cancellation/TimeoutCancellationTest.php +++ b/test/Cancellation/TimeoutCancellationTest.php @@ -3,7 +3,6 @@ namespace Amp\Cancellation; use Amp\CancelledException; -use Amp\DeferredFuture; use Amp\TestCase; use Amp\TimeoutCancellation; use Amp\TimeoutException; @@ -43,16 +42,4 @@ public function testWatcherCancellation(): void unset($cancellation); self::assertSame($identifiers, EventLoop::getIdentifiers()); } - - public function testWatcherUnreference(): void - { - $this->expectException(CancelledException::class); - - $cancellation = new TimeoutCancellation(0.001, reference: true); - - self::assertTrue($cancellation->isReferenced()); - - $deferred = new DeferredFuture(); - $deferred->getFuture()->await($cancellation); - } }