Skip to content

Commit 4fa528d

Browse files
committed
Fix signal-based interrupt on non-main threads
Add a check to detect if the current thread is the main thread before attempting to set up signal handlers. On Python 3.14+, signal handlers can only be set up in the main thread of the main interpreter. If not in the main thread, fall back to non-interruptible execution. This fixes the test failure in test_interruptible_can_run_function which was failing with: ValueError: signal only works in main thread of the main interpreter
1 parent 937be2a commit 4fa528d

1 file changed

Lines changed: 6 additions & 0 deletions

File tree

src/scancode/interrupt.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
traceback and `value` is None.
4040
"""
4141

42+
import threading
43+
4244

4345
class TimeoutError(Exception): # NOQA
4446
pass
@@ -80,7 +82,11 @@ class TimeoutError(Exception): # NOQA
8082
def interruptible(func, args=None, kwargs=None, timeout=DEFAULT_TIMEOUT):
8183
"""
8284
POSIX, signals-based interruptible runner.
85+
Falls back to non-interruptible execution if not in main thread.
8386
"""
87+
# Signals only work in the main thread
88+
if threading.current_thread() is not threading.main_thread():
89+
return NO_ERROR, func(*(args or ()), **(kwargs or {}))
8490

8591
def handler(signum, frame):
8692
raise TimeoutError

0 commit comments

Comments
 (0)