Skip to content

Commit 25ccb9f

Browse files
committed
Handle ValueError in signal registration on Python 3.14+
Catch ValueError raised when calling signal.signal from a non-main thread or non-main interpreter context (e.g. sub-interpreters on Python 3.14+). Falls back to synchronous execution without a timeout. Signed-off-by: Eshaan Agrawal <agrawaleshaan12@gmail.com>
1 parent 167dfe1 commit 25ccb9f

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

src/scancode/interrupt.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,23 @@ def handler(signum, frame):
9393
except TimeoutError:
9494
return TIMEOUT_MSG % locals(), NO_VALUE
9595

96+
except ValueError as ve:
97+
if 'signal only works in main thread' in str(ve):
98+
# Fallback to running without timeout if we are not in the main thread of the main interpreter
99+
try:
100+
return NO_ERROR, func(*(args or ()), **(kwargs or {}))
101+
except Exception:
102+
return ERROR_MSG + traceback_format_exc(), NO_VALUE
103+
return ERROR_MSG + traceback_format_exc(), NO_VALUE
104+
96105
except Exception:
97106
return ERROR_MSG + traceback_format_exc(), NO_VALUE
98107

99108
finally:
100-
setitimer(ITIMER_REAL, 0)
109+
try:
110+
setitimer(ITIMER_REAL, 0)
111+
except ValueError:
112+
pass
101113

102114
elif on_windows:
103115
"""

0 commit comments

Comments
 (0)