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

Refactor qt notification and its test solve problem of segfaults #5138

Merged
merged 5 commits into from
Sep 30, 2022

Conversation

Czaki
Copy link
Collaborator

@Czaki Czaki commented Sep 25, 2022

Description

During investigate why the qt notifications test fails with segfaults, I found that there exists Qdialog that is created without setting Parent that may cause segfaults (this is my assumption)

Type of change

  • Bug-fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

References

Try to fix segfaults showed in #5137

How has this been tested?

  • example: the test suite for my feature covers cases x, y, and z
  • example: all tests pass with my change
  • example: I check if my changes works with both PySide and PyQt backends
    as there are small differences between the two Qt bindings.

Final checklist:

  • My PR is the minimum possible work for the desired functionality
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • If I included new strings, I have used trans. to make them localizable.
    For more information see our translations guide.

@github-actions github-actions bot added qt Relates to qt tests Something related to our tests labels Sep 25, 2022
@codecov
Copy link

codecov bot commented Sep 25, 2022

Codecov Report

Merging #5138 (c75ca46) into main (8bb7e78) will increase coverage by 0.17%.
The diff coverage is 88.88%.

@@            Coverage Diff             @@
##             main    #5138      +/-   ##
==========================================
+ Coverage   87.62%   87.79%   +0.17%     
==========================================
  Files         587      578       -9     
  Lines       50051    49432     -619     
==========================================
- Hits        43855    43398     -457     
+ Misses       6196     6034     -162     
Impacted Files Coverage Δ
napari/_qt/dialogs/qt_notification.py 92.59% <86.95%> (+0.12%) ⬆️
napari/_qt/_tests/test_qt_notifications.py 95.72% <100.00%> (+0.03%) ⬆️
napari/_vispy/visuals/clipping_planes_mixin.py 88.88% <0.00%> (-5.56%) ⬇️
napari/plugins/_tests/test_utils.py 92.85% <0.00%> (-0.48%) ⬇️
napari/conftest.py
napari/_tests/test_key_bindings.py
napari/types.py
napari/__init__.py
napari/experimental/__init__.py
napari/settings/__init__.py
... and 10 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@Czaki
Copy link
Collaborator Author

Czaki commented Sep 25, 2022

failing is unrelated and connected with lack of remote resources required by the briefcase.

@Czaki Czaki requested a review from Carreau September 25, 2022 16:50
@github-actions
Copy link
Contributor

Click here to download the docs artifacts
docs
(zip file)

Copy link
Member

@andy-sweet andy-sweet left a comment

Choose a reason for hiding this comment

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

I don't understand this yet, but that doesn't mean it's not a good idea. If you can help clarify some of my questions and misunderstanding, can definitely switch to approval. Or maybe @Carreau has a better idea here.

@@ -436,3 +405,32 @@ def _debug_tb(tb):
print("Entering debugger. Type 'q' to return to napari.\n")
pdb.post_mortem(tb)
print("\nDebugging finished. Napari active again.")


class TracebackDialog(QDialog):
Copy link
Member

Choose a reason for hiding this comment

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

Is there any benefit to having the new dialog type TracebackDialog? It doesn't expose any new public attributes or methods and I think the behavior is the same (i.e. the code here is mostly the same, just swapping self with tbdialog.

I don't feel strongly here, but just wanted to check I understood the changes.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, is it so you can patch it specifically in the test?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

There are three reasons:
The Main is to change _enter_debug_mode into the method that is better for not leak memory.
The second one you mention: More specific patching.
And for last it looks better for me.

Copy link
Member

Choose a reason for hiding this comment

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

That works.

@@ -358,38 +358,7 @@ def from_notification(
if isinstance(notification, ErrorNotification):

def show_tb(parent_):
Copy link
Member

Choose a reason for hiding this comment

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

Not directly related to these changes, but should the typing of ActionSequence be updated that each action callable associated with a NapariQtNotification passes through the parent (i.e. a QWidget) as the only argument?

I bring this up because I was confused about how the parent actually gets passed through. Is my understanding correct? That is occurs when the action callable is executed? And the parent is the same as the parent of the original notification (likely mainwindow or some main widget in napari)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I have fixed ActionSequence definition. I'm not sure what the general idea for this mechanism was introduced by @Carreau.

My first idea is that it is introduced to manipulate NapariQtNotification but it is closed by the same event. So maybe it should just pass the parent object.

Copy link
Member

Choose a reason for hiding this comment

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

Might be a good idea to rename parent_? If I followed the existing logic properly here (and it's possible that I didn't!), then this will always be an instance of NapariQtNotification. In some sense that's the parent dialog of the dialog created here, but not in the sense of the QObject parent/child hierarchy.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

widget = QWidget()
qtbot.add_widget(widget)

dialog = NapariQtNotification.from_notification(notif, parent=widget)
Copy link
Member

Choose a reason for hiding this comment

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

Why does the dialog need a parent? From the QDialog docs, it seems fine to have a dialog as a top level widget without a parent. And then qtbot should handle closing it, which should also eventually free up memory.

Not saying you're wrong to do this, but I don't understand this change yet.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The NapariQtNotification dialog doe not need to have a set parent, as it could be handled by qtbot, but without this, I do not know how to handle TracebackDialog to be guarded by qtbot

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I get it, the Qt parent hierarchy is:

QWidget
    NapariQtNotification
    TracebackDialog

Previously, we just tracked the instance of NapariQtNotification, which didn't have a parent, but that means TracebackDialog wasn't tracked by qtbot. So parent here is representing some main widget or window in napari.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes

@github-actions
Copy link
Contributor

Click here to download the docs artifacts
docs
(zip file)

Copy link
Member

@andy-sweet andy-sweet left a comment

Choose a reason for hiding this comment

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

Looks good to me, but it's probably worth @Carreau taking a look too.

@github-actions
Copy link
Contributor

Click here to download the docs artifacts
docs
(zip file)

@github-actions
Copy link
Contributor

Click here to download the docs artifacts
docs
(zip file)

@andy-sweet andy-sweet merged commit 679331f into napari:main Sep 30, 2022
@Czaki Czaki deleted the refactor/qt_notifications_tests branch September 30, 2022 08:51
@Czaki Czaki added this to the 0.5.0 milestone Sep 30, 2022
@Czaki Czaki modified the milestones: 0.5.0, 0.4.18 Jun 16, 2023
@Czaki Czaki mentioned this pull request Jun 16, 2023
Czaki added a commit that referenced this pull request Jun 16, 2023
* refactor qt notification and its test to hopefully solve problem of segfaults

* update mocking

* fix ActionSequence definition

* update `show_tb` argument name

* add information about current qtbot guard structure to comment
Czaki added a commit that referenced this pull request Jun 17, 2023
* refactor qt notification and its test to hopefully solve problem of segfaults

* update mocking

* fix ActionSequence definition

* update `show_tb` argument name

* add information about current qtbot guard structure to comment
Czaki added a commit that referenced this pull request Jun 18, 2023
* refactor qt notification and its test to hopefully solve problem of segfaults

* update mocking

* fix ActionSequence definition

* update `show_tb` argument name

* add information about current qtbot guard structure to comment
Czaki added a commit that referenced this pull request Jun 19, 2023
* refactor qt notification and its test to hopefully solve problem of segfaults

* update mocking

* fix ActionSequence definition

* update `show_tb` argument name

* add information about current qtbot guard structure to comment
Czaki added a commit that referenced this pull request Jun 21, 2023
* refactor qt notification and its test to hopefully solve problem of segfaults

* update mocking

* fix ActionSequence definition

* update `show_tb` argument name

* add information about current qtbot guard structure to comment
Czaki added a commit that referenced this pull request Jun 21, 2023
* refactor qt notification and its test to hopefully solve problem of segfaults

* update mocking

* fix ActionSequence definition

* update `show_tb` argument name

* add information about current qtbot guard structure to comment
Czaki added a commit that referenced this pull request Jun 21, 2023
* refactor qt notification and its test to hopefully solve problem of segfaults

* update mocking

* fix ActionSequence definition

* update `show_tb` argument name

* add information about current qtbot guard structure to comment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
qt Relates to qt tests Something related to our tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants