Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. PyQt6 program randomly crashing without any error, falling into despair
QtWS25 Last Chance

PyQt6 program randomly crashing without any error, falling into despair

Scheduled Pinned Locked Moved Unsolved Language Bindings
12 Posts 3 Posters 1.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • JonBJ JonB

    @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?

    T Offline
    T Offline
    Tropaion
    wrote on last edited by
    #3

    @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)"
    jsulmJ 1 Reply Last reply
    0
    • T Offline
      T Offline
      Tropaion
      wrote on last edited by
      #4

      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)"
      JonBJ 1 Reply Last reply
      0
      • T Tropaion

        @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)"
        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #5

        @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.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • T Tropaion

          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)"
          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by
          #6

          @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 run python 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.

          1 Reply Last reply
          0
          • T Offline
            T Offline
            Tropaion
            wrote on last edited by Tropaion
            #7

            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:
            Screenshot 2024-11-12 103733.png

            1 Reply Last reply
            0
            • T Offline
              T Offline
              Tropaion
              wrote on last edited by
              #8

              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
              
              JonBJ jsulmJ 2 Replies Last reply
              0
              • T Tropaion

                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
                
                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by
                #9

                @Tropaion
                Does this indicate/suggest your problem lies in receiving data from some "measurement device", quite outside of anything Qt?

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  Tropaion
                  wrote on last edited by
                  #10

                  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"

                  1 Reply Last reply
                  0
                  • T Tropaion

                    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
                    
                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #11

                    @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?

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • T Offline
                      T Offline
                      Tropaion
                      wrote on last edited by
                      #12

                      @jsulm It's an empty line:
                      Screenshot 2024-11-12 130412.png

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved