-
fix(core): prevent DB corruption from concurrent initialization (#34861)
发布于
2026-03-19 22:51:03 +00:00 Current Behavior
When multiple processes call
connectToNxDb()concurrently (plugin
workers viastartAnalytics(), daemon, main CLI), two bugs can corrupt
the workspace database:Bug 1: Lock file inode race.
unlock_file()deletes the lock file
after unlocking, allowing a subsequentFile::create()to produce a new
file with a different inode. Two processes can hold "the lock"
simultaneously on different file objects, breaking mutual exclusion.Bug 2: Partial file cleanup on version mismatch/connection failure.
Thereasonarm andErrarm ininitialize_dbcall
remove_file(db_path)which only deletes.db, leaving stale.db-wal
and.db-shmon disk. The recursiveinitialize_dbcreates a fresh
.db, but SQLite detects the stale WAL (different inode salt) and
deletes it — destroying all data that existed only in the WAL.Both bugs lead to:
Database file exists but has no metadata table.Expected Behavior
- Lock file persists across lock/unlock cycles — all processes
serialize through the same inode - When DB recreation is needed, all auxiliary files (
.db,.db-wal,
.db-shm) are cleaned up together viaremove_all_database_files
Co-authored-by: nx-cloud[bot] <71083854+nx-cloud[bot]@users.noreply.github.com>
下载附件
- Lock file persists across lock/unlock cycles — all processes