fpoling 8 days ago

Cancelling blocking IO is such a pain point in Go indeed. One can do it via careful Close() calls from another goroutine while ensuring that close is called only once. But Go provides absolutely no help for that in the standard library.

1
dfawcus 7 days ago

I had a need for that, on TCP and UDP connections. The former behind a net.Conn.

Rather than fiddle with Close() calls, from memory what I found worked was setting a short (or in the past) deadline on the Conn (SetReadDeadline). This as it is documented as applying even to an existing blocked call, and makes that blocked call eventually return an error.

So when I wanted to tear down the TCP connection and clean up the various goroutines associated with using the connection, I'd unblock any blocking i/o, then eventually one had an easy Close() call on the connection.

UDP was simpler, as each i/o operation had a short timeout. I've not investigated what could be done for pipe and fifo i/o.