Skip to content

Update close handler to avoid unhandled promise rejections #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
2 changes: 2 additions & 0 deletions src/Io/LazyDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 5 additions & 1 deletion src/Io/ProcessIoDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
20 changes: 13 additions & 7 deletions tests/Io/LazyDatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand All @@ -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');
Expand All @@ -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));

Expand All @@ -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));

Expand Down Expand Up @@ -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();
}

Expand Down