Skip to main content

write_without_waiting

Function write_without_waiting 

Source
pub fn write_without_waiting(fd: i32, bytes: &[u8])
Expand description

Put bytes on fd once, without waiting for the other end and without letting the attempt raise a signal. A diagnostic that cannot be delivered that way is omitted: the exit status still says why the run ended.

THE DESCRIPTOR ITSELF IS NEVER MADE NON-BLOCKING. An inherited descriptor’s open file description is shared with the parent, the guest or a terminal, and fcntl(O_NONBLOCK) on it would leak into all of them. Each kind of file instead gets a primitive that is non-blocking on its own:

  • a regular file: pwritev2(RWF_NOWAIT) at offset -1, so the file position and O_APPEND are honoured. EAGAIN or EOPNOTSUPP omit the line. Buffered RWF_NOWAIT writes fail with EAGAIN on btrfs and with EOPNOTSUPP on tmpfs, so a log file there does not get the line.
  • a socket: send(MSG_DONTWAIT | MSG_NOSIGNAL).
  • a pipe, FIFO or terminal: a NEW open file description for the same object, opened through /proc/self/fd/<fd> with O_NONBLOCK, and one write of at most PIPE_BUF bytes, which a pipe either takes whole or refuses with EAGAIN. Opening a pipe that has no reader fails with ENXIO rather than raising SIGPIPE.
  • anything else, including a descriptor whose type cannot be learned: omitted.

The file type comes from file_type in this module.

No signal escapes; see suppressing_diagnostic_signals.

What this cannot avoid are short kernel locks that sleep uninterruptibly, which no handled signal or timer could break either: the file-position lock of an open file description another process is writing through at that moment, a terminal’s termios and output locks, a socket’s lock. None of them waits for a reader to drain anything.