@@ -91,7 +91,14 @@ class WP_SQLite_Storage {
9191 private $ storage_lock_connection ;
9292
9393 /**
94- * Time to wait for the SQLite storage lock, in milliseconds.
94+ * Connection holding the exclusive transaction on the WordPress SQLite database.
95+ *
96+ * @var PDO|null
97+ */
98+ private $ database_lock_connection ;
99+
100+ /**
101+ * Time to wait for existing database connections, in milliseconds.
95102 *
96103 * @var int
97104 */
@@ -133,9 +140,16 @@ public function initialize(): string {
133140 // Never resolve a database path while storage maintenance is in progress.
134141 $ this ->wait_until_unlocked ();
135142
143+ // Check if we're already holding a lock.
144+ $ was_locked = null !== $ this ->storage_lock_connection ;
145+
136146 // Explicitly configured database path.
137147 if ( null !== $ this ->database_path ) {
138148 $ this ->ensure_database ( $ this ->database_path );
149+ if ( $ was_locked ) {
150+ // If the storage was locked, ensure the database is locked as well.
151+ $ this ->lock ();
152+ }
139153 return $ this ->database_path ;
140154 }
141155
@@ -156,7 +170,6 @@ public function initialize(): string {
156170
157171 // Initialize or repair the managed storage under the storage lock.
158172 // Preserve a lock already held by this instance for a larger operation.
159- $ keep_lock = null !== $ this ->storage_lock_connection ;
160173 $ this ->lock ();
161174 try {
162175 // Another process may have completed the initialization meanwhile.
@@ -170,9 +183,10 @@ public function initialize(): string {
170183 }
171184
172185 $ this ->ensure_database ( $ database_path );
186+ $ this ->lock ();
173187 return $ database_path ;
174188 } finally {
175- if ( ! $ keep_lock ) {
189+ if ( ! $ was_locked ) {
176190 $ this ->unlock ();
177191 }
178192 }
@@ -184,27 +198,29 @@ public function initialize(): string {
184198 * The locking mechanism does the following:
185199 * - Waits for a lock held by another process, up to the database lock timeout.
186200 * - Marks maintenance as active so new requests wait.
201+ * - When a database exists, also acquires an exclusive database lock.
202+ *
203+ * Existing database connections must be closed first to avoid blocking the lock.
187204 *
188205 * The lock is released when unlock() is called or the process ends.
189206 *
190207 * @throws RuntimeException When the lock cannot be acquired.
191208 */
192209 public function lock (): void {
193- // Reuse a lock already held by this instance.
194- if ( null !== $ this ->storage_lock_connection ) {
195- return ;
196- }
210+ $ was_locked = null !== $ this ->storage_lock_connection ;
197211
198212 // Serialize maintenance through the dedicated locking database.
199- try {
200- $ this ->ensure_database ( $ this ->lock_path );
201- $ connection = new PDO ( 'sqlite: ' . $ this ->lock_path );
202- $ connection ->setAttribute ( PDO ::ATTR_ERRMODE , PDO ::ERRMODE_EXCEPTION );
203- $ connection ->exec ( 'PRAGMA busy_timeout = ' . $ this ->database_lock_timeout );
204- $ connection ->exec ( 'BEGIN EXCLUSIVE ' );
205- $ this ->storage_lock_connection = $ connection ;
206- } catch ( Throwable $ exception ) {
207- throw new RuntimeException ( 'Failed to acquire the SQLite storage lock. ' , 0 , $ exception );
213+ if ( ! $ was_locked ) {
214+ try {
215+ $ this ->ensure_database ( $ this ->lock_path );
216+ $ connection = new PDO ( 'sqlite: ' . $ this ->lock_path );
217+ $ connection ->setAttribute ( PDO ::ATTR_ERRMODE , PDO ::ERRMODE_EXCEPTION );
218+ $ connection ->exec ( 'PRAGMA busy_timeout = ' . $ this ->database_lock_timeout );
219+ $ connection ->exec ( 'BEGIN EXCLUSIVE ' );
220+ $ this ->storage_lock_connection = $ connection ;
221+ } catch ( Throwable $ exception ) {
222+ throw new RuntimeException ( 'Failed to acquire the SQLite storage lock. ' , 0 , $ exception );
223+ }
208224 }
209225
210226 try {
@@ -215,8 +231,38 @@ public function lock(): void {
215231 }
216232 @chmod ( $ this ->maintenance_path , 0600 );
217233 }
234+
235+ // Reuse a database lock already held by this instance.
236+ if ( null !== $ this ->database_lock_connection ) {
237+ return ;
238+ }
239+
240+ // Look for an existing database so that it can be locked as well.
241+ $ database_path = $ this ->database_path ;
242+ if ( null === $ database_path ) {
243+ $ database_path = $ this ->read_recorded_database_path ();
244+
245+ // The managed database may still be at a legacy path.
246+ if ( null === $ database_path || ! @is_file ( $ database_path ) ) {
247+ $ database_path = $ this ->database_root . self ::DATABASE_FILENAME ;
248+ }
249+ if ( ! @is_file ( $ database_path ) ) {
250+ $ database_path = $ this ->database_root . self ::DATABASE_FILENAME . '.php ' ;
251+ }
252+ }
253+
254+ // Lock the existing database when found.
255+ if ( @is_file ( $ database_path ) ) {
256+ try {
257+ $ this ->database_lock_connection = $ this ->lock_database ( $ database_path );
258+ } catch ( Throwable $ exception ) {
259+ throw new RuntimeException ( 'Failed to lock the SQLite database. ' , 0 , $ exception );
260+ }
261+ }
218262 } catch ( Throwable $ exception ) {
219- $ this ->unlock ();
263+ if ( ! $ was_locked ) {
264+ $ this ->unlock ();
265+ }
220266 throw $ exception ;
221267 }
222268 }
@@ -225,6 +271,7 @@ public function lock(): void {
225271 * Unlock the storage.
226272 */
227273 public function unlock (): void {
274+ $ this ->database_lock_connection = null ;
228275 if ( null !== $ this ->storage_lock_connection ) {
229276 // Remove the marker before releasing the lock that protects it.
230277 @unlink ( $ this ->maintenance_path );
@@ -384,4 +431,65 @@ private function ensure_file( string $path, string $contents ): void {
384431 }
385432 @chmod ( $ path , 0600 );
386433 }
434+
435+ /**
436+ * Acquire an exclusive transaction on an SQLite database.
437+ *
438+ * @param string $database_path Absolute database path.
439+ * @return PDO Connection holding the transaction.
440+ */
441+ private function lock_database ( string $ database_path ): PDO {
442+ // Do not silently create an empty database if the expected file is missing.
443+ $ pdo_options = array ();
444+ if ( defined ( 'Pdo\Sqlite::ATTR_OPEN_FLAGS ' ) ) {
445+ $ pdo_options [ Pdo \Sqlite::ATTR_OPEN_FLAGS ] = Pdo \Sqlite::OPEN_READWRITE ;
446+ } elseif ( defined ( 'PDO::SQLITE_ATTR_OPEN_FLAGS ' ) ) {
447+ $ pdo_options [ PDO ::SQLITE_ATTR_OPEN_FLAGS ] = PDO ::SQLITE_OPEN_READWRITE ;
448+ }
449+
450+ $ connection = null ;
451+ $ deadline = microtime ( true ) + ( $ this ->database_lock_timeout / 1000 );
452+ do {
453+ if ( microtime ( true ) >= $ deadline ) {
454+ throw new RuntimeException ( 'Failed to acquire an exclusive SQLite database lock. ' );
455+ }
456+
457+ if ( null === $ connection ) {
458+ $ connection = new PDO ( 'sqlite: ' . $ database_path , null , null , $ pdo_options );
459+ $ connection ->setAttribute ( PDO ::ATTR_ERRMODE , PDO ::ERRMODE_EXCEPTION );
460+ $ connection ->exec ( 'PRAGMA busy_timeout = ' . $ this ->database_lock_timeout );
461+ }
462+
463+ try {
464+ // Disable WAL so we can acquire a truly exclusive READ/WRITE lock.
465+ $ journal_mode = $ connection ->query ( 'PRAGMA journal_mode = DELETE ' )->fetchColumn ();
466+ if ( 'delete ' === strtolower ( (string ) $ journal_mode ) ) {
467+ // Acquire exclusive lock.
468+ $ connection ->exec ( 'BEGIN EXCLUSIVE ' );
469+
470+ // Another connection may have restored WAL before the transaction started.
471+ // If that's the case, release the lock and retry in the next iteration.
472+ $ journal_mode = $ connection ->query ( 'PRAGMA journal_mode ' )->fetchColumn ();
473+ if ( 'delete ' !== strtolower ( (string ) $ journal_mode ) ) {
474+ $ connection ->exec ( 'ROLLBACK ' );
475+ } else {
476+ // Connection lock successfully acquired.
477+ return $ connection ;
478+ }
479+ }
480+ } catch ( PDOException $ exception ) {
481+ $ error_info = $ connection ->errorInfo ();
482+ $ sqlite_busy = 5 ;
483+ $ database_busy = isset ( $ error_info [1 ] ) && ( (int ) $ error_info [1 ] & 0xff ) === $ sqlite_busy ;
484+ if ( ! $ database_busy ) {
485+ throw $ exception ;
486+ }
487+
488+ // Close the connection so any raw transaction is rolled back before retrying.
489+ $ connection = null ;
490+ }
491+
492+ usleep ( 100000 ); // 100 milliseconds.
493+ } while ( true );
494+ }
387495}
0 commit comments