Skip to content

Commit c15c700

Browse files
committed
Update close handler to avoid unhandled promise rejections
1 parent 42b3c0a commit c15c700

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed

.github/workflows/ci.yml

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ jobs:
3636
extensions: sqlite3
3737
coverage: xdebug
3838
ini-file: development
39+
# disable JIT on PHP 8.2 with Xdebug: JIT is incompatible with third party extensions that override zend_execute_ex(). JIT disabled.
40+
ini-values: ${{ matrix.php == 8.2 && 'opcache.jit_buffer_size=0' || '' }}
3941
- run: composer install
4042
- run: vendor/bin/phpunit --coverage-text --coverage-clover=clover.xml
4143
if: ${{ matrix.php >= 7.3 }}

src/Io/LazyDatabase.php

+2
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ public function close()
145145
if ($this->promise !== null) {
146146
$this->promise->then(function (DatabaseInterface $db) {
147147
$db->close();
148+
}, function () {
149+
// ignore to avoid reporting unhandled rejection
148150
});
149151
if ($this->promise !== null) {
150152
$this->promise->cancel();

src/Io/ProcessIoDatabase.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,11 @@ public function quit()
115115
$promise = $this->send('close', array());
116116

117117
if ($this->process->stdin === $this->process->stdout) {
118-
$promise->then(function () { $this->process->stdin->close(); });
118+
$promise->then(function () {
119+
$this->process->stdin->close();
120+
}, function () {
121+
// ignore to avoid reporting unhandled rejection
122+
});
119123
} else {
120124
$this->process->stdin->end();
121125
}

tests/Io/LazyDatabaseTest.php

+13-7
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,9 @@ public function testExecAfterPreviousFactoryRejectsUnderlyingDatabaseWillCreateN
167167
new Promise(function () { })
168168
);
169169

170-
$this->db->exec('CREATE');
170+
$promise = $this->db->exec('CREATE');
171+
$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
172+
171173
$deferred->reject($error);
172174

173175
$this->db->exec('CREATE');
@@ -392,9 +394,11 @@ public function testQueryAfterPreviousFactoryRejectsUnderlyingDatabaseWillCreate
392394
$this->factory->expects($this->exactly(2))->method('open')->willReturnOnConsecutiveCalls(
393395
$deferred->promise(),
394396
new Promise(function () { })
395-
);
397+
);
398+
399+
$promise = $this->db->query('CREATE');
400+
$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
396401

397-
$this->db->query('CREATE');
398402
$deferred->reject($error);
399403

400404
$this->db->query('CREATE');
@@ -408,7 +412,7 @@ public function testQueryAfterPreviousUnderlyingDatabaseAlreadyClosedWillCreateN
408412
$this->factory->expects($this->exactly(2))->method('open')->willReturnOnConsecutiveCalls(
409413
\React\Promise\resolve($client),
410414
new Promise(function () { })
411-
);
415+
);
412416

413417
$this->db->query('CREATE');
414418
$client->emit('close');
@@ -433,7 +437,7 @@ public function testQueryAfterQueryWillNotStartIdleTimerWhenFirstQueryResolves()
433437
$client->expects($this->exactly(2))->method('query')->willReturnOnConsecutiveCalls(
434438
$deferred->promise(),
435439
new Promise(function () { })
436-
);
440+
);
437441

438442
$this->factory->expects($this->once())->method('open')->willReturn(\React\Promise\resolve($client));
439443

@@ -451,7 +455,7 @@ public function testQueryAfterQueryWillStartAndCancelIdleTimerWhenSecondQuerySta
451455
$client->expects($this->exactly(2))->method('query')->willReturnOnConsecutiveCalls(
452456
$deferred->promise(),
453457
new Promise(function () { })
454-
);
458+
);
455459

456460
$this->factory->expects($this->once())->method('open')->willReturn(\React\Promise\resolve($client));
457461

@@ -524,7 +528,9 @@ public function testCloseAfterExecWillEmitCloseWithoutErrorWhenUnderlyingDatabas
524528
$this->db->on('error', $this->expectCallableNever());
525529
$this->db->on('close', $this->expectCallableOnce());
526530

527-
$this->db->exec('CREATE');
531+
$promise = $this->db->exec('CREATE');
532+
$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
533+
528534
$this->db->close();
529535
}
530536

0 commit comments

Comments
 (0)