Python's print() can crash when called from a reentrant signal handler
An investigation into Python signal handling shows that Python signal handlers, unlike C ones, run outside the strict low-level signal context but can still be re-entered mid-execution if another signal arrives. A test script that rapidly sends SIGUSR1 signals to itself while calling print() inside the handler triggers a RuntimeError, since Python's buffered stdout object detects the reentrant write and refuses to proceed.
GoKawiil's interpretation of the reporting above, not reported fact.
Developers often assume Python's more forgiving signal-handling model removes the classic C restrictions on what functions are safe to call inside a handler, but this shows that assumption doesn't fully hold for I/O operations like print(). Code that logs or prints inside a signal handler under high-frequency signal delivery can unexpectedly crash in production, making this a subtle reliability trap for systems programming in Python.
- Python delays user signal handler execution until the interpreter is in a safe state, easing many classic C-level restrictions.
- Python signal handlers are reentrant, meaning a new signal can interrupt a handler that's still running.
- Calling print() inside a signal handler can raise a RuntimeError if reentered while writing to the buffered stdout stream.
Fluent Python, 2nd Edition (O'Reilly) — If digging into CPython quirks like reentrant signal handlers and what's actually safe to call inside them is your idea of fun, Luciano Ramalho's Fluent Python is the natural next read. It goes deep on how the interpreter really behaves, including concurrency and the data model, so edge cases like this stop being surprises.
See Fluent Python, 2nd Edition (O'Reilly) on Amazon → Affiliate link — we may earn a commission on purchases, at no extra cost to you. Product picked by AI based on this article; it is not a tested recommendation.Source: iafisher.com — Ian Fisher, 2026-08-26
Published there as: “Is it safe to call print in a Python signal handler?”
Read the original report → The summary and analysis above are GoKawiil's own, written from reporting by the source above. Facts and quotes belong to the original publisher.