Is it possible to detect a closed connection without blocking or sending anything?

I’m not sure if this is Async specific, but I’m trying to re-use HTTP connections and I can’t figure out how to determine if the connection is alive without trying to send something.

The way Cohttp does this is by calling and checking for EOF, but I don’t want to actually want to block on reading (the server has finished sending the previous response at this point and is waiting for a new request). Reader.peek ~len:0 doesn’t seem to do anything, and Reader.peek ~len:1 would block.

My current strategy is to make a request and then retry if we see Cohttp’s remote connection closed exception but I’d like to do something cleaner if it’s possible.

Is it possible?

While you’re waiting for a real answer from someone who knows something about Reader etc. … If you can get a POSIX socket file descriptor value out of there, a close will make it readable, and you can detect a readable state with, with, if you like, a short timeout rather than blocking. This is obviously platform dependent, on UNIX or similar platform.

For normal use that would be sort of anathema, if Reader might be buffered, but in this case it sounds like your only concern is a potential blocking read, and select will fix that. Then your read for EOF will clear the readable flag until the next input or EOF arrives.