Closing for trouble in the land of streams

Thursday, 8 Feb 2024

Recently I learned of a mistake I had been making without realizing it. Zygo:

Closing FD 2, without simultaneously opening something else in its place (like a new TTY, if you’re trying to shed a controlling TTY or drop the last references to your old namespace/chroot parent), is weird and occasionally dangerous.

It’s asking for trouble because libraries are chatty and they write noise to stderr and it’s nearly impossible to stop them. Libraries also like to make their own open file descriptors, and they tend not to notice when the thing they’ve opened/connected to and expect to exclusively control/have a private conversation with happens to also be stderr and full of noise from random library functions. Chaos follows when those patterns collide.

Jamie Zawinski:

XScreenSaver has had code in it since the beginning to re-open FDs 1 and 2 as /dev/null lest XOpenDisplay put the connection to the X server on FD 2 with hilarious results.

In other words, closing one of the standard streams might lead to flooding somewhere else. You must ensure drainage, even if only to the void.