From 4b4a3937eaa31c42dd905829fe1b04f4c94ab334 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Thu, 12 Nov 2020 15:14:32 +0100 Subject: [PATCH] Make sure Loop abortJobs does not lose track of promises in case wait() is called within the scope of a wait(), refs #9463 --- src/Composer/Util/Loop.php | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/Composer/Util/Loop.php b/src/Composer/Util/Loop.php index bed9151a9..3f85434de 100644 --- a/src/Composer/Util/Loop.php +++ b/src/Composer/Util/Loop.php @@ -25,8 +25,10 @@ class Loop private $httpDownloader; /** @var ProcessExecutor|null */ private $processExecutor; - /** @var Promise[]|null */ - private $currentPromises; + /** @var Promise[][] */ + private $currentPromises = array(); + /** @var int */ + private $waitIndex = 0; public function __construct(HttpDownloader $httpDownloader, ProcessExecutor $processExecutor = null) { @@ -67,7 +69,10 @@ class Loop } ); - $this->currentPromises = $promises; + // keep track of every group of promises that is waited on, so abortJobs can + // cancel them all, even if wait() was called within a wait() + $waitIndex = $this->waitIndex++; + $this->currentPromises[$waitIndex] = $promises; if ($progress) { $totalJobs = 0; @@ -101,7 +106,7 @@ class Loop usleep(5000); } - $this->currentPromises = null; + unset($this->currentPromises[$waitIndex]); if ($uncaught) { throw $uncaught; } @@ -109,8 +114,8 @@ class Loop public function abortJobs() { - if ($this->currentPromises) { - foreach ($this->currentPromises as $promise) { + foreach ($this->currentPromises as $promiseGroup) { + foreach ($promiseGroup as $promise) { $promise->cancel(); } }