0

I'm developing PyQt5 application of medium complexity. It has various widgets (both native and third-party) and Qthreads. It woks fine most of the time, but once in about 5 times the application hangs in the background after I exit it.

Here is how I define closeEvent:

def closeEvent(self, event):
    qApp.quit()

And I also have defined a quit_action and connected it to the qApp.quit() like this:

self.quit_action.triggered.connect(qApp.quit)

I launch the PyQt5 application the standard way:

app = QApplication([])


window = GUIWindow()
window.show()

sys.exit(app.exec())

I have no idea how to debug such strange error. It happens only once in a few times and I can't reproduce the error.

I run the application in the console:

python main.py

and usually I can close the application without a problem. But once in a few times after I close the application (either by clicking on "x" button of the window or by triggering the quit_action in the menu) the window disappears and the process hangs in the background. I have to press "Ctlr+Z" and then kill the process.

What is the strategy to debug such strange error? How can find what exactly causes the freezing of the application?

  • You mentioned using threads: do you exit them before quitting? – musicamante Oct 31 '22 at 12:34
  • Yes. I properly exit them every time. I even have checked the number of running threads before exiting - it's always zero even in case when the application hangs. – Mihail Kondratyev Oct 31 '22 at 15:29
  • 1
    If you're using python `threading.active_count()` to count active QThreads, it won't work, since that function checks the number of python `Thread` objects, not system threads. Besides, the fact that the application hangs doesn't change the number of threads. Please provide a [mre] to show us how you're implementing the thread exiting, a basic thread implementation that reflects your case, and how you're calling it at application quit. – musicamante Oct 31 '22 at 15:57
  • Unfortunately I can't reproduce the error with minimal example, and I can't share the whole application. The question is about the strategy of debugging such cases. Is there a way to see what treads are still active after the main application closes? – Mihail Kondratyev Oct 31 '22 at 18:11
  • 1
    The problem is that we're not even sure *if* the issue is about threading. For what we know, it could be something else completely, even including those third party widgets you mentioned. There is no absolute strategy, because there could be dozens of completely unrelated reasons, so you only have the *basic* strategy: isolate the problem. Other than that, the question involves answers that would be too open and vague, making the post off topic. – musicamante Oct 31 '22 at 19:36

1 Answers1

0

This kind of bugs are hard to isolate. It looks like my particular bug emerges as a result of combination of several widgets of the app and it's hard to even provide a minimal working example that reproduces the error. Not to mention the error only happens once in a few runs.

It looks like I've found the main culprit for the bug by inspecting the hanging process through the gdb (see the question from stackoverflow Is there a way to attach a debugger to a multi-threaded Python process?).

When my application has frozen again - I attached to the process via the gdb, investigated threads and found the problematic thread.

EDIT: A temporary solution to make PyQt application close even when qApp.quit() doesn't help:

def closeEvent(self, event):
    qApp.quit()
    sys.exit()