From e4f69cc1d6fba6e9fde8d6900c97c0b2ba5ba509 Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Fri, 9 Apr 2021 01:51:50 -0700 Subject: [PATCH] Maybe fix a deadlock on attempting sys.stderr.flush() Closes #758 Closes #733 --- src/ocrmypdf/leptonica.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/ocrmypdf/leptonica.py b/src/ocrmypdf/leptonica.py index c8759cfa..8ec02e0a 100644 --- a/src/ocrmypdf/leptonica.py +++ b/src/ocrmypdf/leptonica.py @@ -88,9 +88,9 @@ except ffi.error as e: class _LeptonicaErrorTrap_Redirect: """ - Context manager to trap errors reported by Leptonica. + Context manager to trap errors reported by Leptonica < 1.79 or on Apple Silicon. - Leptonica's error return codes don't provide much informatino about what + Leptonica's error return codes don't provide much information about what went wrong. Leptonica does, however, write more detailed errors to stderr (provided this is not disabled at compile time). The Leptonica source code is very consistent in its use of macros to generate errors. @@ -114,8 +114,10 @@ class _LeptonicaErrorTrap_Redirect: # Save the old stderr, and redirect stderr to temporary file self.leptonica_lock.acquire() try: - with suppress(AttributeError): - sys.stderr.flush() + # It would make sense to do sys.stderr.flush() here, but that can deadlock + # due to https://bugs.python.org/issue6721. So don't flush. Pretend + # there's nothing important sys.stderr. If the user cared they would + # be use Leptonica 1.79 or later anyway and avoid this mess. self.copy_of_stderr = os.dup(sys.stderr.fileno()) os.dup2(self.tmpfile.fileno(), sys.stderr.fileno(), inheritable=False) except AttributeError: