Skip to content

Commit e25ba0c

Browse files
committed
Allow configuring the SQLite busy timeout with SQLITE_BUSY_TIMEOUT
1 parent b5dc0cc commit e25ba0c

8 files changed

Lines changed: 90 additions & 0 deletions

File tree

‎packages/mysql-on-sqlite/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ version and configuring the SQLite connection:
7676
| --- | --- | --- |
7777
| `mysql_version` | MySQL version to emulate, represented as an integer | `80038` |
7878
| `sqlite_pdo` | Existing PDO SQLite connection | A new connection for `path` |
79+
| `sqlite_timeout` | SQLite busy timeout in whole seconds | `10` |
7980
| `sqlite_journal_mode` | SQLite journal mode | `WAL` |
8081
| `sqlite_synchronous` | SQLite synchronous setting | `NORMAL` in WAL mode; otherwise the SQLite default |
8182

‎packages/mysql-on-sqlite/src/sqlite/class-wp-mysql-on-sqlite.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,8 @@ class WP_MySQL_On_SQLite extends PDO {
795795
*
796796
* @type int $mysql_version Optional. MySQL version to emulate. Default 80038.
797797
* @type PDO|null $sqlite_pdo Optional. Existing PDO SQLite connection.
798+
* @type int|null $sqlite_timeout Optional. SQLite busy timeout in whole seconds.
799+
* Default null (10 seconds).
798800
* @type string|null $sqlite_journal_mode Optional. SQLite journal mode. Default 'WAL'.
799801
* @type string|int|null $sqlite_synchronous Optional. SQLite synchronous setting.
800802
* }
@@ -876,6 +878,7 @@ function ( $key ) {
876878
}
877879

878880
$connection_options = array(
881+
'timeout' => $options['sqlite_timeout'] ?? null,
879882
'journal_mode' => $options['sqlite_journal_mode'] ?? null,
880883
'synchronous' => $options['sqlite_synchronous'] ?? null,
881884
'pdo_options' => $connection_pdo_options,

‎packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Concurrency_Tests.php‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,35 @@ private function assertReadOnlyQuerySucceedsUnderWriteLock( string $query ): voi
178178
}
179179
}
180180

181+
public function testWriteQueryFailsImmediatelyWithZeroBusyTimeoutWhileAnotherConnectionHoldsWriteLock(): void {
182+
// Connection A: set up the database and hold a write transaction.
183+
$conn_a = new WP_SQLite_Connection( array( 'path' => $this->db_path ) );
184+
$driver_a = $this->create_driver( $conn_a );
185+
$driver_a->query( 'CREATE TABLE t (id INT, name VARCHAR(255))' );
186+
$driver_a->query( "INSERT INTO t VALUES (1, 'Alice')" );
187+
188+
// Simulate another PHP process holding a write transaction.
189+
$conn_a->get_pdo()->exec( 'BEGIN IMMEDIATE' );
190+
191+
try {
192+
// Connection B with a zero busy timeout — any lock conflict fails
193+
// immediately instead of waiting for the default timeout.
194+
$driver_b = new WP_MySQL_On_SQLite(
195+
sprintf( 'mysql-on-sqlite:path=%s;dbname=wp', $this->db_path ),
196+
null,
197+
null,
198+
array( 'sqlite_timeout' => 0 )
199+
);
200+
201+
$this->expectException( PDOException::class );
202+
$this->expectExceptionMessage( 'database is locked' );
203+
204+
$driver_b->query( "INSERT INTO t VALUES (2, 'Bob')" );
205+
} finally {
206+
$conn_a->get_pdo()->exec( 'ROLLBACK' );
207+
}
208+
}
209+
181210
private function create_in_memory_driver(): WP_MySQL_On_SQLite {
182211
$pdo_class = PHP_VERSION_ID >= 80400 ? Pdo\Sqlite::class : PDO::class;
183212
$pdo = new $pdo_class( 'sqlite::memory:' );

‎packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_PDO_API_Tests.php‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,34 @@ public function test_journal_mode_and_synchronous_driver_options(): void {
379379
}
380380
}
381381

382+
public function test_timeout_driver_option_sets_sqlite_busy_timeout(): void {
383+
$driver = new WP_MySQL_On_SQLite(
384+
'mysql-on-sqlite:path=:memory:;dbname=wp',
385+
null,
386+
null,
387+
array(
388+
'sqlite_timeout' => 5,
389+
)
390+
);
391+
392+
// The PDO SQLite driver does not support reading "PDO::ATTR_TIMEOUT"
393+
// back with "getAttribute()", but the SQLite busy timeout reflects it
394+
// in milliseconds.
395+
$this->assertSame(
396+
5000,
397+
(int) $driver->get_sqlite_pdo()->query( 'PRAGMA busy_timeout' )->fetchColumn()
398+
);
399+
}
400+
401+
public function test_timeout_defaults_to_the_connection_default(): void {
402+
$driver = new WP_MySQL_On_SQLite( 'mysql-on-sqlite:path=:memory:;dbname=wp' );
403+
404+
$this->assertSame(
405+
WP_SQLite_Connection::DEFAULT_SQLITE_TIMEOUT * 1000,
406+
(int) $driver->get_sqlite_pdo()->query( 'PRAGMA busy_timeout' )->fetchColumn()
407+
);
408+
}
409+
382410
public function test_query(): void {
383411
$result = $this->driver->query( "SELECT 1, 'abc'" );
384412
$this->assertInstanceOf( WP_MySQL_On_SQLite_Statement::class, $result );

‎packages/plugin-sqlite-database-integration/README.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,22 @@ for an in-memory SQLite database. The database directory must be writable by PHP
5858
Integrations should always read `DB_PATH` after WordPress loads instead of assuming
5959
a fixed database path.
6060

61+
## Busy timeout
62+
63+
SQLite allows only one writer at a time. When a write cannot start because another
64+
connection holds the write lock, SQLite waits up to the busy timeout before failing
65+
with a "database is locked" error. The default timeout is 10 seconds.
66+
67+
To change the timeout, define the `SQLITE_BUSY_TIMEOUT` constant in `wp-config.php`
68+
with a value in whole seconds:
69+
70+
```php
71+
// Wait up to 30 seconds for the write lock before failing.
72+
define( 'SQLITE_BUSY_TIMEOUT', 30 );
73+
```
74+
75+
Set it to `0` to fail immediately instead of waiting.
76+
6177
## Development
6278

6379
Run these commands from the repository root:

‎packages/plugin-sqlite-database-integration/readme.txt‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,17 @@ To properly **secure the SQLite database**, define the `DB_PATH` constant to an
7373

7474
The value of `DB_PATH` must be an absolute path to the SQLite database file, or `:memory:` for an in-memory SQLite database. The database directory must be writable by PHP. Changing `DB_PATH` selects a different database without moving an existing one.
7575

76+
= How can I configure the SQLite busy timeout? =
77+
78+
SQLite allows only one writer at a time. When a write query cannot start because another connection holds the write lock, SQLite waits up to the busy timeout before failing with a "database is locked" error. The default timeout is 10 seconds.
79+
80+
To change the timeout, define the `SQLITE_BUSY_TIMEOUT` constant in `wp-config.php` with a value in whole seconds:
81+
82+
// Wait up to 30 seconds for the write lock before failing.
83+
define( 'SQLITE_BUSY_TIMEOUT', 30 );
84+
85+
Set it to `0` to fail immediately instead of waiting.
86+
7687
= What does the plugin require? =
7788

7889
In addition to the WordPress and PHP versions listed above, the plugin requires the PDO SQLite PHP extension and SQLite 3.37.0 or newer. The setup screen also needs write access to the `wp-content` directory and will detect conflicting database drop-ins.

‎packages/plugin-sqlite-database-integration/wp-includes/sqlite/class-wp-sqlite-db.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,7 @@ public function db_connect( $allow_bail = true ) {
453453
try {
454454
$options = array(
455455
'sqlite_journal_mode' => defined( 'SQLITE_JOURNAL_MODE' ) ? SQLITE_JOURNAL_MODE : null,
456+
'sqlite_timeout' => defined( 'SQLITE_BUSY_TIMEOUT' ) ? (int) SQLITE_BUSY_TIMEOUT : null,
456457
);
457458
$dbh = new WP_MySQL_On_SQLite(
458459
sprintf(

‎packages/plugin-sqlite-database-integration/wp-includes/sqlite/install-functions.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ function sqlite_make_db_sqlite() {
3030
null,
3131
array(
3232
'sqlite_journal_mode' => defined( 'SQLITE_JOURNAL_MODE' ) ? SQLITE_JOURNAL_MODE : null,
33+
'sqlite_timeout' => defined( 'SQLITE_BUSY_TIMEOUT' ) ? (int) SQLITE_BUSY_TIMEOUT : null,
3334
)
3435
);
3536
$translator->setAttribute( PDO::ATTR_STRINGIFY_FETCHES, true ); // phpcs:ignore WordPress.DB.RestrictedClasses.mysql__PDO

0 commit comments

Comments
 (0)