diff --git a/Coroutine/Kernel.php b/Coroutine/Kernel.php index 9c981b8..77eefa7 100644 --- a/Coroutine/Kernel.php +++ b/Coroutine/Kernel.php @@ -437,6 +437,7 @@ function (TaskInterface $task, CoroutineInterface $coroutine) use ($taskId) { $completeList = $coroutine->completedList(); $countComplete = \count($completeList); $gatherCompleteCount = 0; + $isResultsException = false; // Check and handle tasks already completed before entering/executing gather(). if ($countComplete > 0) { @@ -448,7 +449,14 @@ function (TaskInterface $task, CoroutineInterface $coroutine) use ($taskId) { $result = $tasks->result(); } - $results[$id] = $result; + if ($result instanceof \Throwable) { + // @codeCoverageIgnoreStart + $isResultsException = $result; + // @codeCoverageIgnoreEnd + } else { + $results[$id] = $result; + } + $count--; $gatherCompleteCount++; unset($taskIdList[$id]); @@ -604,9 +612,16 @@ function (TaskInterface $task, CoroutineInterface $coroutine) use ($taskId) { } } - self::$gatherResumer = null; - $task->sendValue($results); - $coroutine->schedule($task); + if ($gatherShouldError && $isResultsException !== false) { + // @codeCoverageIgnoreStart + $task->setException($isResultsException); + $coroutine->schedule($tasks); + // @codeCoverageIgnoreEnd + } else { + self::$gatherResumer = null; + $task->sendValue($results); + $coroutine->schedule($task); + } } ); }