rpc: Make unloadwallet wait for complete wallet unload
Summary:
Currently the unloadwallet RPC is asynchronous, it only signals the intent to unload the wallet and then returns the response to the client. The actual unload can happen later and the client has no way to be notified of that. This PR makes the unloadwallet RPC synchronous, meaning that it blocks until the wallet is fully unloaded.
This is expected to fix wallet_multiwallet.py failure with TSAN.
Also include an undefined behavior issue, where the wallet pointer
is used after freed (see https://github.com/bitcoin/bitcoin/issues/16668).
Backport of PR14941 and PR16716.
Depends on D5079.
Test Plan:
ninja check check-functional
Run the CI build-tsan configuration several times, check that the
wallet_multiwallet failure has gone.
Reviewers: #bitcoin_abc, jasonbcox, deadalnix
Reviewed By: #bitcoin_abc, jasonbcox, deadalnix
Subscribers: deadalnix
Differential Revision: https://reviews.bitcoinabc.org/D5080