-
Notifications
You must be signed in to change notification settings - Fork 106
Fix crashes during and after PythonQt cleanup #292
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
Merged
mrbean-bremen
merged 14 commits into
MeVisLab:master
from
jcfr:backport-commontk/fix-cleanup
Sep 4, 2025
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
10bbc3f
Prevent crashes during and after cleanup
msmolens c00824f
Add cleanup/finalization tests
msmolens 11590ea
Fix PythonQtSignalReceiver crash during cleanup
msmolens 6407969
Prevent crashes during and after cleanup (cont'd)
jamesobutler a8572b3
Prevent crash when an object is destroyed after calling PythonQt::cle…
msmolens 9bc4221
chore(PythonQtObjectPtr): Remove redundant nullptr checks
jcfr e778d6b
fix(cleanup): clear global enum-wrapper registry before tearing down …
jcfr caa1129
fix: guard InstanceWrapper during cleanup and Py_Finalize
jcfr a0221d6
tests: isolate cleanup tests
jcfr 83fb76d
fix: gate asyncio import during init; disable it in cleanup CI to avo…
jcfr fc5554e
fix: Ensure proper cleanup in PythonQtTestCleanup
jcfr f5391ab
tests(PythonQtTestCleanup): Fix unknown attribute in testCallQtMethod…
jcfr 8871ee9
ci(tests): suppress CPython 3.12 interned-unicode leak (PyUnicode_New…
jcfr 9e7c519
tests(cleanup): clarify destructor tests and add weakref-guarded variant
jcfr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| #include "PythonQtTestCleanup.h" | ||
| #include "PythonQt.h" | ||
| #include "PythonQt_QtAll.h" | ||
|
|
||
| void PythonQtTestCleanup::initTestCase() | ||
| { | ||
| } | ||
|
|
||
| void PythonQtTestCleanup::cleanupTestCase() | ||
| { | ||
| } | ||
|
|
||
| void PythonQtTestCleanup::init() | ||
| { | ||
| // Initialize before each test | ||
|
|
||
| PythonQt::init(PythonQt::IgnoreSiteModule); | ||
| PythonQt_QtAll::init(); | ||
|
|
||
| _helper = new PythonQtTestCleanupHelper(); | ||
| PythonQtObjectPtr main = PythonQt::self()->getMainModule(); | ||
| PythonQt::self()->addObject(main, "obj", _helper); | ||
| } | ||
|
|
||
| void PythonQtTestCleanup::cleanup() | ||
| { | ||
| // Cleanup PythonQt resources before finalizing Python | ||
| PythonQt::cleanup(); | ||
|
|
||
| if (Py_IsInitialized()) { | ||
| Py_Finalize(); | ||
| } | ||
|
|
||
| delete _helper; | ||
| _helper = nullptr; | ||
| } | ||
|
|
||
| void PythonQtTestCleanup::testQtEnum() | ||
| { | ||
| QVERIFY(_helper->runScript( | ||
| "import PythonQt.QtCore\n" \ | ||
| "x = PythonQt.QtCore.QFile.ReadOnly\n" \ | ||
| "obj.setPassed()" | ||
| )); | ||
| } | ||
|
|
||
| void PythonQtTestCleanup::testCallQtMethodInDestructorOwnedQTimer() | ||
| { | ||
| QVERIFY(_helper->runScript( | ||
| "import PythonQt.QtCore\n" \ | ||
| "class TimerWrapper(object):\n" \ | ||
| " def __init__(self):\n" \ | ||
| " self.timer = PythonQt.QtCore.QTimer()\n" \ | ||
| " def __del__(self):\n" \ | ||
| " self.timer.setSingleShot(True)\n" \ | ||
| "x = TimerWrapper()\n" \ | ||
| "del x\n" \ | ||
| "obj.setPassed()\n" | ||
| )); | ||
| } | ||
|
|
||
| void PythonQtTestCleanup::testCallQtMethodInDestructorWeakRefGuarded() | ||
| { | ||
| QVERIFY(_helper->runScript( | ||
| "import weakref\n" \ | ||
| "import PythonQt.QtCore\n" \ | ||
| "class TimerWrapper(object):\n" \ | ||
| " def __init__(self):\n" \ | ||
| " self.timerWeakRef = weakref.ref(PythonQt.QtCore.QTimer())\n" \ | ||
| " def __del__(self):\n" \ | ||
| " if self.timerWeakRef():\n" \ | ||
| " self.timerWeakRef().setSingleShot(True)\n" \ | ||
| "x = TimerWrapper()\n" \ | ||
| "obj.setPassed()\n" | ||
| )); | ||
| } | ||
|
|
||
| void PythonQtTestCleanup::testSignalReceiverCleanup() | ||
| { | ||
| PythonQtObjectPtr main = PythonQt::self()->getMainModule(); | ||
|
|
||
| // Test that PythonQtSignalReceiver is cleaned up properly, | ||
| // i.e. PythonQt::cleanup() doesn't segfault | ||
| main.evalScript( | ||
| "import PythonQt.QtCore\n" \ | ||
| "timer = PythonQt.QtCore.QTimer(obj)\n" \ | ||
| "timer.connect('destroyed()', obj.onDestroyed)\n" | ||
| ); | ||
| } | ||
|
|
||
| bool PythonQtTestCleanupHelper::runScript(const char* script) | ||
| { | ||
| _passed = false; | ||
| PyRun_SimpleString(script); | ||
| return _passed; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| #ifndef _PYTHONQTTESTCLEANUP_H | ||
| #define _PYTHONQTTESTCLEANUP_H | ||
|
|
||
| #include <QtTest/QtTest> | ||
|
|
||
| class PythonQtTestCleanupHelper; | ||
|
|
||
| //! Test PythonQt cleanup and Python interpreter finalization | ||
| class PythonQtTestCleanup : public QObject | ||
| { | ||
| Q_OBJECT | ||
|
|
||
| private Q_SLOTS: | ||
| void initTestCase(); | ||
| void cleanupTestCase(); | ||
| void init(); | ||
| void cleanup(); | ||
|
|
||
| void testQtEnum(); | ||
| void testCallQtMethodInDestructorOwnedQTimer(); | ||
| void testCallQtMethodInDestructorWeakRefGuarded(); | ||
| void testSignalReceiverCleanup(); | ||
|
|
||
| private: | ||
| PythonQtTestCleanupHelper* _helper; | ||
| }; | ||
|
|
||
| //! Test helper class | ||
| class PythonQtTestCleanupHelper : public QObject | ||
| { | ||
| Q_OBJECT | ||
| public: | ||
| PythonQtTestCleanupHelper() : | ||
| _passed(false) { | ||
| }; | ||
|
|
||
| bool runScript(const char* script); | ||
|
|
||
| public Q_SLOTS: | ||
| void setPassed() { _passed = true; } | ||
| void onDestroyed(QObject *) { } | ||
|
|
||
| private: | ||
| bool _passed; | ||
| }; | ||
|
|
||
| #endif |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, so this is a known problem - thanks for investigating this!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, this has been fixed in Python 3.13 but no backport for 3.12 hence the need for the suppression.