Commit d0f2482
fix(core): prevent DB corruption from concurrent initialization (#34861)
## Current Behavior
When multiple processes call `connectToNxDb()` concurrently (plugin
workers via `startAnalytics()`, 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 subsequent `File::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.**
The `reason` arm and `Err` arm in `initialize_db` call
`remove_file(db_path)` which only deletes `.db`, leaving stale `.db-wal`
and `.db-shm` on disk. The recursive `initialize_db` creates 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
1. Lock file persists across lock/unlock cycles — all processes
serialize through the same inode
2. When DB recreation is needed, all auxiliary files (`.db`, `.db-wal`,
`.db-shm`) are cleaned up together via `remove_all_database_files`
---------
Co-authored-by: nx-cloud[bot] <71083854+nx-cloud[bot]@users.noreply.github.com>1 parent 2343537 commit d0f2482
1 file changed
+8
-20
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
123 | 123 | | |
124 | 124 | | |
125 | 125 | | |
126 | | - | |
127 | | - | |
128 | | - | |
| 126 | + | |
| 127 | + | |
129 | 128 | | |
130 | 129 | | |
131 | | - | |
132 | | - | |
133 | | - | |
134 | | - | |
135 | | - | |
136 | | - | |
137 | | - | |
138 | | - | |
139 | | - | |
| 130 | + | |
140 | 131 | | |
141 | 132 | | |
142 | 133 | | |
| |||
146 | 137 | | |
147 | 138 | | |
148 | 139 | | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
| 140 | + | |
153 | 141 | | |
154 | 142 | | |
155 | 143 | | |
| |||
222 | 210 | | |
223 | 211 | | |
224 | 212 | | |
225 | | - | |
226 | | - | |
| 213 | + | |
| 214 | + | |
227 | 215 | | |
228 | 216 | | |
229 | 217 | | |
| |||
237 | 225 | | |
238 | 226 | | |
239 | 227 | | |
240 | | - | |
241 | | - | |
| 228 | + | |
| 229 | + | |
242 | 230 | | |
243 | 231 | | |
244 | 232 | | |
| |||
0 commit comments