Is it safe to call print in a Python signal handler?

Hacker News Top News

Summary

The article explores the safety of calling print in Python signal handlers, concluding that while it may crash under extreme reentrant conditions, it's unlikely in practice and less severe than C signal handler issues.

No content available
Original Article
View Cached Full Text

Cached at: 08/30/26, 12:39 AM

# Is it safe to call `print` in a Python signal handler? Source: [https://iafisher.com/2026/08/sigprint](https://iafisher.com/2026/08/sigprint) [“You are clever, O Samana\.”](https://iafisher.com/quote/60e3da)- [Python has two signal handlers](https://iafisher.com/2026/08/sighandler) - [Blocking a signal is different than ignoring it](https://iafisher.com/2026/08/masks) - [Python doesn't pass`SA\_RESTART`](https://iafisher.com/2026/08/restart) - [Safe signal idioms](https://iafisher.com/2026/08/safe-signals) - [Python signal handlers are unexpectedly reentrant](https://iafisher.com/2026/08/reentrant) - **Is it safe to call`print`in a Python signal handler?** - [`time\.sleep`and signal interrupts](https://iafisher.com/2026/08/nanosleep) - [Python`siginterrupt`is confusing](https://iafisher.com/2026/08/siginterrupt) We learned earlier that because[Python has two signal handlers](https://iafisher.com/2026/08/sighandler), the[onerous restrictions](https://iafisher.com/man/signal-safety.7)on what functions a signal handler may call do not apply to Python, because CPython does not call the user\-supplied Python signal handler inside the low\-level C signal handler, where those restrictions*do*apply, but arranges for it to be called later, when the interpreter is in a consistent state\. We also learned that[Python signal handlers are unexpectedly reentrant](https://iafisher.com/2026/08/reentrant)– if a signal arrives while a Python signal handler is running, the signal handler can be called again in the middle of the first call\. What happens if a signal handler is reentered in the middle of a call to`print`? Let's stress\-test it by sending ourselves a rapid barrage of signals: ``` import os, signal, subprocess def sighandler(_signo, _frame): print("signal received") signal.signal(signal.SIGUSR1, sighandler) subprocess.run("for x in {1..50}; do kill -USR1 %s; done" % os.getpid(), shell=True) ``` Running this program on my machine produced: ``` File "multiple_signals.py", line 6, in sighandler print("signal received") File "multiple_signals.py", line 6, in sighandler print("signal received") File "multiple_signals.py", line 6, in sighandler print("signal received") [Previous line repeated 2 more times] RuntimeError: reentrant call inside <_io.BufferedWriter name='<stdout>'> ``` The test program shows that under extreme circumstances, calling`print`in a signal handler may cause your program to crash\. I want to emphasize that this requires*extreme circumstances*: it is unlikely that a real program would face these conditions, and even so, failing with an exception is more palatable than the possible consequences of unsafe signal handlers in C, which include deadlock, corrupted data structures, and silent failures\. So I view this as another bit of signals trivia and not a practical consideration for writing signal handlers – though I still advise against[doing non\-trivial work](https://iafisher.com/2026/08/safe-signals)in a signal handler\.

Similar Articles

Safe, or Simply Incapable? Rethinking Safety Evaluation for Phone-Use Agents

Hugging Face Daily Papers

The paper introduces PhoneSafety, a benchmark of 700 safety-critical moments across 130+ apps to evaluate phone-use agents. Results show that avoiding harmful outcomes does not necessarily indicate safety, as models may fail to act or make unsafe choices, requiring a distinction between capability and safety signals.

A New Design for Pretty Printer Implementations in Rust

Lobsters Hottest

A blog post exploring a new design for pretty printer implementations in Rust, addressing challenges of adapting functional programming research to a systems language without garbage collection, and comparing existing approaches like the 'pretty' crate and Oppen-style printers.