package lwt-exit

  1. Overview
  2. Docs
An opinionated clean-exit and signal-handling library for Lwt programs

Install

Dune Dependency

Authors

Maintainers

Sources

lwt-exit-1.0.tar.gz
md5=f8f1c83d88d8955e055e0ccca11a3072
sha512=0ba5ef4495d402f5364af3f84e5378b04c9a1c5296a7f8bb84a0246a0bded1d5f4e3ab8e601056d7f96580eec78179d117fbd56670058460d0ae2e0990883ac2

Description

Published: 07 Dec 2020

README

Lwt-exit

Lwt-exit is a somewhat opinionated library for handling signals and clean exit in programs that use Lwt for concurrency.

Installation

Lwt-exit is available through opam:

opam install lwt-exit

Use

There are two sides to Lwt-exit: the cleaning up of resources when the program exits and the handling of signals whilst the program is running. These two sides are somewhat indepenedet, but note that signal handling can trigger an exit which triggers the cleaning up of resources.

You can set up resource clean-up as follows:

let output_socket = .. (* allocate resource *) in
let _ =
   Lwt_exit.register_clean_up_callback
      ~loc:__LOC__ (* used if needed for error reporting *)
      (fun exit_code ->
         .. (* clean-up resource *)
      )
in

You can trigger an exit from within your own program. This is similar to using Stdlib.exit but it will ensure the clean-up callbacks are executed.

match message with
| `Payload m -> process m
| `Stop -> Lwt_exit.exit_and_raise 0 (* exit-code 0 *)
| `Abort -> Lwt_exit.exit_and_raise 1 (* exit-code 1 *)

In addition, you can set up signal handlers which may trigger exits on their own.

let signal_setup = make_signal_setup ~soft:[..] ~hard:[]

Finally, you must wrap the main promise of your program:

let () = Lwt_main.run @@ Lwt_exit.wrap_and_exit ~signal_setup @@ ..

Check the documentation inline in the mli (src/lwt_exit.mli) or online for more information, advanced uses and additional examples.

Historical notes

The previous versions of Lwt-exit were developped as part of the Tezos project and is now used as an external depenedency.

OCaml

Innovation. Community. Security.