Non-Blocking file descriptors

This page is about a bug in Linux's handling of non-blocking files.

Current status

Circumstances under which the bug is visible

To see the bug, you need to do a blocking write to the inode. If this blocks, then that process will hold the atomic_write semaphore. While that write is still blocked, do a non-blocking write to the same inode. The result is that the non-blocking write blocks while trying to acquire the semaphore.

These conditions happen in practice when using the 'wall' program. If a program writing to a tty blocks due to the terminal being stopped, the wall process will hang on writing to that tty. Later, when the terminal is unblocked or hung up, the wall program continues and delivers the message late to other terminals.

Reproducing the bug

The following programs will demonstrate the bug. If your system has the bug, you will see the message "write blocked despite file descriptor being non-blocking!" If you see "test failed", the program was unable to complete the test and you should run the test again.

Note: The tty test should be compiled with -D_XOPEN_SOURCE to get the definition of grantpt.

Programs for demonstrating the bug
SourceBinary (libc6)
tty-testnonblock.c tty-testnonblock
pipe-testnonblock.c pipe-testnonblock

Patches

Results from running the tests, and patches against 2.2.19 and 2.4.14
Kernel TTY testPipe test TTY patchPipe patch
2.2 FailedFailed Patch Patch
2.4 FailedPassed Patch  

Valid HTML 4.01! Valid CSS!

Peter Benie <peterb@chiark.greenend.org.uk>
Linux