# \[Dream\] w-chat example: Uncaught exception

**URL:** <https://discuss.ocaml.org/t/dream-w-chat-example-uncaught-exception/11339>\
**Category:** Ecosystem\
**Tags:** dream\
**Created:** [February 6, 2023, 1:48pm UTC](https://discuss.ocaml.org/t/dream-w-chat-example-uncaught-exception/11339 "2023-02-06T13:48:42Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Severin](https://avatars.discourse-cdn.com/v4/letter/s/eb8c5e/32.png) [@Severin](https://discuss.ocaml.org/u/Severin)\
**Post date:** [February 6, 2023, 1:48pm UTC](https://discuss.ocaml.org/t/dream-w-chat-example-uncaught-exception/11339/1 "2023-02-06T13:48:42Z")

</div>

I run [w-chat](https://github.com/aantron/dream/tree/master/example/w-chat) example from OCaml [Dream](https://aantron.github.io/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)`
