Skip to content

Commit

Permalink
Revert ability to reference TimeoutCancellation and SignalCancellation
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Jan 26, 2025
1 parent 3774a59 commit 2cfca75
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 134 deletions.
58 changes: 3 additions & 55 deletions src/SignalCancellation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
}
52 changes: 3 additions & 49 deletions src/TimeoutCancellation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -41,9 +37,7 @@ public function __construct(
$source->cancel(new TimeoutException($message));
});

if (!$reference) {
EventLoop::unreference($this->callbackId);
}
EventLoop::unreference($this->callbackId);
}

/**
Expand Down Expand Up @@ -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;
}
}
17 changes: 0 additions & 17 deletions test/Cancellation/SignalCancellationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Amp\Cancellation;

use Amp\CancelledException;
use Amp\DeferredFuture;
use Amp\SignalCancellation;
use Amp\SignalException;
use Amp\TestCase;
Expand Down Expand Up @@ -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);
}
}
13 changes: 0 additions & 13 deletions test/Cancellation/TimeoutCancellationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Amp\Cancellation;

use Amp\CancelledException;
use Amp\DeferredFuture;
use Amp\TestCase;
use Amp\TimeoutCancellation;
use Amp\TimeoutException;
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 2cfca75

Please sign in to comment.