# How to make a Python script crash without a dialog

#### strantor

Joined Oct 3, 2010
5,974
I have a python script running with a PyQt5 GUI on a machine with a real monitor and a dummy monitor. The dummy monitor is for me to use when I log in remotely with VNC server, so I can have a desktop on which do my thing, without interfering with what's displayed on the real monitor.

Every once in a while my script crashes and when it does, this error is displayed:

This gets displayed on the dummy monitor, not the real monitor. So whoever is looking at the real monitor doesn't realize that the GUI is frozen and that python has crashed. Neither do they have any way to acknowledge the prompt and restart the script. I would like Python to, if it absolutely must crash, just die quietly and get off the screen, rather than hold the machine ransom for an acknowledgement of its death.

is there any way to make it do that?

This script is started with a desktop shortcut to batch file, if that helps. Maybe is some batch command that I could add like:
hypothetical.bat:
venv\Scripts\Python.exe -justF*ingDIE mulitThreadedGUI.py

#### GetDeviceInfo

Joined Jun 7, 2009
2,101
How did that start. I ran openOPC and it had what appeared to be a memory leak, crashing every 3-4 days. I ran a scheduled batch to kill and start at midnight. Hokey, but it worked.

#### strantor

Joined Oct 3, 2010
5,974
How did that start. I ran openOPC and it had what appeared to be a memory leak, crashing every 3-4 days. I ran a scheduled batch to kill and start at midnight. Hokey, but it worked.
Not a memory leak issue in my case; It's totally random as far as I can tell. I run this script on a dozen identical machines and some of them will run 90+ days without fail, then crash twice in the same day, then fine for weeks or months again. PyQt is hard to troubleshoot because when it crashes there is no traceback, and try/except only works half the time. I am working on logging more info to see if there is any pattern in the events leading up to crashes, but it is going to be a needle in a haystack. It runs on touchscreen PanelPCs with no mouse or keyboard, so when it freezes up the only recourse is to cycle power. It would be a non-issue if it would just go back to the desktop if/when it crashes, so whoever is onsite can just tap the shortcut to re-start the script.

#### click_here

Joined Sep 22, 2020
544
I don't know if this will help because I don't program with Python, but when this sort of thing happens in C# it is because an unhandled exception has occured.

Here is the equivalent for Python - https://pythonbasics.org/try-except/

You can use "AppDomain.UnhandledException" if you want to be notified of any unhandled exception.

You can also find what caused the crash using "Event Viewer" - https://www.cyberlink.com/support/faq-content.do?id=10449

Hope this helps

#### strantor

Joined Oct 3, 2010
5,974
I don't know if this will help because I don't program with Python, but when this sort of thing happens in C# it is because an unhandled exception has occured.

Here is the equivalent for Python - https://pythonbasics.org/try-except/

You can use "AppDomain.UnhandledException" if you want to be notified of any unhandled exception.

You can also find what caused the crash using "Event Viewer" - https://www.cyberlink.com/support/faq-content.do?id=10449

Hope this helps
Thanks, the issue is indeed unhandled exceptions; not in my code, but in the libraries I use. Some of the libraries are wrappers for C type binaries that I can't access to add exception handling; in my experience I just have to feel around in the dark for the things that make them get surly. Python's try-except does not catch them and the information provided by Event Viewer has, in every instance I've encountered, pointed to python.exe - not very helpful. This is a rather niche problem I think, only routinely encountered when developing multithreaded applications using PyQt threadpool. I have been developing this project and dealing with these unhandled exceptions off-and-on for a couple of years now, and only in looking for a link that would briefly but adequately describe the issue for readers here in better words than my own, did I stumble onto some new information.

https://pythonspeed.com/articles/python-c-extension-crashes/

I am going to try the advice in the article but I would like, when it inevitably still crashes, for it to just crash and disappear. This is maddening. Why would windows even have this "feature" where the corpse of an application remains on display until someone clicks a decidedly uninformative pop-up? What's the point? Is there any windows setting that can disable this kind of feedback?

#### MrSalts

Joined Apr 2, 2020
1,845
create a document and keep replacing the document that contains the current Unix time stamp every 5 to 10 seconds.

1)
As you launch your Python script, check the process ID
PID = os.getpid()

2)
Create a temp file : named Python appended with your PID
Send the current Unix time stamp to this document. Update (replace) time stamp every 10-seconds in your main code (or a reasonable frequency).

3)
Spawn a new Python script in a new instance (pass the PID from above to the new script). The new script repeatedly checks whether the Unix time stamp in the text file is older than 15 seconds (slightly longer than update frequency). If the time stamp is aged, kill the process, restart the Unix script, end the current PID Unix time stamp monitor (because a new one is created with the launch). To avoid having two running, you can delay the relaunch of the main Python script with a Unix at-command. I believe windows has a similar command.