posixFileWriteChannel

fun posixFileWriteChannel(fd: Int, onWriteFailure: () -> Unit = {}): ByteWriteChannel

Creates a ByteWriteChannel that will write bytes to the specified file descriptor fd.

This is accomplished by creating a dedicated thread that blocks on reads before queueing to a ByteChannel for suspended reading, so only use when needed.

Parameters

onWriteFailure

invoked once if the dedicated writer thread aborts because the underlying posix write failed (e.g. the peer closed the read end of the fd). Cancelling the write ByteChannel only surfaces to new sends; a call that has already sent its request and is awaiting a response is parked on the connection, not on the write channel. If the read side stays open, that pending call would hang forever (#201, the K/N analog of #200). The caller uses this hook to force-close the connection so pending receivers complete exceptionally and callers fail fast instead of hanging. It is never invoked on a clean shutdown (the write channel being closed for read, e.g. via connection.close()).