mirage / ocaml-cohttp

An OCaml library for HTTP clients and servers using Lwt or Async
Other
703 stars 173 forks source link

Request timeout with Cohttp_lwt_unix.Client.get #790

Open vouillon opened 2 years ago

vouillon commented 2 years ago

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);
    ]
mseri commented 2 years ago

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