fix: when reading from unix FD, use a loop to handle signals

This commit is contained in:
Simon Cruanes 2021-12-11 09:13:16 -05:00
parent f316e66c15
commit 6536bfeeb3
No known key found for this signature in database
GPG key ID: 4AC01D0849AA62B6

View file

@ -92,10 +92,20 @@ module Byte_stream = struct
{ bs_fill_buf=(fun () ->
if !i >= !len then (
i := 0;
let (to_read,_,_) = Unix.select [ic] [] [] timeout in
let rec wait() =
let to_read,_,_ = Unix.select [ic] [] [] timeout in
if to_read = [] then raise Timeout;
read()
and read() =
try len := Unix.read ic buf 0 (Bytes.length buf)
with Unix.Unix_error ((EAGAIN | EWOULDBLOCK), _, _) -> ();
with
| Unix.Unix_error (EAGAIN, _, _) -> read()
| Unix.Unix_error (EWOULDBLOCK, _, _) ->
(* FIXME: we should decrease the timeout by however long was spent in [select] *)
wait()
in
wait()
);
buf, !i,!len - !i);
bs_consume=(fun n -> i := !i + n);