`qemu -serial stdio` leaves stdout in non-blocking mode
Host environment
- Operating system: Arch Linux
- OS/kernel version: Linux xxx 6.9.5-arch1-1 #1 SMP PREEMPT_DYNAMIC Sun, 16 Jun 2024 19:06:37 +0000 x86_64 GNU/Linux
- Architecture: x86_64
- QEMU flavor: qemu-system-x86_64
- QEMU version: 9.0.1
- QEMU command line:
qemu-system-x86_64 -nodefaults -display none -no-reboot -serial stdio
Emulated/Virtualized environment
- None / doesn't matter
Description of problem
When -serial stdio is used, qemu exits leaving stdout in non-blocking mode. Although it attempts to restore stdin to blocking mode, it misses that stdout also gets O_NONBLOCK by qemu_chr_open_fd (here). It causes the next applications in the script misbehave because they get unexpected EAGAIN on write to stdout.
Steps to reproduce
Run the following script:
#!/usr/bin/env bash
qemu-system-x86_64 -nodefaults -display none -no-reboot -serial stdio &
PID="$!"
sleep 5
kill "$PID"
wait "$PID"
echo "EXITING $?"
sleep 5
seq 1 400000
The seq command will be interrupted prematurely:
...
5143
5144
5145⏎ wResource temporarily unavailable
write: Resource temporarily unavailable
write: Resource temporarily unavailable
When run from fish shell, it will also start misbehaving when running next commands (fish bug report: https://github.com/fish-shell/fish-shell/issues/10600).
Additional information
Expect a patch from me soon.