[Dream] w-chat example: Uncaught exception

I run w-chat example from OCaml Dream framework on an Ubuntu 22.04.1 VPS.

At first, chat works as expected. Clients can send, receive, refresh, close tab, disconnect, or lose internet connection. However, if

  1. client A joins from a smartphone (Brave running on Android 7.0)
  2. puts his phone to sleep
  3. wait a few minutes (2 or 3)
  4. client B sends a message
    on server side, logger:

dream.http ERROR HTTP: Unix.Unix_error(Unix.ECONNRESET, “read”, “”)
dream.http ERROR Raised by primitive operation at Lwt_unix.shutdown in file “src/unix/lwt_unix.cppo.ml”, line 1672, characters 2-38
dream.http ERROR Called from Gluten_lwt_unix.Io.close.(fun) in file “src/vendor/gluten/lwt-unix/gluten_lwt_unix.ml”, line 49, characters 10-47
dream.http ERROR Called from Lwt.Sequential_composition.catch in file “src/core/lwt.ml”, line 2008, characters 16-20

  1. client B sends an other message
    Issue: chat not responsive anymore. Logger:

dream.log ERROR REQ 2 Async exception: Failure(“cannot write to closed writer”)
dream.log ERROR REQ 2 Raised at Dream__server__Helpers.websocket.(fun) in file “src/server/helpers.ml”, line 135, characters 8-17
dream.log ERROR REQ 2 Called from Lwt.Sequential_composition.try_bind.create_result_promise_and_callback_if_deferred.callback in file “src/core/lwt.ml”, line 2145, characters 23-28

Expected behaviour: from my understanding, at some point Dream.receive should evaluate to None for client A and thus be removed from the hash table.

Temporary solution: I just wrap Dream.send function:
let safely_send client_id s m = Lwt.catch (fun () -> Dream.send s m) (fun _ -> forget client_id; Dream.close_websocket s)

and modify send:
let send message = Hashtbl.to_seq clients |> List.of_seq |> Lwt_list.iter_p (fun (client_id,s) -> safely_send client_id s message)