diff --git a/src/scancode/interrupt.py b/src/scancode/interrupt.py index e26d090567..1a21306cce 100644 --- a/src/scancode/interrupt.py +++ b/src/scancode/interrupt.py @@ -86,9 +86,20 @@ def handler(signum, frame): raise TimeoutError try: - create_signal(SIGALRM, handler) - setitimer(ITIMER_REAL, timeout) - return NO_ERROR, func(*(args or ()), **(kwargs or {})) + create_signal(SIGALRM, handler) + setitimer(ITIMER_REAL, timeout) +except (ValueError, TypeError): + # Signals only work in the main thread. + # If we cannot install them, continue without interrupt support. + return NO_ERROR, func(*(args or ()), **(kwargs or {})) + + try: + return NO_ERROR, func(*(args or ()), **(kwargs or {})) + except TimeoutError: + return TIMEOUT_MSG % locals(), NO_VALUE + except Exception: + return ERROR_MSG + traceback_format_exc(), NO_VALUE + except TimeoutError: return TIMEOUT_MSG % locals(), NO_VALUE @@ -167,7 +178,10 @@ def async_raise(tid, exctype=Exception): from http://tomerfiliba.com/recipes/Thread2/ license: public domain. """ - assert isinstance(tid, int), 'Invalid thread id: must an integer' + if not isinstance(tid, int): + raise TypeError( + f"Invalid thread id: must be an integer, got {type(tid).__name__}" + ) tid = c_long(tid) exception = py_object(Exception)