Skip to content

Commit 06d1aef

Browse files
committed
fix(Database): reset pretend mode in BaseConnection::prepare() on exception
BaseConnection::prepare() enables pretend mode while the user closure builds the query. If the closure threw, the following pretend(false) never ran, leaving the connection in pretend mode. Every later query on the same connection was then silently discarded instead of being executed, which is especially harmful in long-lived processes such as Worker Mode. Wrap the closure call in try/finally so pretend mode is always reset, and add a regression test covering the throwing closure.
1 parent 59e5eea commit 06d1aef

3 files changed

Lines changed: 26 additions & 3 deletions

File tree

‎docs/changelogs/v4.7.7.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Release Date: Unreleased
1616

1717
## Bugs Fixed
1818

19+
- **Database:** Fixed a bug where `BaseConnection::prepare()` did not reset pretend mode when the closure threw an exception, leaving the connection in pretend mode so that subsequent queries on it were silently discarded.
1920
- **HTTP:** Fixed a bug where `UploadedFile::move()` and `UploadedFile::store()` on Windows could store a file under a different name than `getName()` reported, or write its contents to an NTFS alternate data stream. Filenames ending in a period or space or containing a colon are now rejected, including explicitly supplied names.
2021
- **Security:** Fixed a bug where `Security::__construct()` threw a `TypeError` when the CSRF cookie was an array, causing an HTTP 500 response for affected requests.
2122
- **Worker Mode:** Fixed a bug where every request that ended with an uncaught exception, including a `404 Page Not Found` without a 404 override, called `exit()` in the exception handler. This ended the worker script, so FrankenPHP booted a new one, and the worker loop skipped its cleanup after the request. The exception handler now returns to the worker loop.

‎system/Database/BaseConnection.php‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,9 +1188,11 @@ public function prepare(Closure $func, array $options = [])
11881188

11891189
$this->pretend();
11901190

1191-
$sql = $func($this);
1192-
1193-
$this->pretend(false);
1191+
try {
1192+
$sql = $func($this);
1193+
} finally {
1194+
$this->pretend(false);
1195+
}
11941196

11951197
if ($sql instanceof QueryInterface) {
11961198
$sql = $sql->getOriginalQuery();

‎tests/system/Database/Live/PretendTest.php‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use CodeIgniter\Test\CIUnitTestCase;
1818
use CodeIgniter\Test\DatabaseTestTrait;
1919
use PHPUnit\Framework\Attributes\Group;
20+
use RuntimeException;
2021

2122
/**
2223
* @internal
@@ -46,4 +47,23 @@ public function testPretendReturnsQueryObject(): void
4647

4748
$this->assertInstanceOf(Query::class, $result);
4849
}
50+
51+
public function testPrepareResetsPretendWhenClosureThrowsException(): void
52+
{
53+
$this->db->pretend(false);
54+
55+
$exception = new RuntimeException('Error in prepare closure');
56+
57+
try {
58+
$this->db->prepare(static function () use ($exception): never {
59+
throw $exception;
60+
});
61+
62+
$this->fail('prepare() must rethrow the exception thrown by the closure.');
63+
} catch (RuntimeException $e) {
64+
$this->assertSame($exception, $e, 'prepare() must rethrow the exception thrown by the closure.');
65+
}
66+
67+
$this->assertFalse($this->db->pretend, 'prepare() must reset pretend mode when the closure throws.');
68+
}
4969
}

0 commit comments

Comments
 (0)