Skip to content

Conversation

john-moffett
Copy link
Contributor

@john-moffett john-moffett commented Nov 15, 2022

This is a PR to address bitcoin/bitcoin#26490

The menu bar currently subscribes to window focus change notifications to enable or disable certain menu options in response to the window status.

Notifications are automatically unsubscribed (disconnected in Qt parlance) if the sender is deleted -- in this case, the sender is the QTApplication object (qApp). However, MacOS 13 sends a window focus change notification after the main window has been destroyed but before qApp has been fully destroyed.

Since the menu bar is deleted in the main window's destructor, it no longer exists when it receives these notifications (in two different places via lambda expressions). The solution is to pass the main window (this) as context when subscribing to the notifications. In this overloaded version of connect, Qt automatically unsubscribes to notifications if the sender OR context (here the main window object) is destroyed. Since the spurious notifications are sent after the main window object is destroyed, this change prevents them from being sent.

Tested on Mac OS 13 and 12 only.

MacOS 13 sends a window focus change notification after the main
window has been destroyed but before the QTApplication has been
destroyed. This results in the menu bar receiving a notification
despite it no longer existing. The solution is to pass the main
window as context when subscribing to the notifications. Qt
automatically unsubscribes to notifications if the sender OR
context is destroyed.
@jarolrod jarolrod added Bug Something isn't working macOS labels Nov 15, 2022
@jarolrod
Copy link
Member

I have confirmed that the issue is isolated to macOS 13 and that this PR fixes the issue.

Normally we don't reference an issue in the commit title, and I believe this can lead to some issues and unwanted notifications. A better commit title would be: qt: pass main window as context when subscribing to notifications. Then in the commit description you can add at the bottom of what you've already written: closes bitcoin#26490

@hebasto
Copy link
Member

hebasto commented Nov 17, 2022

Concept ACK.

@DrahtBot
Copy link
Contributor

DrahtBot commented Nov 17, 2022

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

Reviews

See the guideline for information on the review process.

Type Count Reviewers
ACK 1 hebasto

@hebasto
Copy link
Member

hebasto commented Nov 17, 2022

@john-moffett

Thank you for your such a nice first contribution!

Tested 8a5014c on macOS Ventura (M1). It fixes bitcoin/bitcoin#26490 indeed.

The solution is to pass the main window (this) as context when subscribing to the notifications. In this overloaded version of connect, Qt automatically unsubscribes to notifications if the sender OR context (here the main window object) is destroyed.

I believe you was going to point at https://doc.qt.io/qt-5/qobject.html#connect-5 :)

Indeed,

The connection will automatically disconnect if the sender or the context is destroyed.

Therefore, I think in our code we should avoid connections without specifying a context explicitly. For example:

connect(qApp, &QApplication::focusWindowChanged, wallet_model, [this, wallet_model]() {
if (!QApplication::activeModalWidget()) {
removeAndDeleteWallet(wallet_model);
}

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 8a5014c

@hebasto hebasto merged commit fb01af6 into bitcoin-core:master Nov 17, 2022
hebasto pushed a commit to hebasto/bitcoin that referenced this pull request Nov 17, 2022
MacOS 13 sends a window focus change notification after the main
window has been destroyed but before the QTApplication has been
destroyed. This results in the menu bar receiving a notification
despite it no longer existing. The solution is to pass the main
window as context when subscribing to the notifications. Qt
automatically unsubscribes to notifications if the sender OR
context is destroyed.

Github-Pull: bitcoin-core/gui#680
Rebased-From: 8a5014c
hebasto pushed a commit to hebasto/bitcoin that referenced this pull request Nov 17, 2022
MacOS 13 sends a window focus change notification after the main
window has been destroyed but before the QTApplication has been
destroyed. This results in the menu bar receiving a notification
despite it no longer existing. The solution is to pass the main
window as context when subscribing to the notifications. Qt
automatically unsubscribes to notifications if the sender OR
context is destroyed.

Github-Pull: bitcoin-core/gui#680
Rebased-From: 8a5014c
hebasto pushed a commit to hebasto/bitcoin that referenced this pull request Nov 17, 2022
MacOS 13 sends a window focus change notification after the main
window has been destroyed but before the QTApplication has been
destroyed. This results in the menu bar receiving a notification
despite it no longer existing. The solution is to pass the main
window as context when subscribing to the notifications. Qt
automatically unsubscribes to notifications if the sender OR
context is destroyed.

Github-Pull: bitcoin-core/gui#680
Rebased-From: 8a5014c
@hebasto
Copy link
Member

hebasto commented Nov 17, 2022

@john-moffett
Copy link
Contributor Author

Thank you, @hebasto ! I did indeed link to the wrong overload 😬. Thank you for the correction. I agree that context ought to be passed to connections that use lambda expressions. I'll take a look to see if there are any other potentially vulnerable places in the code.

fanquake added a commit to bitcoin/bitcoin that referenced this pull request Nov 18, 2022
39af5f2 Fixes #26490 by preventing notifications (John Moffett)

Pull request description:

  Backports:
  - bitcoin-core/gui#680

ACKs for top commit:
  jarolrod:
    ACK 39af5f2

Tree-SHA512: 1c84703395cc750611922aa2c59dc2e2f2e98c92845859f9f57e30ee5371c9a31690318230ab514f2d9b71e75a716a30280aa1e585b6da31e45fd970017bc74e
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Nov 18, 2022
…tain notifications after main window is destroyed

8a5014c Fixes bitcoin#26490 by preventing notifications (John Moffett)

Pull request description:

  This is a PR to address bitcoin#26490

  The menu bar currently subscribes to window focus change notifications to enable or disable certain menu options in response to the window status.

  Notifications are automatically unsubscribed (disconnected in Qt parlance) if the sender is deleted -- in this case, the sender is the QTApplication object (`qApp`). However, MacOS 13 sends a window focus change notification *after* the main window has been destroyed but *before* `qApp` has been fully destroyed.

  Since the menu bar is deleted in the main window's destructor, it no longer exists when it receives these notifications (in two different places via lambda expressions). The solution is to pass the main window (`this`) as context when subscribing to the notifications. In this [overloaded version](https://doc.qt.io/qt-5/qobject.html#connect-1) of `connect`, Qt automatically unsubscribes to notifications if the sender OR context (here the main window object) is destroyed. Since the spurious notifications are sent after the main window object is destroyed, this change prevents them from being sent.

  Tested on Mac OS 13 and 12 only.

ACKs for top commit:
  hebasto:
    ACK 8a5014c

Tree-SHA512: 3dff0a252fe0e93dd68cf5503135ecf6a72bcf385ba38407d6021ab77cca323f8bbe58aeca90ec124aa2a22ab9d35b706946179ac3b5d171c96a7010de51a090
fanquake added a commit to bitcoin/bitcoin that referenced this pull request Nov 21, 2022
e54a4de Fixes #26490 by preventing notifications (John Moffett)

Pull request description:

  Backports:
  - bitcoin-core/gui#680

ACKs for top commit:
  jarolrod:
    ACK e54a4de

Tree-SHA512: b81c73ece3f3c1e1d1c81bd0bb5b80a47488bca1fa43bbed25bab859cd063cd9b3acc1cff76f07961c3bd01276fab2fad8ea10b9d06e18965a198e78ff1f6705
hebasto pushed a commit to hebasto/bitcoin that referenced this pull request Nov 21, 2022
MacOS 13 sends a window focus change notification after the main
window has been destroyed but before the QTApplication has been
destroyed. This results in the menu bar receiving a notification
despite it no longer existing. The solution is to pass the main
window as context when subscribing to the notifications. Qt
automatically unsubscribes to notifications if the sender OR
context is destroyed.

Github-Pull: bitcoin-core/gui#680
Rebased-From: 8a5014c
fanquake added a commit to bitcoin/bitcoin that referenced this pull request Nov 22, 2022
272fa25 Fixes #26490 by preventing notifications (John Moffett)
7b7bbc1 Disallow encryption of watchonly wallets (Andrew Chow)

Pull request description:

  Backports:
  - bitcoin-core/gui#631
  - bitcoin-core/gui#680

ACKs for top commit:
  jarolrod:
    ACK 272fa25

Tree-SHA512: 4c285327464240ace3884d9653cc46d8e7b60b888f3b096ceb4fd5000d084ea8d97f1ef86ca1dea8dc7d3be8cdd2da19eece2b8c5b7351c5961b50b78fcd4c4d
fanquake added a commit to bitcoin/bitcoin that referenced this pull request Nov 23, 2022
d9bd628 doc: add release notes for 22.1rc2 (fanquake)
6523107 doc: Update manual pages for 22.1rc2 (fanquake)
6af7af6 build: Bump version to 22.1rc2 (fanquake)

Pull request description:

  Bump the version to 22.1rc2.
  Regenerate the man pages.
  Add WIP 22.1 release notes.

  Changes since rc1:
  - bitcoin-core/gui#631
  - bitcoin-core/gui#680

ACKs for top commit:
  stickies-v:
    ACK [d9bd628](d9bd628)
  jarolrod:
    ACK d9bd628

Tree-SHA512: 70b1723fd5f77a93763ffc153b18c5d6c11c8294828406bd5e93daf9e8aac5e62306280ef6601508b4d22e1cce5136687afc826be6d159816071549849c40f91
psgreco pushed a commit to psgreco/elements that referenced this pull request Dec 7, 2022
MacOS 13 sends a window focus change notification after the main
window has been destroyed but before the QTApplication has been
destroyed. This results in the menu bar receiving a notification
despite it no longer existing. The solution is to pass the main
window as context when subscribing to the notifications. Qt
automatically unsubscribes to notifications if the sender OR
context is destroyed.

Github-Pull: bitcoin-core/gui#680
Rebased-From: 8a5014c
(cherry picked from commit 272fa25)
psgreco pushed a commit to psgreco/elements that referenced this pull request Dec 7, 2022
MacOS 13 sends a window focus change notification after the main
window has been destroyed but before the QTApplication has been
destroyed. This results in the menu bar receiving a notification
despite it no longer existing. The solution is to pass the main
window as context when subscribing to the notifications. Qt
automatically unsubscribes to notifications if the sender OR
context is destroyed.

Github-Pull: bitcoin-core/gui#680
Rebased-From: 8a5014c
(cherry picked from commit 272fa25)
psgreco pushed a commit to psgreco/elements that referenced this pull request Dec 7, 2022
MacOS 13 sends a window focus change notification after the main
window has been destroyed but before the QTApplication has been
destroyed. This results in the menu bar receiving a notification
despite it no longer existing. The solution is to pass the main
window as context when subscribing to the notifications. Qt
automatically unsubscribes to notifications if the sender OR
context is destroyed.

Github-Pull: bitcoin-core/gui#680
Rebased-From: 8a5014c
(cherry picked from commit 272fa25)
psgreco pushed a commit to psgreco/elements that referenced this pull request Dec 7, 2022
MacOS 13 sends a window focus change notification after the main
window has been destroyed but before the QTApplication has been
destroyed. This results in the menu bar receiving a notification
despite it no longer existing. The solution is to pass the main
window as context when subscribing to the notifications. Qt
automatically unsubscribes to notifications if the sender OR
context is destroyed.

Github-Pull: bitcoin-core/gui#680
Rebased-From: 8a5014c
(cherry picked from commit 272fa25)
vertiond pushed a commit to vertiond/vertcoin-core that referenced this pull request Dec 10, 2022
MacOS 13 sends a window focus change notification after the main
window has been destroyed but before the QTApplication has been
destroyed. This results in the menu bar receiving a notification
despite it no longer existing. The solution is to pass the main
window as context when subscribing to the notifications. Qt
automatically unsubscribes to notifications if the sender OR
context is destroyed.

Github-Pull: bitcoin-core/gui#680
Rebased-From: 8a5014c
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jan 14, 2023
…tain notifications after main window is destroyed

8a5014c Fixes bitcoin#26490 by preventing notifications (John Moffett)

Pull request description:

  This is a PR to address bitcoin#26490

  The menu bar currently subscribes to window focus change notifications to enable or disable certain menu options in response to the window status.

  Notifications are automatically unsubscribed (disconnected in Qt parlance) if the sender is deleted -- in this case, the sender is the QTApplication object (`qApp`). However, MacOS 13 sends a window focus change notification *after* the main window has been destroyed but *before* `qApp` has been fully destroyed.

  Since the menu bar is deleted in the main window's destructor, it no longer exists when it receives these notifications (in two different places via lambda expressions). The solution is to pass the main window (`this`) as context when subscribing to the notifications. In this [overloaded version](https://doc.qt.io/qt-5/qobject.html#connect-1) of `connect`, Qt automatically unsubscribes to notifications if the sender OR context (here the main window object) is destroyed. Since the spurious notifications are sent after the main window object is destroyed, this change prevents them from being sent.

  Tested on Mac OS 13 and 12 only.

ACKs for top commit:
  hebasto:
    ACK 8a5014c

Tree-SHA512: 3dff0a252fe0e93dd68cf5503135ecf6a72bcf385ba38407d6021ab77cca323f8bbe58aeca90ec124aa2a22ab9d35b706946179ac3b5d171c96a7010de51a090
UdjinM6 pushed a commit to PastaPastaPasta/dash that referenced this pull request Jan 22, 2023
…tain notifications after main window is destroyed

8a5014c Fixes bitcoin#26490 by preventing notifications (John Moffett)

Pull request description:

  This is a PR to address bitcoin#26490

  The menu bar currently subscribes to window focus change notifications to enable or disable certain menu options in response to the window status.

  Notifications are automatically unsubscribed (disconnected in Qt parlance) if the sender is deleted -- in this case, the sender is the QTApplication object (`qApp`). However, MacOS 13 sends a window focus change notification *after* the main window has been destroyed but *before* `qApp` has been fully destroyed.

  Since the menu bar is deleted in the main window's destructor, it no longer exists when it receives these notifications (in two different places via lambda expressions). The solution is to pass the main window (`this`) as context when subscribing to the notifications. In this [overloaded version](https://doc.qt.io/qt-5/qobject.html#connect-1) of `connect`, Qt automatically unsubscribes to notifications if the sender OR context (here the main window object) is destroyed. Since the spurious notifications are sent after the main window object is destroyed, this change prevents them from being sent.

  Tested on Mac OS 13 and 12 only.

ACKs for top commit:
  hebasto:
    ACK 8a5014c

Tree-SHA512: 3dff0a252fe0e93dd68cf5503135ecf6a72bcf385ba38407d6021ab77cca323f8bbe58aeca90ec124aa2a22ab9d35b706946179ac3b5d171c96a7010de51a090
@bitcoin-core bitcoin-core locked and limited conversation to collaborators Nov 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug Something isn't working macOS
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants