Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wallet: Handle duplicate fileid exception #16923

Merged
merged 2 commits into from
Apr 2, 2020

Conversation

promag
Copy link
Contributor

@promag promag commented Sep 20, 2019

Handle the duplicate fileid exception thrown at CheckUniqueFileid in tow cases:

  • when duplicate wallets are set on the command line - catch in LoadWallets;
  • when a duplicate wallet is loaded dynamically - catch in LoadWallet.

Fixes #16776.

@promag
Copy link
Contributor Author

promag commented Sep 20, 2019

To backport?

@promag promag force-pushed the 2019-09-fix-duplicate-exception branch from 1c55c12 to 8b8964e Compare September 20, 2019 15:15
@cvengler
Copy link
Contributor

I don't know if that's a thing here but there is no function at the end which returns something (In practice it is). Probably some compilers will give a warning or errer so you might should add a return false; at the end

Copy link
Member

@jonatack jonatack left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @promag for working on this.

Note: the PR description should be updated to suggest reviewing with git show -w 8b8964e

Functional test wallet_multiwallet.py --usecli is failing for me with AssertionError: [node 0] Expected message "BerkeleyBatch: Can't open database w8_copy \(duplicates fileid \w+ from w8\)" does not partially match stderr: ""

Here are the results at 8b8964e of loading 2 identical wallets in ~/.bitcoin/testnet3/wallets/ named t7 and t8:

$ ./src/qt/bitcoin-qt -debug -testnet
terminate called after throwing an instance of 'std::runtime_error'
  what():  BerkeleyBatch: Can't open database wallet.dat (duplicates fileid 99059e0100fe00005cbe4ae945270e0000000000 from wallet.dat)
Aborted
$ ./src/bitcoind -testnet -wallet=t7.dat -wallet=t8.dat
2019-09-20T17:55:40Z Bitcoin Core version v0.18.99.0-8b8964e872 (debug build)
...
2019-09-20T17:55:40Z Using data directory /home/jon/.bitcoin/testnet3
2019-09-20T17:55:40Z Using wallet directory /home/jon/.bitcoin/testnet3/wallets
2019-09-20T17:55:40Z init message: Verifying wallet(s)...
2019-09-20T17:55:40Z Using BerkeleyDB version Berkeley DB 4.8.30: (April  9, 2010)
2019-09-20T17:55:40Z Using wallet /home/jon/.bitcoin/testnet3/wallets/t7.dat
2019-09-20T17:55:40Z BerkeleyEnvironment::Open: LogDir=/home/jon/.bitcoin/testnet3/wallets/t7.dat/database
                      ErrorFile=/home/jon/.bitcoin/testnet3/wallets/t7.dat/db.log
2019-09-20T17:55:40Z scheduler thread start
2019-09-20T17:55:40Z Using BerkeleyDB version Berkeley DB 4.8.30: (April  9, 2010)
2019-09-20T17:55:40Z Using wallet /home/jon/.bitcoin/testnet3/wallets/t8.dat
2019-09-20T17:55:40Z BerkeleyEnvironment::Open: LogDir=/home/jon/.bitcoin/testnet3/wallets/t8.dat/database
                      ErrorFile=/home/jon/.bitcoin/testnet3/wallets/t8.dat/db.log

@promag promag force-pushed the 2019-09-fix-duplicate-exception branch from 01717f5 to fd576c4 Compare September 29, 2019 22:17
@laanwj laanwj added the Bug label Sep 30, 2019
@DrahtBot
Copy link
Contributor

DrahtBot commented Oct 7, 2019

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

Conflicts

Reviewers, this pull request conflicts with the following ones:

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.

@promag promag force-pushed the 2019-09-fix-duplicate-exception branch from fd576c4 to e06614b Compare November 3, 2019 23:49
@hebasto
Copy link
Member

hebasto commented Nov 20, 2019

Dynamically loading a duplicate wallet doesn't result in the same problem.

That's not the case, unfortunately. Tested on Linux Mint 19.2:

2019-11-20T09:42:58Z [] Using BerkeleyDB version Berkeley DB 4.8.30: (April  9, 2010)
2019-11-20T09:42:58Z [] Using wallet /home/hebasto/.bitcoin/testnet3/wallets/w20191015
2019-11-20T09:42:58Z [] BerkeleyEnvironment::Open: LogDir=/home/hebasto/.bitcoin/testnet3/wallets/w20191015/database ErrorFile=/home/hebasto/.bitcoin/testnet3/wallets/w20191015/db.log
2019-11-20T09:42:58Z [] init message: Loading wallet...
2019-11-20T09:42:58Z [] BerkeleyEnvironment::Open: LogDir=/home/hebasto/.bitcoin/testnet3/wallets/w20191015/database ErrorFile=/home/hebasto/.bitcoin/testnet3/wallets/w20191015/db.log
2019-11-20T09:42:58Z [] [w20191015] Wallet File Version = 169900
2019-11-20T09:42:58Z [] [w20191015] Keys: 2003 plaintext, 0 encrypted, 2003 w/ metadata, 2003 total. Unknown wallet records: 0
2019-11-20T09:42:58Z [] [w20191015] Wallet completed loading in              29ms
...
2019-11-20T09:43:16Z [] Using BerkeleyDB version Berkeley DB 4.8.30: (April  9, 2010)
2019-11-20T09:43:16Z [] Using wallet /home/hebasto/.bitcoin/testnet3/wallets/w20191015 (copy)
2019-11-20T09:43:16Z [] BerkeleyEnvironment::Open: LogDir=/home/hebasto/.bitcoin/testnet3/wallets/w20191015 (copy)/database ErrorFile=/home/hebasto/.bitcoin/testnet3/wallets/w20191015 (copy)/db.log
2019-11-20T09:43:16Z [] init message: Loading wallet...
2019-11-20T09:43:16Z [] BerkeleyEnvironment::Open: LogDir=/home/hebasto/.bitcoin/testnet3/wallets/w20191015 (copy)/database ErrorFile=/home/hebasto/.bitcoin/testnet3/wallets/w20191015 (copy)/db.log
terminate called after throwing an instance of 'std::runtime_error'
  what():  BerkeleyBatch: Can't open database wallet.dat (duplicates fileid f78d2600020800008513f6218301020000000000 from wallet.dat)
Aborted (core dumped)

@promag
Copy link
Contributor Author

promag commented Jan 9, 2020

I don't know if that's a thing here but there is no function at the end which returns something (In practice it is). Probably some compilers will give a warning or errer so you might should add a return false; at the end

@emilengler I think you mean in LoadWallets, note that all branches have a return value.

@promag
Copy link
Contributor Author

promag commented Jan 9, 2020

@hebasto IIRC I tried with no success to hit that, prolly I've done something wrong.

@hebasto
Copy link
Member

hebasto commented Jan 9, 2020

@promag

@hebasto IIRC I tried with no success to hit that, prolly I've done something wrong.

I can confirm that the fix does not work, unfortunately.

Steps to reproduce:

  1. prepare a copy of a wallet:
$ ls ~/.bitcoin/testnet3/wallets/ | grep w20191015
w20191015
w20191015 (copy)
  1. load wallets one by one

on master:

$ ./src/qt/bitcoin-qt -testnet
terminate called after throwing an instance of 'std::runtime_error'
  what():  BerkeleyBatch: Can't open database wallet.dat (duplicates fileid f78d2600020800008513f6218301020000000000 from wallet.dat)
Aborted (core dumped)

with this pr:

$ ./src/qt/bitcoin-qt -testnet
terminate called after throwing an instance of 'std::runtime_error'
  what():  BerkeleyBatch: Can't open database wallet.dat (duplicates fileid f78d2600020800008513f6218301020000000000 from wallet.dat)
Aborted (core dumped)

Literally, I did steps as described in #16776 (comment)

@hebasto
Copy link
Member

hebasto commented Mar 27, 2020

@promag are you still working on this pr?

@promag promag force-pushed the 2019-09-fix-duplicate-exception branch from e06614b to 635f509 Compare March 28, 2020 02:15
@promag promag changed the title wallet: Fix duplicates fileid exception on start wallet: Handle duplicates fileid exception Mar 28, 2020
@promag promag changed the title wallet: Handle duplicates fileid exception wallet: Handle duplicate fileid exception Mar 28, 2020
@promag promag force-pushed the 2019-09-fix-duplicate-exception branch from 635f509 to 4deb8fa Compare March 28, 2020 11:06
@promag
Copy link
Contributor Author

promag commented Mar 28, 2020

@hebasto updated OP, thanks for the remind!

Copy link
Member

@hebasto hebasto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested 4deb8fa on Linux Mint 19.3:

Screenshot from 2020-03-28 20-14-25

i.e., the exception is handled, but the client keeps trying to load the duplicated wallet forever.

@promag
Copy link
Contributor Author

promag commented Mar 28, 2020

i.e., the exception is handled, but the client keeps trying to load the duplicated wallet forever.

Forever how?

@hebasto
Copy link
Member

hebasto commented Mar 28, 2020

i.e., the exception is handled, but the client keeps trying to load the duplicated wallet forever.

Forever how?

Run test again: it lasts 2 minutes and keeps going...

@promag
Copy link
Contributor Author

promag commented Mar 30, 2020

@hebasto I fail to reproduce your case on osx. Does the progress go away when you press "OK" on the "Open wallet failed" dialog? On osx the error dialog shows on top.

@hebasto
Copy link
Member

hebasto commented Mar 30, 2020

@promag

Does the progress go away when you press "OK" on the "Open wallet failed" dialog?

No.

On osx the error dialog shows on top.

On Linux Mint 19.3 the sequence of events is as follows:

@hebasto
Copy link
Member

hebasto commented Mar 30, 2020

ACK a7f81a9, tested on Linux Mint 19.3.

@promag promag force-pushed the 2019-09-fix-duplicate-exception branch from a7f81a9 to 68e0ff0 Compare March 30, 2020 12:02
@promag
Copy link
Contributor Author

promag commented Mar 30, 2020

@hebasto improved last commit, no longer wip.

Copy link
Member

@hebasto hebasto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 68e0ff0

@promag your last changes were on my tongue ;)

nit: #include <cassert>

Copy link
Member

@jonatack jonatack left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 68e0ff0 tested rebased on master 5f9cd62

(origin/pr/16923) $ uname -a
Linux 4.19.0-5-amd64 #1 SMP Debian 4.19.37-5+deb10u2 (2019-08-08) x86_64 GNU/Linux
(origin/pr/16923) $ ./src/bitcoind -testnet -wallet=t8-copy.dat -wallet=t8.dat
...
2020-03-30T13:04:17Z Using wallet directory /home/jon/.bitcoin/testnet3/wallets
2020-03-30T13:04:17Z init message: Verifying wallet(s)...
2020-03-30T13:04:17Z Using BerkeleyDB version Berkeley DB 4.8.30: (April  9, 2010)
2020-03-30T13:04:17Z Using wallet /home/jon/.bitcoin/testnet3/wallets/t8-copy.dat
2020-03-30T13:04:17Z BerkeleyEnvironment::Open:
    LogDir=/home/jon/.bitcoin/testnet3/wallets/t8-copy.dat/database
    ErrorFile=/home/jon/.bitcoin/testnet3/wallets/t8-copy.dat/db.log
2020-03-30T13:04:17Z Using BerkeleyDB version Berkeley DB 4.8.30: (April  9, 2010)
2020-03-30T13:04:17Z Using wallet /home/jon/.bitcoin/testnet3/wallets/t8.dat
2020-03-30T13:04:17Z BerkeleyEnvironment::Open:
    LogDir=/home/jon/.bitcoin/testnet3/wallets/t8.dat/database
    ErrorFile=/home/jon/.bitcoin/testnet3/wallets/t8.dat/db.log

Attempting to load wallet t8-copy after loading wallet t8:

Screenshot from 2020-03-30 15-03-30

In a follow-up, it might be helpful if the error message stated the conflicting wallet names.

@promag
Copy link
Contributor Author

promag commented Mar 30, 2020

In a follow-up, it might be helpful if the error message stated the conflicting wallet names.

But It says :) yeah display the wallet name, not the underlying filename which is useless for directory based wallets.

@promag promag force-pushed the 2019-09-fix-duplicate-exception branch from 68e0ff0 to 9eefc6e Compare March 31, 2020 13:23
@promag
Copy link
Contributor Author

promag commented Mar 31, 2020

Rebased after merging #18338.

@jonatack
Copy link
Member

Re-ACK 9eefc6e no change since last review 68e0ff0

Screenshot from 2020-03-31 17-23-57

@laanwj laanwj added this to the 0.20.0 milestone Mar 31, 2020
@jonatack
Copy link
Member

Tested on macOS 10.14.6 as well (preceding test was on Debian).

Screen Shot 2020-03-31 at 7 28 54 PM

@hebasto
Copy link
Member

hebasto commented Mar 31, 2020

re-ACK 9eefc6e

@laanwj laanwj merged commit 6bdd515 into bitcoin:master Apr 2, 2020
@promag promag deleted the 2019-09-fix-duplicate-exception branch April 2, 2020 13:10
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Apr 2, 2020
9eefc6e gui: Delete progress dialog instead of hidding it (João Barbosa)
ee9e88b wallet: Handle duplicate fileid exception (João Barbosa)

Pull request description:

  Handle the duplicate fileid exception thrown at `CheckUniqueFileid` in tow cases:
   - when duplicate wallets are set on the command line - catch in `LoadWallets`;
   - when a duplicate wallet is loaded dynamically - catch in `LoadWallet`.

  Fixes bitcoin#16776.

ACKs for top commit:
  jonatack:
    Re-ACK 9eefc6e no change since last review 68e0ff0
  hebasto:
    re-ACK 9eefc6e

Tree-SHA512: 46e3c1cd6708b54e2d1c4973a74c8d5428822e04cecbc147cf200eb034efa385e867bd749c7c639020e83c9813fae8fed64a851bdd99abf60c33b07e0363f5d5
Fabcien pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Jan 13, 2021
Summary:
Loading two identical wallet files caused a crash
> Handle the duplicate fileid exception thrown at CheckUniqueFileid in tow cases:
>
>  - when duplicate wallets are set on the command line - catch in LoadWallets;
>  - when a duplicate wallet is loaded dynamically - catch in LoadWallet.

This is a backport of Core [[bitcoin/bitcoin#16923 | PR16923]]

Test Plan:
`ninja all check-all`

In bitcoin-qt, backup the currently open wallet to a file named wallet.dat,
then in the console window type `loadwallet /path/to/wallet/`.
Check that the program does not crash and you get a useful error message dialog.

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D8898
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Feb 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

wallet: Duplicate wallets crash bitcoin core
6 participants