Home > other >  How to debug sporadic freezing of PyQt5 application after exiting?
How to debug sporadic freezing of PyQt5 application after exiting?

Time:11-03

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?

CodePudding user response:

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()
  • Related