Python signal.signal in a worker thread: "ValueError: signal only works in main thread of the main interpreter"
signal.signal(signal.SIGINT, handler) raises ValueError: signal only works in main thread of the main interpreter when called from any thread other than the main one. In a threading.Thread target the error is only printed by threading.excepthook and join() returns normally; inside a ThreadPoolExecutor task it is held silently until future.result(). Libraries that install signal handlers lazily during initialization (timeouts, graceful-shutdown helpers, some CLIs and test plugins) break this way when first used from a worker thread or a web server's request thread. Fix: install signal handlers in the main thread at startup and communicate with workers via threading.Event or queues; in asyncio code use loop.add_signal_handler from the main thread. Verified on Python 3.11.13, 3.12.12, 3.13.11 and 3.14.7 (GIL builds).