From c15c7000c7c6978d766f926f73c714ac370f8311 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Wed, 1 Jan 2025 19:02:21 +0100 Subject: [PATCH] Update close handler to avoid unhandled promise rejections --- .github/workflows/ci.yml | 2 ++ src/Io/LazyDatabase.php | 2 ++ src/Io/ProcessIoDatabase.php | 6 +++++- tests/Io/LazyDatabaseTest.php | 20 +++++++++++++------- 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 38df9d5..799bce8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -36,6 +36,8 @@ jobs: extensions: sqlite3 coverage: xdebug ini-file: development + # disable JIT on PHP 8.2 with Xdebug: JIT is incompatible with third party extensions that override zend_execute_ex(). JIT disabled. + ini-values: ${{ matrix.php == 8.2 && 'opcache.jit_buffer_size=0' || '' }} - run: composer install - run: vendor/bin/phpunit --coverage-text --coverage-clover=clover.xml if: ${{ matrix.php >= 7.3 }} diff --git a/src/Io/LazyDatabase.php b/src/Io/LazyDatabase.php index 23c377b..fd4ad8c 100644 --- a/src/Io/LazyDatabase.php +++ b/src/Io/LazyDatabase.php @@ -145,6 +145,8 @@ public function close() if ($this->promise !== null) { $this->promise->then(function (DatabaseInterface $db) { $db->close(); + }, function () { + // ignore to avoid reporting unhandled rejection }); if ($this->promise !== null) { $this->promise->cancel(); diff --git a/src/Io/ProcessIoDatabase.php b/src/Io/ProcessIoDatabase.php index a2797af..7c7f807 100644 --- a/src/Io/ProcessIoDatabase.php +++ b/src/Io/ProcessIoDatabase.php @@ -115,7 +115,11 @@ public function quit() $promise = $this->send('close', array()); if ($this->process->stdin === $this->process->stdout) { - $promise->then(function () { $this->process->stdin->close(); }); + $promise->then(function () { + $this->process->stdin->close(); + }, function () { + // ignore to avoid reporting unhandled rejection + }); } else { $this->process->stdin->end(); } diff --git a/tests/Io/LazyDatabaseTest.php b/tests/Io/LazyDatabaseTest.php index 4e69a88..212a0d6 100644 --- a/tests/Io/LazyDatabaseTest.php +++ b/tests/Io/LazyDatabaseTest.php @@ -167,7 +167,9 @@ public function testExecAfterPreviousFactoryRejectsUnderlyingDatabaseWillCreateN new Promise(function () { }) ); - $this->db->exec('CREATE'); + $promise = $this->db->exec('CREATE'); + $promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection + $deferred->reject($error); $this->db->exec('CREATE'); @@ -392,9 +394,11 @@ public function testQueryAfterPreviousFactoryRejectsUnderlyingDatabaseWillCreate $this->factory->expects($this->exactly(2))->method('open')->willReturnOnConsecutiveCalls( $deferred->promise(), new Promise(function () { }) - ); + ); + + $promise = $this->db->query('CREATE'); + $promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection - $this->db->query('CREATE'); $deferred->reject($error); $this->db->query('CREATE'); @@ -408,7 +412,7 @@ public function testQueryAfterPreviousUnderlyingDatabaseAlreadyClosedWillCreateN $this->factory->expects($this->exactly(2))->method('open')->willReturnOnConsecutiveCalls( \React\Promise\resolve($client), new Promise(function () { }) - ); + ); $this->db->query('CREATE'); $client->emit('close'); @@ -433,7 +437,7 @@ public function testQueryAfterQueryWillNotStartIdleTimerWhenFirstQueryResolves() $client->expects($this->exactly(2))->method('query')->willReturnOnConsecutiveCalls( $deferred->promise(), new Promise(function () { }) - ); + ); $this->factory->expects($this->once())->method('open')->willReturn(\React\Promise\resolve($client)); @@ -451,7 +455,7 @@ public function testQueryAfterQueryWillStartAndCancelIdleTimerWhenSecondQuerySta $client->expects($this->exactly(2))->method('query')->willReturnOnConsecutiveCalls( $deferred->promise(), new Promise(function () { }) - ); + ); $this->factory->expects($this->once())->method('open')->willReturn(\React\Promise\resolve($client)); @@ -524,7 +528,9 @@ public function testCloseAfterExecWillEmitCloseWithoutErrorWhenUnderlyingDatabas $this->db->on('error', $this->expectCallableNever()); $this->db->on('close', $this->expectCallableOnce()); - $this->db->exec('CREATE'); + $promise = $this->db->exec('CREATE'); + $promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection + $this->db->close(); }