wallet: Don't crash if the SQLite db can't be created during migration #36821

pull omegadrh wants to merge 1 commits into bitcoin:master from omegadrh:fix/new-db-lock-error changing 6 files +331 −41
  1. omegadrh commented at 7:17 PM on November 9, 2026: none

    Rationale / motivation

    The rationale for this PR is to improve the user experience in new_db failure to migrate a wallet.

    My story

    After using bitcoin core for a while -- and then ignoring it for a while -- I tried to open my old wallet with v31 release build, saw I had to migrate it, attempted to do so, and the app crashed; running from a terminal, I reproduced and got an error in the terminal:

    ./wallet/wallet.cpp:3868: bool wallet::CWallet::MigrateToSQLite(bilingual_str&): Assertion `new_db' failed.
    

    Explanation

    Tracking down this assertion, I discovered the old BDB format was getting migrated to SQLite, but my wallet was stored on a CIFS mount (v3.1, nounix), which doesn't support the BEGIN EXCLUSIVE lock requested here. (fcntl(3, F_SETLK, {F_WRLCK, start=1073741826, len=510}) = -1 EACCES)

    MigrateToSQLite() deletes the legacy BDB wallet before creating the new SQLite database, then asserts that the creation succeeded. Since this fails in my case (due to CIFS not supporting the requested lock), the wallet file is gone, leaving only the .legacy.bak copy made before migration. However, this could also be triggered locally by a full disk or permission errors.

    This change attempts to populate the new database with a temporary filename, and only deletes the legacy wallet if that succeeds -- otherwise clean up the failed attempt and return an error with the underlying reason and a hint about filesystem locking. I included equivalent error handling/cleanup for other cases during migration that were also previously asserts, even though they weren't strictly part of the symptom I experienced.

    Steps to reproduce

    1. Create an old BDB wallet (e.g., Bitcoin Core v27) on a CIFS mount (definitely not the only way, but an easy path for me), or fill up the filesystem after creating the wallet.
    2. Attempt to migrate the wallet with Bitcoin Core v31

    Expected result: since the wallet cannot be migrated, the application should tell the user why and leave the original wallet in its original state.

    Actual result: Bitcoin Core exits and leaves a 0-byte wallet.dat in its place.

    Tests

    I added a few new tests covering this case and behavior change. I've only tested this on Linux. Without the fix, the tests abort on master.

  2. fix(wallet): avoid crash if db create fails in migration
    MigrateToSQLite() deleted the legacy BDB file before creating the new
    SQLite database, then asserted that the creation succeeded.  When
    it fails, for example because the wallet directory is on a network
    filesystem (SMB/CIFS) where SQLite cannot obtain an exclusive lock,
    or the filesystem is full or has permission errors, the node aborts
    with "Assertion `new_db' failed" and the wallet file is gone, leaving
    only the .legacy.bak backup made before migration.
    
    Create the new database next to the old one, and swap it in only
    after the creation is successful.
    
    If the new database fails before completing, remove the failed
    attempt and return an error with the underlying reason and a hint.
    
    The legacy file is deleted once all records have been committed to
    the new database and it's in place.
    af9a4ab8a0
  3. DrahtBot added the label Wallet on Nov 9, 2026
  4. DrahtBot commented at 7:17 PM on November 9, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    <!--006a51241073e994b41acfe9ec718e94-->

    External sites

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline and AI policy for information on the review process. A summary of reviews will appear here.

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #35990 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35990.svg"></sub> (wallet: harden the read-only BDB parser against crafted files by shuv-amp)
    • #34909 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/34909.svg"></sub> (wallet, refactor: modularise wallet by extracting out legacy wallet migration by rkrux)
    • #34193 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/34193.svg"></sub> (wallet: make migration more robust against failures by furszy)

    If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  5. furszy commented at 7:47 PM on November 9, 2026: member

    Have you seen #34193? it should also be fixing this in a more comprehensive way.

  6. achow101 commented at 10:33 PM on November 12, 2026: member

    Approach NACK and appears to be AI written, please read the AI policy.

    An issue would be more appropriate for this problem rather than a PR.

    There are definitely ways to do migration better, and #34193 should resolve many issues in a more comprehensive manner. There are further ideas to increase robustness of migration which will take some time to implement, but can probably handle all such failure scenarios better.

    Also, whatever your CIFS configuration is not one that we can support. We rely heavily on sqlite which has its own requirements on filesystems. If sqlite cannot work on your filesystems, then we will not be able to load the wallet, even after migration.

  7. achow101 closed this on Nov 12, 2026


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-11-19 19:51 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me