1
0
mirror of https://github.com/danog/MadelineProto.git synced 2024-11-30 09:58:59 +01:00

Do not unreference timeout cancellations

This commit is contained in:
Daniil Gentili 2023-07-25 14:05:51 +02:00
parent 5f7325f71b
commit ebe06ad96a
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
9 changed files with 25 additions and 19 deletions

View File

@ -26,7 +26,6 @@ use Amp\Future;
use Amp\Future\UnhandledFutureError; use Amp\Future\UnhandledFutureError;
use Amp\Ipc\Sync\ChannelledSocket; use Amp\Ipc\Sync\ChannelledSocket;
use Amp\SignalException; use Amp\SignalException;
use Amp\TimeoutCancellation;
use Amp\TimeoutException; use Amp\TimeoutException;
use danog\MadelineProto\ApiWrappers\Start; use danog\MadelineProto\ApiWrappers\Start;
use danog\MadelineProto\Ipc\Client; use danog\MadelineProto\Ipc\Client;
@ -312,7 +311,7 @@ final class API extends AbstractAPI
$this->session, $this->session,
$settings, $settings,
$forceFull $forceFull
)->await(new TimeoutCancellation(30.0)); )->await(Tools::getTimeoutCancellation(30.0));
} catch (CancelledException $e) { } catch (CancelledException $e) {
if (!$e->getPrevious() instanceof TimeoutException) { if (!$e->getPrevious() instanceof TimeoutException) {
throw $e; throw $e;

View File

@ -22,9 +22,9 @@ namespace danog\MadelineProto;
use Amp\Cancellation; use Amp\Cancellation;
use Amp\CancelledException; use Amp\CancelledException;
use Amp\DeferredCancellation;
use Amp\DeferredFuture; use Amp\DeferredFuture;
use Amp\Future; use Amp\Future;
use Amp\TimeoutCancellation;
use Amp\TimeoutException; use Amp\TimeoutException;
use Closure; use Closure;
use Generator; use Generator;
@ -120,7 +120,7 @@ abstract class AsyncTools extends StrTools
*/ */
public static function timeout(Generator|Future $promise, int $timeout): mixed public static function timeout(Generator|Future $promise, int $timeout): mixed
{ {
return self::call($promise)->await(new TimeoutCancellation($timeout/1000)); return self::call($promise)->await(Tools::getTimeoutCancellation($timeout/1000));
} }
/** /**
* Creates an artificial timeout for any `Promise`. * Creates an artificial timeout for any `Promise`.
@ -305,6 +305,17 @@ abstract class AsyncTools extends StrTools
{ {
delay($time); delay($time);
} }
/**
* @internal
*/
public static function getTimeoutCancellation(float $timeout, string $message = "Operation timed out"): Cancellation
{
$deferred = new DeferredCancellation;
EventLoop::delay($timeout, fn () => $deferred->cancel(new TimeoutException($message)));
return $deferred->getCancellation();
}
/** /**
* Asynchronously read line. * Asynchronously read line.
* *

View File

@ -32,7 +32,6 @@ use Amp\Socket\ResourceSocket;
use Amp\Socket\Socket; use Amp\Socket\Socket;
use Amp\Socket\SocketAddress; use Amp\Socket\SocketAddress;
use Amp\Socket\SocketConnector; use Amp\Socket\SocketConnector;
use Amp\TimeoutCancellation;
use AssertionError; use AssertionError;
use danog\MadelineProto\Stream\ConnectionContext; use danog\MadelineProto\Stream\ConnectionContext;
use Revolt\EventLoop; use Revolt\EventLoop;
@ -119,7 +118,7 @@ final class DoHConnector implements SocketConnector
$watcher = EventLoop::onWritable($socket, $deferred->complete(...)); $watcher = EventLoop::onWritable($socket, $deferred->complete(...));
$id = $token->subscribe($deferred->error(...)); $id = $token->subscribe($deferred->error(...));
try { try {
$deferred->getFuture()->await(new TimeoutCancellation($timeout)); $deferred->getFuture()->await(Tools::getTimeoutCancellation($timeout));
} catch (CancelledException $e) { } catch (CancelledException $e) {
if (!$e->getPrevious() instanceof DnsTimeoutException) { if (!$e->getPrevious() instanceof DnsTimeoutException) {
throw $e; throw $e;

View File

@ -43,7 +43,6 @@ use danog\MadelineProto\LocalFile;
use danog\MadelineProto\ParseMode; use danog\MadelineProto\ParseMode;
use danog\MadelineProto\RemoteUrl; use danog\MadelineProto\RemoteUrl;
use danog\MadelineProto\Settings; use danog\MadelineProto\Settings;
use Webmozart\Assert\Assert;
/** /**
* Manages upload and download of files. * Manages upload and download of files.
@ -89,7 +88,7 @@ trait FilesAbstraction
return new VideoSticker($this, $media, $attr, $has_video, $protected); return new VideoSticker($this, $media, $attr, $has_video, $protected);
} }
assert($has_document_photo !== null); \assert($has_document_photo !== null);
if ($attr['mask']) { if ($attr['mask']) {
return new MaskSticker($this, $media, $attr, $has_document_photo, $protected); return new MaskSticker($this, $media, $attr, $has_document_photo, $protected);
} }
@ -114,7 +113,7 @@ trait FilesAbstraction
} }
} }
if ($has_animated) { if ($has_animated) {
assert($has_video !== null); \assert($has_video !== null);
return new Gif($this, $media, $has_video, $protected); return new Gif($this, $media, $has_video, $protected);
} }
if ($has_video) { if ($has_video) {

View File

@ -24,7 +24,6 @@ use Amp\CancelledException;
use Amp\DeferredFuture; use Amp\DeferredFuture;
use Amp\Http\Client\Request; use Amp\Http\Client\Request;
use Amp\Http\Client\Response; use Amp\Http\Client\Response;
use Amp\TimeoutCancellation;
use Amp\TimeoutException; use Amp\TimeoutException;
use danog\MadelineProto\API; use danog\MadelineProto\API;
use danog\MadelineProto\EventHandler\AbstractMessage; use danog\MadelineProto\EventHandler\AbstractMessage;
@ -52,6 +51,7 @@ use danog\MadelineProto\RPCErrorException;
use danog\MadelineProto\Settings; use danog\MadelineProto\Settings;
use danog\MadelineProto\TL\TL; use danog\MadelineProto\TL\TL;
use danog\MadelineProto\TL\Types\Button; use danog\MadelineProto\TL\Types\Button;
use danog\MadelineProto\Tools;
use danog\MadelineProto\UpdateHandlerType; use danog\MadelineProto\UpdateHandlerType;
use danog\MadelineProto\VoIP; use danog\MadelineProto\VoIP;
use Revolt\EventLoop; use Revolt\EventLoop;
@ -227,7 +227,7 @@ trait UpdateHandler
try { try {
$this->update_deferred = new DeferredFuture(); $this->update_deferred = new DeferredFuture();
$this->update_deferred->getFuture()->await( $this->update_deferred->getFuture()->await(
$timeout === INF ? null : new TimeoutCancellation($timeout) $timeout === INF ? null : Tools::getTimeoutCancellation($timeout)
); );
} catch (CancelledException $e) { } catch (CancelledException $e) {
if (!$e->getPrevious() instanceof TimeoutException) { if (!$e->getPrevious() instanceof TimeoutException) {

View File

@ -25,7 +25,6 @@ use Amp\DeferredCancellation;
use Amp\DeferredFuture; use Amp\DeferredFuture;
use Amp\Future; use Amp\Future;
use Amp\Ipc\Sync\ChannelledSocket; use Amp\Ipc\Sync\ChannelledSocket;
use Amp\TimeoutCancellation;
use Amp\TimeoutException; use Amp\TimeoutException;
use danog\MadelineProto\Db\DbPropertiesFactory; use danog\MadelineProto\Db\DbPropertiesFactory;
use danog\MadelineProto\Db\DriverArray; use danog\MadelineProto\Db\DriverArray;
@ -268,7 +267,7 @@ abstract class Serialization
} }
} }
try { try {
if ($res = $cancelConnect->await(new TimeoutCancellation(1.0))) { if ($res = $cancelConnect->await(Tools::getTimeoutCancellation(1.0))) {
if ($res instanceof Throwable) { if ($res instanceof Throwable) {
return [$res, null]; return [$res, null];
} }

View File

@ -24,7 +24,6 @@ use Amp\Cancellation;
use Amp\CancelledException; use Amp\CancelledException;
use Amp\CompositeCancellation; use Amp\CompositeCancellation;
use Amp\DeferredFuture; use Amp\DeferredFuture;
use Amp\TimeoutCancellation;
use AssertionError; use AssertionError;
use BaconQrCode\Renderer\Image\SvgImageBackEnd; use BaconQrCode\Renderer\Image\SvgImageBackEnd;
use BaconQrCode\Renderer\ImageRenderer; use BaconQrCode\Renderer\ImageRenderer;
@ -33,6 +32,7 @@ use BaconQrCode\Renderer\RendererStyle\RendererStyle;
use BaconQrCode\Writer; use BaconQrCode\Writer;
use danog\MadelineProto\Ipc\Client; use danog\MadelineProto\Ipc\Client;
use danog\MadelineProto\MTProto; use danog\MadelineProto\MTProto;
use danog\MadelineProto\Tools;
use JsonSerializable; use JsonSerializable;
/** /**
@ -90,7 +90,7 @@ final class LoginQrCode implements JsonSerializable
public function getExpirationCancellation(): Cancellation public function getExpirationCancellation(): Cancellation
{ {
return new TimeoutCancellation((float) $this->expiresIn(), "The QR code expired!"); return Tools::getTimeoutCancellation((float) $this->expiresIn(), "The QR code expired!");
} }
public function getLoginCancellation(): Cancellation public function getLoginCancellation(): Cancellation

View File

@ -21,10 +21,10 @@ declare(strict_types=1);
namespace danog\MadelineProto\Wrappers; namespace danog\MadelineProto\Wrappers;
use Amp\Sync\LocalMutex; use Amp\Sync\LocalMutex;
use Amp\TimeoutCancellation;
use danog\MadelineProto\API; use danog\MadelineProto\API;
use danog\MadelineProto\Exception; use danog\MadelineProto\Exception;
use danog\MadelineProto\Settings; use danog\MadelineProto\Settings;
use danog\MadelineProto\Tools;
use Throwable; use Throwable;
use Webmozart\Assert\Assert; use Webmozart\Assert\Assert;
@ -105,7 +105,7 @@ trait DialogHandler
$result = $this->methodCallAsyncRead( $result = $this->methodCallAsyncRead(
'updates.getDifference', 'updates.getDifference',
$state, $state,
['cancellation' => new TimeoutCancellation(15.0), 'FloodWaitLimit' => 86400] ['cancellation' => Tools::getTimeoutCancellation(15.0), 'FloodWaitLimit' => 86400]
)['_']; )['_'];
} catch (Throwable $e) { } catch (Throwable $e) {
$this->logger->logger("Got {$e->getMessage()} while getting difference, trying another PTS..."); $this->logger->logger("Got {$e->getMessage()} while getting difference, trying another PTS...");

View File

@ -22,7 +22,6 @@ namespace danog\MadelineProto\Wrappers;
use Amp\CancelledException; use Amp\CancelledException;
use Amp\CompositeCancellation; use Amp\CompositeCancellation;
use Amp\TimeoutCancellation;
use danog\MadelineProto\API; use danog\MadelineProto\API;
use danog\MadelineProto\Exception; use danog\MadelineProto\Exception;
use danog\MadelineProto\Ipc\Client; use danog\MadelineProto\Ipc\Client;
@ -222,7 +221,7 @@ trait Start
/** @var ?LoginQrCode */ /** @var ?LoginQrCode */
$qr = $this->qrLogin(); $qr = $this->qrLogin();
if (isset($_GET['waitQrCodeOrLogin'])) { if (isset($_GET['waitQrCodeOrLogin'])) {
$qr = $qr?->waitForLoginOrQrCodeExpiration(new TimeoutCancellation(5.0)); $qr = $qr?->waitForLoginOrQrCodeExpiration(Tools::getTimeoutCancellation(5.0));
} }
} catch (CancelledException) { } catch (CancelledException) {
/** @var ?LoginQrCode */ /** @var ?LoginQrCode */