# Why calling print in a Python signal handler can crash your program

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

> Hacker News story 49450248 · language: en · submitted 2026-08-26
> Web version: https://zeli.app/story/49450248
> About this feed: https://zeli.app/llms.txt · Agent guide: https://zeli.app/skill.md

▲ 64 · by [hellerve](<https://news.ycombinator.com/user?id=hellerve>) · 💬 35
[original](<https://iafisher.com/2026/08/sigprint>) · [HN discussion](<https://news.ycombinator.com/item?id=49450248>)

## Summary

Python's signal handling model defers user handlers until the interpreter is safe, but reentrancy can still bite: if a signal arrives mid-print, the handler may call print again, triggering a RuntimeError. This article stress-tests the scenario, explains why it happens, and argues it's a curiosity rather than a practical threat—though it still advises against heavy work in handlers.

## Key quote

> Running this program on my machine produced: RuntimeError: reentrant call inside <_io.BufferedWriter name='<stdout>'>

## HN discussion (35 comments)

### [nine_k](<https://news.ycombinator.com/item?id=49495509>)

> POSIX signals are broken by design, alas: https://lwn.net/Articles/414618/
>
> Python or not, almost nothing is safe inside a signal handler.

### [IgorPartola](<https://news.ycombinator.com/item?id=49494869>)

> What’s really fun is mixing signal handling and threads, especially on Linux. There is a simple way to do it and about a thousand ways that include at least one gotcha.

### [jrumbut](<https://news.ycombinator.com/item?id=49495668>)

> I think the author undersells the significance of this. I could easily picture someone writing code that boils down to the example, all it requires is a flood of signals and a print statement.
>
> If your program generates signals, it could generate a flood unintentionally. If you put a print statement in the handler, you can get this result.
>
> It's not terribly surprising, but good to know.

### [Uptrenda](<https://news.ycombinator.com/item?id=49495369>)

> And when you combine it with event loops and multiple OSes + python versions it gets even more difficult. Don't get me wrong: I love python, but shut down / cleanup is kind of a pain in the ass. If someone built a (good) lib for this it would probably be quite popular.

### [charcircuit](<https://news.ycombinator.com/item?id=49495627>)

> I don't understand how this is still a problem in 2026. Signals should just come in via a new thread and it would solve all the complexity around them. Everyone has known the current way it works is extremely limited in what you can safely do. This whole pause the execution of what's currently running and then run some extra code somewhere else turned out to not be a good idea.

---

## Cite

- Page: https://zeli.app/story/49450248
- Date: 2026-08-26
- Source: iafisher.com (https://iafisher.com/2026/08/sigprint)
- Hacker News: https://news.ycombinator.com/item?id=49450248

## Other languages

- zh: https://zeli.app/zh/story/49450248 · markdown: https://zeli.app/zh/story/49450248.md
- ja: https://zeli.app/ja/story/49450248 · markdown: https://zeli.app/ja/story/49450248.md
- ko: https://zeli.app/ko/story/49450248 · markdown: https://zeli.app/ko/story/49450248.md
- de: https://zeli.app/de/story/49450248 · markdown: https://zeli.app/de/story/49450248.md
- es: https://zeli.app/es/story/49450248 · markdown: https://zeli.app/es/story/49450248.md
- ru: https://zeli.app/ru/story/49450248 · markdown: https://zeli.app/ru/story/49450248.md
