Skip to content

Commit

Permalink
Don't call accept_fork's error handler on cancellation
Browse files Browse the repository at this point in the history
This typically happens when the server is shutting down and wants to
stop all connection handlers. There is no need to have each connection
fiber log this; they just need to stop.
  • Loading branch information
talex5 committed May 18, 2023
1 parent c1e3aca commit 7c984b7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib_eio/net.ml
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ let accept_fork ~sw (t : #listening_socket) ~on_error handle =
Fiber.fork ~sw (fun () ->
match child_started := true; handle (flow :> stream_socket) addr with
| x -> Flow.close flow; x
| exception (Cancel.Cancelled _ as ex) ->
Flow.close flow;
raise ex
| exception ex ->
Flow.close flow;
on_error (Exn.add_context ex "handling connection from %a" Sockaddr.pp addr)
Expand Down
6 changes: 5 additions & 1 deletion lib_eio/net.mli
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,12 @@ val accept_fork :
@param on_error Called if [connection_handler] raises an exception.
This is typically a good place to log the error and continue.
If the exception is an {!Eio.Io} error then the caller's address is added to it.
If you don't want to handle connection errors,
use [~on_error:raise] to cancel the caller's context. *)
use [~on_error:raise] to cancel the caller's context.
[on_error] is not called for {!Cancel.Cancelled} exceptions,
which do not need to be reported. *)

(** {2 Running Servers} *)

Expand Down
17 changes: 17 additions & 0 deletions tests/network.md
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,23 @@ If the fork itself fails, we still close the connection:
Exception: Failure "Simulated error".
```

`accept_fork` doesn't send cancellations to `on_error`:

```ocaml
# Eio_mock.Backend.run @@ fun () ->
let socket = Eio_mock.Net.listening_socket "tcp/80" in
let flow = Eio_mock.Flow.make "connection" in
let addr = `Tcp (Eio.Net.Ipaddr.V4.loopback, 1234) in
Eio_mock.Net.on_accept socket [`Return (flow, addr)];
Switch.run @@ fun sw ->
Eio.Net.accept_fork ~sw ~on_error:(traceln "BUG: %a" Fmt.exn) socket
(fun _flow _addr -> Fiber.await_cancel ());
Switch.fail sw (Failure "Simulated error");;
+tcp/80: accepted connection from tcp:127.0.0.1:1234
+connection: closed
Exception: Failure "Simulated error".
```

## Socketpair

```ocaml
Expand Down

0 comments on commit 7c984b7

Please sign in to comment.