The documentation for Lwt.finalize states:
finalize f g returns the same result as f () whether it fails or not. In both cases, g () is executed after f.
Yet the following example demonstrates that this is not the case:
Lwt.finalize (fun() -> Lwt.fail (Failure "1")) (fun () -> Lwt.fail (Failure "2")) |> state;;
This gives a state with
Failure "2", not the
Failure "1" so the failure comes from the execution of g. Is it a bug in the documentation?
In any case, what is a good way to deal with a failed finalize when main promise also failed ? I can create own version of finalize that wraps both failures into a new exception and fail with that but it feels heavy.