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

gui: Fix shutdown order #15280

Merged
merged 4 commits into from Feb 4, 2019
Merged

gui: Fix shutdown order #15280

merged 4 commits into from Feb 4, 2019

Conversation

@promag
Copy link
Member

@promag promag commented Jan 29, 2019

This PR consists in small fixes in order to have a clean shutdown from the GUI.

Copy link
Member

@Sjors Sjors left a comment

Concept ACK, but paging @Empact, @laanwj, @LeandroRocha84 #13217 and @MarcoFalke #13880 as QT shutdown connoisseurs.

@@ -218,6 +218,8 @@ BitcoinApplication::~BitcoinApplication()
#ifdef ENABLE_WALLET
delete paymentServer;
paymentServer = nullptr;
delete m_wallet_controller;
m_wallet_controller = nullptr;
Copy link
Member

@Sjors Sjors Jan 29, 2019

Can you explain this move, either in a comment or in the commit message?

Copy link
Member Author

@promag promag Jan 29, 2019

The m_wallet_controller must be deleted after window (because it's used there).

Copy link
Member

@Sjors Sjors Jan 29, 2019

Thanks. I meant in a source code comment though :-) The shutdown logic keeps causing headaches, so there's no such thing as too much documentation I think.

Copy link
Member Author

@promag promag Jan 29, 2019

I'll add if I need to update this branch for other reasons, otherwise I'll improve the shutdown even further with more comments. From testing this is enough to get #15153 working without problems.

@@ -95,6 +95,9 @@ class BitcoinGUI : public QMainWindow
*/
bool hasTrayIcon() const { return trayIcon; }

/** Disconnect core signals from GUI client */
void unsubscribeFromCoreSignals();
Copy link
Member

@Sjors Sjors Jan 29, 2019

Nit: might as well merge this with the next commit, which explains why this exposure is needed..

Copy link
Member Author

@promag promag Jan 30, 2019

I think it's good to have move only separate?

Copy link
Member

@Empact Empact Jan 31, 2019

I like to combine exposing a new interface with adding uses for it - I think of it as an expression of the outside-in development principle, the use comes before the changes it motivates/justifies.

window->unsubscribeFromCoreSignals();
// Request node shutdown, which can interrupt long operations, like
// rescanning a wallet.
m_node.startShutdown();
Copy link
Member

@Sjors Sjors Jan 29, 2019

Can you explain why you moved startShutdown(); up? Is it because setClientModel could be blocking and so you can to get this over with first?

Copy link
Member Author

@promag promag Jan 29, 2019

Calling setClientModel(nullptr) hits:

if (!model) {
// Client model is being set to 0, this means shutdown() is about to be called.
thread.quit();
thread.wait();
}

and if there is a rescan in progress it blocks until the rescan completes. Calling m_node.startShutdown(); causes the rescan to interrupt.

@jonasschnelli
Copy link
Contributor

@jonasschnelli jonasschnelli commented Jan 29, 2019

utACK 870e35c

src/validationinterface.cpp Outdated Show resolved Hide resolved
@hebasto
Copy link
Member

@hebasto hebasto commented Jan 30, 2019

tACK 870e35c (Linux) modulo #15280 (comment) and #15280 (comment).

@Empact
Copy link
Member

@Empact Empact commented Jan 30, 2019

This could benefit from adding "why" to each commit message, which currently only describe what is done. Will be easier to evaluate now and interpret in the future with that info.

@promag promag force-pushed the 2019-01-gui-shutdown branch 2 times, most recently from df785ea to 0b53bf9 Jan 30, 2019
@promag
Copy link
Member Author

@promag promag commented Jan 30, 2019

Improved each commit message (hope it is now more clear).

A good test case to check with and without this PR is:

  1. add a MilliSleep to the rescan loop, after the while:

    bitcoin/src/wallet/wallet.cpp

    Lines 1659 to 1660 in cb77dc8

    while (block_height && !fAbortRescan && !ShutdownRequested()) {
    if (*block_height % 100 == 0 && progress_end - progress_begin > 0.0) {
  2. launch with -nowallet -rescan
  3. call loadwallet a_wallet_with_some_transactions in the console window
  4. close the application

@DrahtBot
Copy link
Contributor

@DrahtBot DrahtBot commented Jan 31, 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:

  • #15153 (gui: Add Open Wallet menu by promag)

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.

@Empact
Copy link
Member

@Empact Empact commented Jan 31, 2019

utACK 0b53bf9

Thanks for expanding those commit messages. Should we also guard in RegisterValidationInterface? It's the only other unguarded access to g_signals.m_internals.

@promag
Copy link
Member Author

@promag promag commented Feb 1, 2019

@Empact different PR IMO. I had a commit to guard it with cs_main but removed since it's not GUI related.

promag added 4 commits Feb 3, 2019
The wallet controller instanced must be deleted after the window instance
since it is used there.
Move only change that makes unsubscribeFromCoreSignals public. It must be
called if the event loop is not running otherwise core signals handlers
can deadlock.
This change forwards the shutdown request on the GUI (close the
application for instace) to the node as soon as possible. This way the
GUI doesn't have to wait for long operations to complete (rescan the
wallet for instance), instead those operations detect the shutdown
request and abort/interrupt.
When a wallet is created it is registered in the validation interface (in
CWallet::CreateWalletFromFile) but it is not immediately added to the
wallets list. If a shutdown is requested before AddWallet (case more
evident when -rescan is set) then m_internals can be released (in
Shutdown -> UnregisterBackgroundSignalScheduler) before the wallet and
then ReleaseWallet would call UnregisterValidationInterface with
m_internals already released.
@promag promag force-pushed the 2019-01-gui-shutdown branch from 0b53bf9 to 0dd6a8c Feb 3, 2019
@laanwj
Copy link
Member

@laanwj laanwj commented Feb 4, 2019

utACK 0dd6a8c

pull bot pushed a commit to jaschadub/bitcoin that referenced this issue Feb 4, 2019
0dd6a8c Check m_internals in UnregisterValidationInterface (João Barbosa)
fd6d499 gui: Fix m_node.startShutdown() order (João Barbosa)
07b9aad gui: Expose BitcoinGUI::unsubscribeFromCoreSignals (João Barbosa)
60e190c gui: Fix WalletController deletion (João Barbosa)

Pull request description:

  This PR consists in small fixes in order to have a clean shutdown from the GUI.

Tree-SHA512: a9c641f202bc810698c4a39d5c5a1f54e54bdab098c412d65418879e00764a9db9f38383813914d591e24e097e49f177942b2ae6c57bba05dcc095e8a1d0b8f4
@laanwj laanwj merged commit 0dd6a8c into bitcoin:master Feb 4, 2019
2 checks passed
@promag promag deleted the 2019-01-gui-shutdown branch Feb 4, 2019
deadalnix added a commit to Bitcoin-ABC/bitcoin-abc that referenced this issue Jun 3, 2020
Summary:
 * gui: Fix WalletController deletion

The wallet controller instanced must be deleted after the window instance
since it is used there.

 * gui: Expose BitcoinGUI::unsubscribeFromCoreSignals

Move only change that makes unsubscribeFromCoreSignals public. It must be
called if the event loop is not running otherwise core signals handlers
can deadlock.

 * gui: Fix m_node.startShutdown() order

This change forwards the shutdown request on the GUI (close the
application for instace) to the node as soon as possible. This way the
GUI doesn't have to wait for long operations to complete (rescan the
wallet for instance), instead those operations detect the shutdown
request and abort/interrupt.

 * Check m_internals in UnregisterValidationInterface

When a wallet is created it is registered in the validation interface (in
CWallet::CreateWalletFromFile) but it is not immediately added to the
wallets list. If a shutdown is requested before AddWallet (case more
evident when -rescan is set) then m_internals can be released (in
Shutdown -> UnregisterBackgroundSignalScheduler) before the wallet and
then ReleaseWallet would call UnregisterValidationInterface with
m_internals already released.

This is a backport of Core [[bitcoin/bitcoin#15280 | PR15280]]

Test Plan:
  ninja all check-all

Run bitcoin-qt on win64 and verify it shuts down properly.

Reviewers: #bitcoin_abc, jasonbcox

Reviewed By: #bitcoin_abc, jasonbcox

Differential Revision: https://reviews.bitcoinabc.org/D6344
ftrader added a commit to bitcoin-cash-node/bitcoin-cash-node that referenced this issue Aug 17, 2020
Summary:
 * gui: Fix WalletController deletion

The wallet controller instanced must be deleted after the window instance
since it is used there.

 * gui: Expose BitcoinGUI::unsubscribeFromCoreSignals

Move only change that makes unsubscribeFromCoreSignals public. It must be
called if the event loop is not running otherwise core signals handlers
can deadlock.

 * gui: Fix m_node.startShutdown() order

This change forwards the shutdown request on the GUI (close the
application for instace) to the node as soon as possible. This way the
GUI doesn't have to wait for long operations to complete (rescan the
wallet for instance), instead those operations detect the shutdown
request and abort/interrupt.

 * Check m_internals in UnregisterValidationInterface

When a wallet is created it is registered in the validation interface (in
CWallet::CreateWalletFromFile) but it is not immediately added to the
wallets list. If a shutdown is requested before AddWallet (case more
evident when -rescan is set) then m_internals can be released (in
Shutdown -> UnregisterBackgroundSignalScheduler) before the wallet and
then ReleaseWallet would call UnregisterValidationInterface with
m_internals already released.

This is a backport of Core [[bitcoin/bitcoin#15280 | PR15280]]

Test Plan:
  ninja all check-all

Run bitcoin-qt on win64 and verify it shuts down properly.

Reviewers: #bitcoin_abc, jasonbcox

Reviewed By: #bitcoin_abc, jasonbcox

Differential Revision: https://reviews.bitcoinabc.org/D6344
PastaPastaPasta added a commit to PastaPastaPasta/dash that referenced this issue Aug 2, 2021
…ionInterface

When a wallet is created it is registered in the validation interface (in
CWallet::CreateWalletFromFile) but it is not immediately added to the
wallets list. If a shutdown is requested before AddWallet (case more
evident when -rescan is set) then m_internals can be released (in
Shutdown -> UnregisterBackgroundSignalScheduler) before the wallet and
then ReleaseWallet would call UnregisterValidationInterface with
m_internals already released.
UdjinM6 added a commit to dashpay/dash that referenced this issue Aug 13, 2021
PastaPastaPasta added a commit to PastaPastaPasta/dash that referenced this issue Sep 21, 2021
0dd6a8c Check m_internals in UnregisterValidationInterface (João Barbosa)
fd6d499 gui: Fix m_node.startShutdown() order (João Barbosa)
07b9aad gui: Expose BitcoinGUI::unsubscribeFromCoreSignals (João Barbosa)
60e190c gui: Fix WalletController deletion (João Barbosa)

Pull request description:

  This PR consists in small fixes in order to have a clean shutdown from the GUI.

Tree-SHA512: a9c641f202bc810698c4a39d5c5a1f54e54bdab098c412d65418879e00764a9db9f38383813914d591e24e097e49f177942b2ae6c57bba05dcc095e8a1d0b8f4
kittywhiskers added a commit to kittywhiskers/dash that referenced this issue Oct 12, 2021
0dd6a8c Check m_internals in UnregisterValidationInterface (João Barbosa)
fd6d499 gui: Fix m_node.startShutdown() order (João Barbosa)
07b9aad gui: Expose BitcoinGUI::unsubscribeFromCoreSignals (João Barbosa)
60e190c gui: Fix WalletController deletion (João Barbosa)

Pull request description:

  This PR consists in small fixes in order to have a clean shutdown from the GUI.

Tree-SHA512: a9c641f202bc810698c4a39d5c5a1f54e54bdab098c412d65418879e00764a9db9f38383813914d591e24e097e49f177942b2ae6c57bba05dcc095e8a1d0b8f4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

8 participants