ocaml-cohttp icon indicating copy to clipboard operation
ocaml-cohttp copied to clipboard

Request timeout with Cohttp_lwt_unix.Client.get

Open vouillon opened this issue 4 years ago • 1 comments

The documentation explains how to set a timeout while waiting for a response from the server, but I don't see how one can set up a timeout while reading the response body without getting a file descriptor leak.

In particular, as far as I can see, with the following code, the file descriptor is not closed as long as the server does not close its side of the connection, which can well never happen if it stops responding.

  Lwt.pick
    [
      (let%lwt _, body =
         Cohttp_lwt_unix.Client.get
           (Uri.make ~scheme:"http" ~host:"localhost" ~port:8080 ())
       in
       let%lwt () = Cohttp_lwt.Body.drain_body body in
       Lwt.return_unit);
      (let%lwt () = Lwt_unix.sleep 6. in
       Lwt.return_unit);
    ]

vouillon avatar Nov 10 '21 17:11 vouillon

You are correct and I don’t have a good answer, I need to think about it

mseri avatar Nov 10 '21 22:11 mseri