juanmf / StepperMotors

Python stepper motor library with customizable acceleration strategies and responsive, interruptible motor operations.
https://github.com/juanmf/StepperMotors
MIT License
18 stars 0 forks source link

Logging reentrant lock attempt #16

Closed juanmf closed 6 months ago

juanmf commented 6 months ago

Upon exit on Ctrl-C, sometimes I get. this error occurs while handling an error. I might be triggering this by waiting too long to print buffers. Still a bug in logging lib.

Logging error (RuntimeError("reentrant call inside <_io.BufferedWriter name='<stderr>'>"), 
               'Traceback (most recent call last):\n  
                File "/usr/lib/python3.11/logging/__init__.py", line 1113, in emit\n    
                      stream.write(msg + self.terminator)\n
                RuntimeError: reentrant call inside <_io.BufferedWriter name=\'<stderr>\'>\n\n
                During handling of the above exception, another exception occurred:\n\n  
                Traceback (most recent call last):\n    
                    File "/home/juanmf/projects/turret/env/lib/python3.11/site-packages/stepper_motors_juanmf1/ThreadOrderedPrint.py", line 71, in flush_streams\n      
                        logging.info(out)\n    
                    File "/usr/lib/python3.11/logging/__init__.py", line 2148, in info\n      
                        root.info(msg, *args, **kwargs)\n    
                    File "/usr/lib/python3.11/logging/__init__.py", line 1489, in info\n      
                        self._log(INFO, msg, args, **kwargs)\n    
                    File "/usr/lib/python3.11/logging/__init__.py", line 1634, in _log\n      
                        self.handle(record)\n    
                    File "/usr/lib/python3.11/logging/__init__.py", line 1644, in handle\n      
                        self.callHandlers(record)\n    
                    File "/usr/lib/python3.11/logging/__init__.py", line 1706, in callHandlers\n      
                        hdlr.handle(record)\n    
                    File "/usr/lib/python3.11/logging/__init__.py", line 978, in handle\n      
                        self.emit(record)\n    
                    File "/usr/lib/python3.11/logging/__init__.py", line 1118, in emit\n      
                        self.handleError(record)\n    
                    File "/usr/lib/python3.11/logging/__init__.py", line 1031, in handleError\n      
                        sys.stderr.write(\'--- Logging error ---\\n\')\n    
                RuntimeError: reentrant call inside <_io.BufferedWriter name=\'<stderr>\'>\n')

the error handling code in logging.Handler.handleError() [python@3.11/3.11.6_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/logging/init.py]:

        if raiseExceptions and sys.stderr:  # see issue 13807
            t, v, tb = sys.exc_info()
            try:
                sys.stderr.write('--- Logging error ---\n')  # <<<< This one, in prior [ line 1031, in handleError\n   ]
                traceback.print_exception(t, v, tb, None, sys.stderr)
                sys.stderr.write('Call stack:\n')
    def handle(self, record):
        """
        Conditionally emit the specified logging record.

        Emission depends on filters which may have been added to the handler.
        Wrap the actual emission of the record with acquisition/release of
        the I/O thread lock. Returns whether the filter passed the record for
        emission.
        """
        rv = self.filter(record)
        if rv:
            self.acquire()  # this acquire that happens 1st might be interfering with the error handle fn. [, line 978, in handle\n      ]
            try:
                self.emit(record)
            finally:
                self.release()
        return rv
juanmf commented 6 months ago

Looks like this commit fixes it, reason was the print was attempted on app shitting down. https://stackoverflow.com/questions/75367828/runtimeerror-reentrant-call-inside-io-bufferedwriter-name-stdout