Open kormax opened 8 months ago
Thinking about it, the r
variable should also be set to None at the end of related block to prevent the same thing happening to the result object.
Hi @hosaka ,
hope you are well.
Sorry for a direct mention, but you were the last person ever active in this repository.
May I ask you if it is possible to get this PR reviewed or approved and merged?
Regards,
Hello @kormax ,
I am a bit concerned about an effect this will have on QObjects, because the ownership/lifetime is managed internally in PySide at least (Shiboken). You do mention it as a "potential downside", I think this could be an issue for code that uses QObjects and Slots and most of the code using this library probably does use QObjects.
At least some tests should be performed to see if this might cause a double free.
After a bit of testing with QObjects and a codebase of mine that relies on qasync I can see no issues related to this change, including parent-child objects. Just like the comments in cpython issue state, the worker should not keep these references.
@kormax it would be nice to have a new test in test_qthreadexec.py
, akin to https://github.com/python/cpython/blob/main/Lib/test/test_concurrent_futures/executor.py#L80, is it something that you could add to this change?
NP. Gonna add this change a bit later . (I've tried an hour ago, but for some reason when running example through pytest, something weird happens to rc/gc and test results are inconclusive, while entirely stable in normal code).
When writing tests, I've noticed that when using
qasync.QThreadExecutor
, the objects, be it instance method owners, or something passed in as args, were not deleted immediately as the executor finished running the submitted task, even if there were no more references to those objects on the user side. The only way to release them was either to submit a new task, or shutdown an executor.While investigating the code, I've also taken a look into
concurrent.futures.ThreadPoolExecutor
, which didn't exhibit this behaviour.That's where I found the following code:
This PR mimics this change by adding a manual deletion of references to command tuple and its inner parameters inside of
_QThreadWorker.run
;The change can be tested by running the following example:
Before:
After:
As for potential downsides of this change: there could theoretically be code that accidentally relies on QThreadExecutor not releasing references, especially if QObject, Slots and other Qt-related stuff is involved. But even if it's a big issue, it still might be worth adding an option to enable proper behaviour via an environment variable or other parameter.