fixed som ocassionally not working due to threading issues

This commit is contained in:
Dillon DuPont
2025-04-13 16:22:46 -04:00
parent 7206a01d6a
commit dfcf45aa93

View File

@@ -17,17 +17,28 @@ class TimeoutException(Exception):
@contextmanager
def timeout(seconds: int):
def timeout_handler(signum, frame):
raise TimeoutException("OCR process timed out")
import threading
# Check if we're in the main thread
if threading.current_thread() is threading.main_thread():
def timeout_handler(signum, frame):
raise TimeoutException("OCR process timed out")
original_handler = signal.signal(signal.SIGALRM, timeout_handler)
signal.alarm(seconds)
original_handler = signal.signal(signal.SIGALRM, timeout_handler)
signal.alarm(seconds)
try:
yield
finally:
signal.alarm(0)
signal.signal(signal.SIGALRM, original_handler)
try:
yield
finally:
signal.alarm(0)
signal.signal(signal.SIGALRM, original_handler)
else:
# In a non-main thread, we can't use signal
logger.warning("Timeout function called from non-main thread; signal-based timeout disabled")
try:
yield
finally:
pass
class OCRProcessor: