发布

  • fix(core): prevent DB corruption from concurrent initialization (#34861)

    frostbyte_neo 发布于 2026-03-19 22:51:03 +00:00

    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>

    下载附件