"not enough input" with Angstrom and Lwt on a TCP stream

I’m using an Angstrom decoder with Lwt.

Specifically, I have some code like this:

let%lwt input_channel, output_channel = Lwt_io.open_connection ... in
let%lwt unconsumed, r = Angstrom_lwt_unix.parse decoder input_channel in

Actually, the second line is repeated in a loop (so that many messages are decoded) as long as the channel is open.

Occasionally, decoding will fail with the message “not enough input”.

That’s strange to me, because I thought that if the connection was open, Lwt would block until input was available.

I ran the program under strace and noticed the following call to read corresponding to the “not enough input” error:

read(40, "", 4096)                      = 0

My understanding is that read will return zero on end-of-file. Here, I think that means the peer closed the connection.

If that’s correct, is there a way to distinguish between data being available but in the wrong format and the peer closing the connection?

I suppose I could do string matching on the error message returned by Angstrom but that seems flake-y.