diff --git a/lib/conduit_lwt_unix.ml b/lib/conduit_lwt_unix.ml index 40f11a09..283257d7 100644 --- a/lib/conduit_lwt_unix.ml +++ b/lib/conduit_lwt_unix.ml @@ -318,10 +318,13 @@ let serve_with_default_tls ?timeout ?stop ~ctx ~certfile ~keyfile ~pass ~port callback | No_tls -> failwith "No SSL or TLS support compiled into Conduit" -let serve ?backlog ?timeout ?stop ~(ctx:ctx) ~(mode:server) callback = - let callback flow ic oc = Lwt.catch +let serve ?backlog ?timeout ?stop + ?(on_exn=(fun exn -> !Lwt.async_exception_hook exn)) + ~(ctx:ctx) ~(mode:server) callback = + let callback flow ic oc = + Lwt.catch (fun () -> callback flow ic oc) - (fun exn -> !Lwt.async_exception_hook exn; Lwt.return_unit) + (fun exn -> on_exn exn; Lwt.return_unit) in match mode with | `TCP (`Port port) -> diff --git a/lib/conduit_lwt_unix.mli b/lib/conduit_lwt_unix.mli index be19b012..fe12e80c 100644 --- a/lib/conduit_lwt_unix.mli +++ b/lib/conduit_lwt_unix.mli @@ -156,18 +156,19 @@ val init : ?src:string -> ?tls_server_key:tls_server_key -> unit -> ctx io via the [ctx] context to the endpoint described by [client] *) val connect : ctx:ctx -> client -> (flow * ic * oc) io -(** [serve ?backlog ?timeout ?stop ~ctx ~mode fn] establishes a - listening connection of type [mode], using the [ctx] context. - The [stop] thread will terminate the server if it ever becomes - determined. Every connection will be served in a new - lightweight thread that is invoked via the [fn] callback. - The [fn] callback is passed the {!flow} representing the - client connection and the associated input {!ic} and output - {!oc} channels. If the callback raises an exception, it is - passed to [!Lwt.async_exception_hook]. *) +(** [serve ?backlog ?timeout ?stop ?on_exn ~ctx ~mode fn] + establishes a listening connection of type [mode], using the [ctx] + context. The [stop] thread will terminate the server if it ever + becomes determined. Every connection will be served in a new + lightweight thread that is invoked via the [fn] callback. The + [fn] callback is passed the {!flow} representing the client + connection and the associated input {!ic} and output {!oc} + channels. If the callback raises an exception, it is passed to + [on_exn] (by default, to !Lwt.async_exception_hook). *) val serve : - ?backlog:int -> ?timeout:int -> ?stop:(unit io) -> ctx:ctx -> - mode:server -> (flow -> ic -> oc -> unit io) -> unit io + ?backlog:int -> ?timeout:int -> ?stop:(unit io) -> + ?on_exn:(exn -> unit) -> ctx:ctx -> mode:server -> + (flow -> ic -> oc -> unit io) -> unit io (** [endp_of_flow flow] retrieves the original {!Conduit.endp} from the established [flow] *)