Repository: bitcoin/bitcoin (language: cpp)

Issue:
ci: failure in `wallet_assumeutxo.py --descriptors`
Merged in #28838. Seems to be failing on all MSVC CI jobs.

https://github.com/bitcoin/bitcoin/actions/runs/7492122146/job/20394956531?pr=29232#step:27:7862:
```bash
  test  2024-01-11T17:24:30.783000Z TestFramework (INFO): Backup can't be loaded during background sync 
 test  2024-01-11T17:24:30.783000Z TestFramework (ERROR): Assertion failed 
                                   Traceback (most recent call last):
                                     File "D:\a\bitcoin\bitcoin\test\functional\test_framework\util.py", line 140, in try_rpc
                                       fun(*args, **kwds)
                                     File "D:\a\bitcoin\bitcoin\test\functional\test_framework\coverage.py", line 50, in __call__
                                       return_val = self.auth_service_proxy_instance.__call__(*args, **kwargs)
                                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                                     File "D:\a\bitcoin\bitcoin\test\functional\test_framework\authproxy.py", line 129, in __call__
                                       raise JSONRPCException(response['error'], status)
                                   test_framework.authproxy.JSONRPCException: remove_all: The process cannot access the file because it is being used by another process.: "D:\a\_temp\test_runner_?_??_20240111_171305\wallet_assumeutxo_47\node1\regtest\w" (-1)
                                   During handling of the above exception, another exception occurred:
                                   Traceback (most recent call last):
                                     File "D:\a\bitcoin\bitcoin\test\functional\test_framework\test_framework.py", line 131, in main
                                       self.run_test()
                                     File "D:\a\bitcoin\bitcoin/test/functional/wallet_assumeutxo.py", line 127, in run_test
                                       assert_raises_rpc_error(-4, "Wallet loading failed. Error loading wallet. Wallet requires blocks to be downloaded, and software does not currently support loading wallets while blocks are being downloaded out of order when using assumeutxo snapshots. Wallet should be able to load successfully after node sync reaches height 299", n1.restorewallet, "w", "backup_w.dat")
                                     File "D:\a\bitcoin\bitcoin\test\functional\test_framework\util.py", line 131, in assert_raises_rpc_error
                                       assert try_rpc(code, message, fun, *args, **kwds), "No exception raised"
                                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                                     File "D:\a\bitcoin\bitcoin\test\functional\test_framework\util.py", line 144, in try_rpc
                                       raise AssertionError("Unexpected JSONRPC error code %i" % e.error["code"])
                                   AssertionError: Unexpected JSONRPC error code -1
```


Discussion / hints:
Thanks for reporting.  Ran into it as well.

https://github.com/bitcoin/bitcoin/actions/runs/7492061657/job/20394747721?pr=29230#step:27:7866
https://github.com/bitcoin/bitcoin/actions/runs/7500571397/job/20419528825?pr=29179
https://github.com/bitcoin/bitcoin/actions/runs/7503027363/job/20426899651
Could this be a bug in the wallet? Why would it call `remove_all` on the wallet file of a completely different node?
> Could this be a bug in the wallet?

Indeed, this looks more like a windows related bug when there's a wallet restoring failure.

> Why would it call `remove_all` on the wallet file of a completely different node?

I don't think it is. I think the problem is more along the lines of the same bitcoind has the database open at the time it tries to cleanup the failed restore.

Fix patch:
diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp
index bf8cfcb082aa9..e03f5532fcf0b 100644
--- a/src/wallet/wallet.cpp
+++ b/src/wallet/wallet.cpp
@@ -3099,6 +3099,7 @@ std::shared_ptr<CWallet> CWallet::Create(WalletContext& context, const std::stri
     if (time_first_key) walletInstance->MaybeUpdateBirthTime(*time_first_key);
 
     if (chain && !AttachChain(walletInstance, *chain, rescan_required, error, warnings)) {
+        walletInstance->m_chain_notifications_handler.reset(); // Reset this pointer so that the wallet will actually be unloaded
         return nullptr;
     }
 


Test patch: