PyQt6 program randomly crashing without any error, falling into despair
-
@Tropaion
First things first:main worker thread -> button handling, start/stop...
What does "button handling" mean? Assuming "main worker thread" means "a thread which is not the "main thread -> GUI" ", are you aware that no secondary thread is allowed to access any UI stuff, e.g. a button. So what exactly does the secondary thread do for "button handling"?
Second
The only output i got was from pycharm in debugging mode: "Process finished with exit code -1073741819 (0xC0000005)".
Can you run your Python program outside of PyCharm? E.g. from a command line
python yourprogram.py
? Does that crash? With same message? -
@JonB Thanks for your reply
@JonB said in PyQt6 program randomly crashing without any error, falling into despair:
What does "button handling" mean? Assuming "main worker thread" means "a thread which is not the "main thread -> GUI" ", are you aware that no secondary thread is allowed to access any UI stuff, e.g. a button. So what exactly does the secondary thread do for "button handling"?
Yes, I know that secondary threads are not allowed to access UI stuff.
The "main worker thread" has many slot functions which do long lasting stuff.
But everything is handled via signals. So a button click sends a signal from the GUI to the main worker.It basically looks like this:
# Function to initialize main worker thread def _init_main_worker(self) -> None: logging.debug("called") # Create worker object and thread self.main_worker = _main_worker.Worker(self.configuration) self.main_worker_thread = QThread() # Move worker to thread self.main_worker.moveToThread(self.main_worker_thread) # Stop handler self.frontend.main.close_event.connect(self.main_worker.stop) # ----- TESTER SELECTION DIALOG ----- self.main_worker.enable_tester_selection_button.connect(self.frontend.main.bttn_change_pruefer.setEnabled) self.main_worker.set_tester_selection_label.connect(self.frontend.main.label_selected_pruefer.setText) self.main_worker.start_tester_selection_dialog.connect(self.frontend.tester_selection_dialog.exec) self.frontend.tester_selection_dialog.selected_tester.connect(self.main_worker.set_tester) # ----- DUT SELECTION DIALOG ----- self.main_worker.enable_dut_selection_button.connect(self.frontend.main.bttn_change_dut.setEnabled) self.main_worker.periphery_status.connect(self.frontend.dut_selection_dialog.update_periphery_status) self.frontend.dut_selection_dialog.selected_dut_key.connect(self.main_worker.set_selected_dut) # ----- TESTED DEVICE DIALOG ----- self.main_worker.enable_tested_device_button.connect(self.frontend.main.bttn_tested_device.setEnabled)``` @JonB said in [PyQt6 program randomly crashing without any error, falling into despair](/post/814353): > Can you run your Python program outside of PyCharm? E.g. from a command line python yourprogram.py? Does that crash? With same message? I have tried to run it in many different ways. * embedded x64 python -> no error output -> crashes faster * installed x64 python in console -> no error output -> crashes a slower * installed x64 python in console with trace -> no error output * installed x64 python in console with pdb -> no error output * only pycharm did throw an error: "Process finished with exit code -1073741819 (0xC0000005)"
-
Sorry, rest of my answer is in the code section...and somehow I can't edit it because if akismet spam warning.
Here the second answer:
@JonB said in PyQt6 program randomly crashing without any error, falling into despair:Can you run your Python program outside of PyCharm? E.g. from a command line python yourprogram.py? Does that crash? With same message?
I have tried to run it in many different ways.
- embedded x64 python -> no error output -> crashes faster
- installed x64 python in console -> no error output -> crashes a slower
- installed x64 python in console with trace -> no error output
- installed x64 python in console with pdb -> no error output
- only pycharm did throw an error: "Process finished with exit code -1073741819 (0xC0000005)"
-
@Tropaion said in PyQt6 program randomly crashing without any error, falling into despair:
self.main_worker.enable_tester_selection_button
Is this button a UI button like QPushButton? If so what you are doing is not allowed as @JonB pointed out.
All UI stuff must be done in main thread. -
@Tropaion
If you can get it to crash outside of PyCharm, do you have a C++ or system debugger available to you? If so you might runpython yourprogram.py
inside that and let it crash. Although this is not ideal you would get a stack trace of where it is in the Python --- but not in your code --- which may give a clue as to where the problem lies. -
Hello @JonB, that would be a beginning.
Can you recommend me an C++/system debugger? Not sure which one I should install and is supporting python.
I heard MinGW works, but not sure about it on Windows. And I don't really know how to use it.I now tried this one I found: https://marketplace.visualstudio.com/items?itemName=benjamin-simmonds.pythoncpp-debug
It did stop because of an exception but I'm not sure what to do here now:
-
Now, I enabled a few debugging modes I found and added many many prints and got this output:
EMITTTING DICT EMITTED DICT <SDM> [reset_safety_circuit scope] overwrite: False <SDM> [start_backend scope] self._cryptokey: b'\xdb\xe3\x15\xef\xffL\xb0%s%\x1bC\xb9\xe4DH' <SDM> [start_backend scope] _worker_params: {'dut': '200101', 'mac_address': '986D35C0F889', 'serial_number': '2001012411120027', 'vendor_serial_number': '0000000000000000', 'cryptokey': b'\xdb\xe3\x15\xef\xffL\xb0%s%\x1bC\xb9\xe4DH', 'inital_test_index': 0} <SDM> [_get_meter_measurements scope] measurements: {} Windows fatal exception: access violation Thread 0x00002e1c (most recent call first): File "C:\Users\Pruefplatz\AppData\Local\Programs\Python\Python310\lib\site-packages\pymodbus\client\tcp.py", line 227 in recv File "C:\Users\Pruefplatz\AppData\Local\Programs\Python\Python310\lib\site-packages\pymodbus\transaction.py<SDM> [_get_meter_measurements scope] measurements[U_in]: 230.9 ", line 417 in _recv File "C:\Users\Pruefplatz\AppData\Local\Programs\Python\Python310\lib\site-packages\pymodbus\transaction.py", line 325 in _transact File "C:\Users\Pruefplatz\AppData\Local\Programs\Python\Python310\lib\site-packages\pymodbus\transaction.py", line 215 in execute File "C:\Users\Pruefplatz\AppData\Local\Programs\Python\Python310\lib\site-packages\pymodbus\client\base.py", line 236 in execute File "C:\Users\Pruefplatz\AppData\Local\Programs\Python\Python310\lib\site-packages\pymodbus\client\mixin.py<SDM> [_get_meter_measurements scope] measurements[U_out]: 0.0 ", line 92 in read_holding_registers File "C:\Users\Pruefplatz\AppData\Local\Programs\Python\Python310\lib\site-packages\\drivers\modbus.py", line 68 in read_register File "c:\Users\Pruefplatz\Documents\GitHub\teststation-V3\software\src\interface\_main_worker.py", line 1237 in _get_meter_measurements Current thread 0x000037d0 (most recent call first): File "c:\Users\Pruefplatz\Documents\GitHub\teststation-V3\software\src\main.py", line 57 in main File "c:\Users\Pruefplatz\Documents\GitHub\teststation-V3\software\src\main.py", line 61 in <module> <SDM> [_get_meter_measurements scope] measurements[I_in]: 0.02 <SDM> [_get_meter_measurements scope] measurements[P_in]: 0.0 <SDM> [_get_meter_measurements scope] measurements[P_in_9s]: 0.0 <SDM> [_get_meter_measurements scope] measurements[U_in_9s]: 0.0 <SDM> [_get_meter_measurements scope] measurements[I_in_9s]: 0.0 EMITTTING DICT EMITTED DICT <SDM> [_get_meter_measurements scope] measurements: {} <SDM> [_get_meter_measurements scope] measurements[U_in]: 230.8 <SDM> [_get_meter_measurements scope] measurements[U_out]: 0.0 <SDM> [_get_meter_measurements scope] measurements[I_in]: 0.02 <SDM> [_get_meter_measurements scope] measurements[P_in]: 0.0
-
I don't think so, this function only gets some measurements from an device via the internet.
This function is called every 500ms by an QSingleShotTimer, gets the measurements and writes them in an dict.
The dict is then emitted to all other threads.@pyqtSlot() def _get_meter_measurements(self): """! Function periodically reads measurements from meters and sends a signal.""" try: # Create empty dict measurements = {} # Read acthor values measurements["U_in"] = self._meters.read_register(0, 2, 2) if measurements["U_in"] is False: measurements["U_in"] = "??" measurements["U_out"] = self._meters.read_register(7022, 2, 4, True) if measurements["U_out"] is False: measurements["U_out"] = "??" measurements["I_in"] = self._meters.read_register(6, 2, 2) if measurements["I_in"] is False: measurements["I_in"] = "??" measurements["P_in"] = self._meters.read_register(12, 2, 2) if measurements["P_in"] is False: measurements["P_in"] = "??" # Read 9s values measurements["P_in_9s"] = self._meters.read_register(12, 2, 1) if measurements["P_in_9s"] is False: measurements["P_in_9s"] = "??" measurements["U_in_9s"] = self._meters.read_register(7022, 2, 3, True) if measurements["U_in_9s"] is False: measurements["U_in_9s"] = "??" measurements["I_in_9s"] = self._meters.read_register(6, 2, 1) if measurements["I_in_9s"] is False: measurements["I_in_9s"] = "??" # Measuring done, emit signal self.update_meter_measurements.emit(measurements) # Check if meter measurement should be called again if self._meter_measurement_timer: QTimer.singleShot(self._config["general_periphery"]["modbus"]["interval"], self._get_meter_measurements) except Exception as e: logging.exception(e)
Truthfully, the logs are not very helpfull.
In the image I posted previously for example it says: "(Windows) Attach: Unhandled exception at 0x00007FF919D03A20 (Qt6Qui.dll) in python.exe:
0xC0000005: Access Violation reading location 0x0000000000000171" -
@Tropaion said in PyQt6 program randomly crashing without any error, falling into despair:
line 1237 in _get_meter_measurements
Which line in _get_meter_measurements is that?