You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit f581e83
Browse filesBrowse the repository at this point in the historyBrowse files
Allow configuring the SQLite busy timeout with SQLITE_BUSY_TIMEOUT (#558)
## Summary
SQLite lets only one connection write at a time. When another connection
holds the lock, SQLite waits up to the **busy timeout** and then fails
with `SQLSTATE[HY000]: General error: 5 database is locked`.
`WP_SQLite_Connection` already sets a busy timeout of 10 seconds, but
neither `WP_MySQL_On_SQLite` nor the plugin let you change it.
This shows up on hosts that serve one site from several PHP processes.
Writes take turns without trouble, but a request that holds a write
transaction past the timeout makes every other writer fail instead of
wait. WordPress Studio runs a pool of native PHP workers per site and
hits this case: a long transaction in one worker fails a concurrent
`wpdb->insert()` in another, on GET and POST requests alike. A
development host would rather wait longer than show the user an error.
## Changes
The busy timeout can now be configured, always in **milliseconds**, the
unit SQLite uses for it:
- **`SQLITE_BUSY_TIMEOUT` constant.** The plugin reads an optional
`SQLITE_BUSY_TIMEOUT` constant from `wp-config.php`, the same way it
reads `SQLITE_JOURNAL_MODE`. For example, `define(
'SQLITE_BUSY_TIMEOUT', 60000 );` waits up to 60 seconds, and `0` fails
immediately.
- **`sqlite_busy_timeout` driver option.** `WP_MySQL_On_SQLite` accepts
a `sqlite_busy_timeout` option and passes it to the connection. Like
`sqlite_journal_mode` and `sqlite_synchronous`, it is named after its
SQLite PRAGMA and takes the same values. Without it, the default of
**10000 ms** applies, as before.
- **Validation.** A value that is not an integer from 0 to 2147483647
throws an `InvalidArgumentException`, like an invalid journal mode. A
mistake such as an empty value can no longer silently turn into "don't
wait at all".
- **Renamed connection option.** The internal `WP_SQLite_Connection`
option `timeout` (seconds) is now `busy_timeout` (milliseconds), and
`DEFAULT_SQLITE_TIMEOUT` is now `DEFAULT_SQLITE_BUSY_TIMEOUT`. The
connection sets the timeout with `PRAGMA busy_timeout` instead of
`PDO::ATTR_TIMEOUT`, which only accepts whole seconds and overflows to
no wait at all for very large values.
The constant isn't in the plugin readme yet. Like `SQLITE_JOURNAL_MODE`,
it will be documented with the other configuration options later.
## Testing
End to end in WordPress Studio, with the first version of this PR (which
used seconds) applied to the bundled plugin: one request held a write
transaction for 45s while another worker wrote.
| | Second writer |
|---|---|
| Default timeout | fails after about 19s, "database is locked" |
| `SQLITE_BUSY_TIMEOUT` set to 60 seconds | waits 44s, then succeeds
(GET and POST) |
Closes#548.
---
AI assistance: the Pi coding agent, run by Homeboy, implemented the
change and tests (served model not recorded). Claude Opus via Pi
reviewed it, ran the end-to-end check in Studio, and wrote this
description. Chris Huber reviewed before opening.
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
<!-- coderabbit-ai-generated:v1 -->
## Summary by CodeRabbit
* **New Features**
* SQLite lock-wait duration can now be configured with
`sqlite_busy_timeout`, in milliseconds. The default is 10,000
milliseconds, and the setting is applied when connecting to or
initializing the database.
* Values must be whole numbers from 0 to 2,147,483,647; invalid values
produce an error.
* **Tests**
* Added coverage for timeout configuration, invalid values, and how long
writes wait when another connection holds a lock.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
---------
Co-authored-by: Jan Jakeš <jan@jakes.pro>
0 commit comments