Skip to content

Commit f4da930

Browse files
Better naming for exception filters
1 parent f953a5b commit f4da930

17 files changed

+88
-82
lines changed

src/core/lwt.ml

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -711,17 +711,19 @@ end
711711
open Basic_helpers
712712

713713
(* Small helpers to avoid catching ocaml-runtime exceptions *)
714-
type exception_filter = exn -> bool
715-
let catch_filter__all = fun _ -> true
716-
let catch_filter__all_except_runtime = function
717-
| Out_of_memory -> false
718-
| Stack_overflow -> false
719-
| _ -> true
720-
let exception_filter =
721-
(* Default value: the legacy behaviour to avoid breaking programs *)
722-
ref catch_filter__all
723-
let set_exception_filter f = exception_filter := f
724-
let filter_exception e = !exception_filter e
714+
module Exception_filter = struct
715+
type t = exn -> bool
716+
let handle_all = fun _ -> true
717+
let handle_all_except_runtime = function
718+
| Out_of_memory -> false
719+
| Stack_overflow -> false
720+
| _ -> true
721+
let v =
722+
(* Default value: the legacy behaviour to avoid breaking programs *)
723+
ref handle_all
724+
let set f = v := f
725+
let run e = !v e
726+
end
725727

726728
module Sequence_associated_storage :
727729
sig
@@ -802,7 +804,7 @@ struct
802804
let result = f () in
803805
current_storage := saved_storage;
804806
result
805-
with exn when !exception_filter exn ->
807+
with exn when Exception_filter.run exn ->
806808
current_storage := saved_storage;
807809
raise exn
808810
end
@@ -1140,7 +1142,7 @@ struct
11401142
be reject later, it is not the responsibility of this function to pass
11411143
the exception to [!async_exception_hook]. *)
11421144
try f v
1143-
with exn when !exception_filter exn ->
1145+
with exn when Exception_filter.run exn ->
11441146
!async_exception_hook exn
11451147

11461148

@@ -1840,7 +1842,7 @@ struct
18401842

18411843
let p' =
18421844
try f v with exn
1843-
when !exception_filter exn -> fail exn
1845+
when Exception_filter.run exn -> fail exn
18441846
in
18451847
let Internal p' = to_internal_promise p' in
18461848
(* Run the user's function [f]. *)
@@ -1906,7 +1908,7 @@ struct
19061908

19071909
let p' =
19081910
try f v
1909-
with exn when !exception_filter exn ->
1911+
with exn when Exception_filter.run exn ->
19101912
fail (add_loc exn) in
19111913
let Internal p' = to_internal_promise p' in
19121914

@@ -1963,7 +1965,7 @@ struct
19631965

19641966
let p''_result =
19651967
try Fulfilled (f v) with exn
1966-
when !exception_filter exn -> Rejected exn
1968+
when Exception_filter.run exn -> Rejected exn
19671969
in
19681970

19691971
let State_may_now_be_pending_proxy p'' = may_now_be_proxy p'' in
@@ -1993,7 +1995,7 @@ struct
19931995
to_public_promise
19941996
{state =
19951997
try Fulfilled (f v)
1996-
with exn when !exception_filter exn -> Rejected exn})
1998+
with exn when Exception_filter.run exn -> Rejected exn})
19971999
~if_deferred:(fun () ->
19982000
let (p'', callback) =
19992001
create_result_promise_and_callback_if_deferred () in
@@ -2012,7 +2014,7 @@ struct
20122014
let catch f h =
20132015
let p =
20142016
try f ()
2015-
with exn when !exception_filter exn -> fail exn
2017+
with exn when Exception_filter.run exn -> fail exn
20162018
in
20172019
let Internal p = to_internal_promise p in
20182020
let p = underlying p in
@@ -2037,7 +2039,7 @@ struct
20372039

20382040
let p' =
20392041
try h exn
2040-
with exn when !exception_filter exn -> fail exn
2042+
with exn when Exception_filter.run exn -> fail exn
20412043
in
20422044
let Internal p' = to_internal_promise p' in
20432045

@@ -2073,7 +2075,7 @@ struct
20732075
let backtrace_catch add_loc f h =
20742076
let p =
20752077
try f ()
2076-
with exn when !exception_filter exn -> fail exn
2078+
with exn when Exception_filter.run exn -> fail exn
20772079
in
20782080
let Internal p = to_internal_promise p in
20792081
let p = underlying p in
@@ -2098,7 +2100,7 @@ struct
20982100

20992101
let p' =
21002102
try h exn
2101-
with exn when !exception_filter exn ->
2103+
with exn when Exception_filter.run exn ->
21022104
fail (add_loc exn)
21032105
in
21042106
let Internal p' = to_internal_promise p' in
@@ -2135,7 +2137,7 @@ struct
21352137
let try_bind f f' h =
21362138
let p =
21372139
try f ()
2138-
with exn when !exception_filter exn -> fail exn
2140+
with exn when Exception_filter.run exn -> fail exn
21392141
in
21402142
let Internal p = to_internal_promise p in
21412143
let p = underlying p in
@@ -2152,7 +2154,7 @@ struct
21522154

21532155
let p' =
21542156
try f' v
2155-
with exn when !exception_filter exn -> fail exn
2157+
with exn when Exception_filter.run exn -> fail exn
21562158
in
21572159
let Internal p' = to_internal_promise p' in
21582160

@@ -2168,7 +2170,7 @@ struct
21682170

21692171
let p' =
21702172
try h exn
2171-
with exn when !exception_filter exn -> fail exn
2173+
with exn when Exception_filter.run exn -> fail exn
21722174
in
21732175
let Internal p' = to_internal_promise p' in
21742176

@@ -2210,7 +2212,7 @@ struct
22102212
let backtrace_try_bind add_loc f f' h =
22112213
let p =
22122214
try f ()
2213-
with exn when !exception_filter exn -> fail exn
2215+
with exn when Exception_filter.run exn -> fail exn
22142216
in
22152217
let Internal p = to_internal_promise p in
22162218
let p = underlying p in
@@ -2227,7 +2229,7 @@ struct
22272229

22282230
let p' =
22292231
try f' v
2230-
with exn when !exception_filter exn ->
2232+
with exn when Exception_filter.run exn ->
22312233
fail (add_loc exn)
22322234
in
22332235
let Internal p' = to_internal_promise p' in
@@ -2244,7 +2246,7 @@ struct
22442246

22452247
let p' =
22462248
try h exn
2247-
with exn when !exception_filter exn ->
2249+
with exn when Exception_filter.run exn ->
22482250
fail (add_loc exn)
22492251
in
22502252
let Internal p' = to_internal_promise p' in
@@ -2499,7 +2501,7 @@ struct
24992501
let dont_wait f h =
25002502
let p =
25012503
try f ()
2502-
with exn when !exception_filter exn -> fail exn
2504+
with exn when Exception_filter.run exn -> fail exn
25032505
in
25042506
let Internal p = to_internal_promise p in
25052507

@@ -2522,7 +2524,7 @@ struct
25222524
let async f =
25232525
let p =
25242526
try f ()
2525-
with exn when !exception_filter exn -> fail exn
2527+
with exn when Exception_filter.run exn -> fail exn
25262528
in
25272529
let Internal p = to_internal_promise p in
25282530

@@ -3125,39 +3127,39 @@ struct
31253127

31263128

31273129
let apply f x =
3128-
try f x with exn when !exception_filter exn -> fail exn
3130+
try f x with exn when Exception_filter.run exn -> fail exn
31293131

31303132
let wrap f =
31313133
try return (f ())
3132-
with exn when !exception_filter exn -> fail exn
3134+
with exn when Exception_filter.run exn -> fail exn
31333135

31343136
let wrap1 f x1 =
31353137
try return (f x1)
3136-
with exn when !exception_filter exn -> fail exn
3138+
with exn when Exception_filter.run exn -> fail exn
31373139

31383140
let wrap2 f x1 x2 =
31393141
try return (f x1 x2)
3140-
with exn when !exception_filter exn -> fail exn
3142+
with exn when Exception_filter.run exn -> fail exn
31413143

31423144
let wrap3 f x1 x2 x3 =
31433145
try return (f x1 x2 x3)
3144-
with exn when !exception_filter exn -> fail exn
3146+
with exn when Exception_filter.run exn -> fail exn
31453147

31463148
let wrap4 f x1 x2 x3 x4 =
31473149
try return (f x1 x2 x3 x4)
3148-
with exn when !exception_filter exn -> fail exn
3150+
with exn when Exception_filter.run exn -> fail exn
31493151

31503152
let wrap5 f x1 x2 x3 x4 x5 =
31513153
try return (f x1 x2 x3 x4 x5)
3152-
with exn when !exception_filter exn -> fail exn
3154+
with exn when Exception_filter.run exn -> fail exn
31533155

31543156
let wrap6 f x1 x2 x3 x4 x5 x6 =
31553157
try return (f x1 x2 x3 x4 x5 x6)
3156-
with exn when !exception_filter exn -> fail exn
3158+
with exn when Exception_filter.run exn -> fail exn
31573159

31583160
let wrap7 f x1 x2 x3 x4 x5 x6 x7 =
31593161
try return (f x1 x2 x3 x4 x5 x6 x7)
3160-
with exn when !exception_filter exn -> fail exn
3162+
with exn when Exception_filter.run exn -> fail exn
31613163

31623164

31633165

src/core/lwt.mli

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2011,24 +2011,30 @@ val ignore_result : _ t -> unit
20112011
The helpers below allow you to change the way that Lwt handles the two OCaml
20122012
runtime exceptions [Out_of_memory] and [Stack_overflow]. *)
20132013

2014-
(** An [exception_filter] is a value which indicates to Lwt what exceptions to
2015-
catch and what exceptions to let bubble up all the way out of the main loop
2016-
immediately. *)
2017-
type exception_filter
2014+
module Exception_filter: sig
20182015

2019-
(** [catch_filter__all] is the default filter. With it the all the exceptions
2020-
(including [Out_of_memory] and [Stack_overflow]) are caught and transformed
2021-
into rejected promises. *)
2022-
val catch_filter__all : exception_filter
2016+
(** An [Exception_filter.t] is a value which indicates to Lwt what exceptions to
2017+
catch and what exceptions to let bubble up all the way out of the main loop
2018+
immediately. *)
2019+
type t
20232020

2024-
(** [catch_filter__all_except_runtime] is a filter which lets the OCaml runtime
2025-
exceptions ([Out_of_memory] and [Stack_overflow]) go through all the Lwt
2026-
abstractions and bubble all the way out of the call to [Lwt_main.run]. *)
2027-
val catch_filter__all_except_runtime : exception_filter
2021+
(** [handle_all] is the default filter. With it the all the exceptions
2022+
(including [Out_of_memory] and [Stack_overflow]) can be handled: caught
2023+
and transformed into rejected promises. *)
2024+
val handle_all : t
20282025

2029-
(** [set_exception_filter] sets the given exception filter globally. *)
2030-
val set_exception_filter : exception_filter -> unit
2026+
(** [handle_all_except_runtime] is a filter which lets the OCaml runtime
2027+
exceptions ([Out_of_memory] and [Stack_overflow]) go through all the Lwt
2028+
abstractions and bubble all the way out of the call to [Lwt_main.run]. *)
2029+
val handle_all_except_runtime : t
20312030

2031+
(** [set] sets the given exception filter globally. *)
2032+
val set : t -> unit
2033+
2034+
(**/**)
2035+
val run : exn -> bool
2036+
2037+
end
20322038

20332039

20342040
(**/**)
@@ -2048,5 +2054,3 @@ val backtrace_try_bind :
20482054
val abandon_wakeups : unit -> unit
20492055

20502056
val debug_state_is : 'a state -> 'a t -> bool t
2051-
2052-
val filter_exception : exn -> bool

src/core/lwt_seq.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ let rec unfold f u () =
270270
match f u with
271271
| None -> return_nil
272272
| Some (x, u') -> Lwt.return (Cons (x, unfold f u'))
273-
| exception exc when Lwt.filter_exception exc -> Lwt.fail exc
273+
| exception exc when Lwt.Exception_filter.run exc -> Lwt.fail exc
274274

275275
let rec unfold_lwt f u () =
276276
let* x = f u in
@@ -305,7 +305,7 @@ let rec of_seq seq () =
305305
| Seq.Nil -> return_nil
306306
| Seq.Cons (x, next) ->
307307
Lwt.return (Cons (x, (of_seq next)))
308-
| exception exn when Lwt.filter_exception exn -> Lwt.fail exn
308+
| exception exn when Lwt.Exception_filter.run exn -> Lwt.fail exn
309309

310310
let rec of_seq_lwt (seq: 'a Lwt.t Seq.t): 'a t = fun () ->
311311
match seq () with
@@ -321,4 +321,4 @@ let of_seq_lwt (seq: 'a Lwt.t Seq.t): 'a t = fun () ->
321321
let+ x = x in
322322
let next = of_seq_lwt next in
323323
Cons (x, next)
324-
| exception exc when Lwt.filter_exception exc -> Lwt.fail exc
324+
| exception exc when Lwt.Exception_filter.run exc -> Lwt.fail exc

src/react/lwt_react.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ module E = struct
9898
Lwt_stream.iter
9999
(fun v ->
100100
try push v
101-
with exn when Lwt.filter_exception exn ->
101+
with exn when Lwt.Exception_filter.run exn ->
102102
!Lwt.async_exception_hook exn)
103103
stream in
104104
with_finaliser (cancel_thread t) event

src/unix/lwt_io.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ let make :
537537
perform_io,
538538
fun pos cmd ->
539539
try seek pos cmd
540-
with e when Lwt.filter_exception e -> Lwt.fail e
540+
with e when Lwt.Exception_filter.run e -> Lwt.fail e
541541
);
542542
} and wrapper = {
543543
state = Idle;

src/unix/lwt_main.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ let run p =
107107
| result ->
108108
finished ();
109109
result
110-
| exception exn when Lwt.filter_exception exn ->
110+
| exception exn when Lwt.Exception_filter.run exn ->
111111
finished ();
112112
raise exn
113113

src/unix/lwt_main.mli

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ let () = Lwt_main.run (main ())
4141
4242
In addition, note that if you have set the exception filter to let runtime
4343
exceptions bubble up (via
44-
[Lwt.set_exception_filter catch_filter__all_except_runtime]) then Lwt does
45-
not attempt to catch exceptions thrown by the OCaml runtime. Specifically,
46-
in this case, Lwt lets [Out_of_memory] and [Stack_overflow] exceptions
47-
traverse all of its functions and bubble up to the caller of
44+
[Lwt.Exception_filter.(set handle_all_except_runtime)])
45+
then Lwt does not attempt to catch exceptions thrown by the OCaml runtime.
46+
Specifically, in this case, Lwt lets [Out_of_memory] and [Stack_overflow]
47+
exceptions traverse all of its functions and bubble up to the caller of
4848
[Lwt_main.run]. Moreover because these exceptions are left to traverse the
4949
call stack, they leave the internal data-structures in an inconsistent
5050
state. For this reason, calling [Lwt_main.run] again after such an

src/unix/lwt_preemptive.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ let detach f args =
182182
let task () =
183183
try
184184
result := Result.Ok (f args)
185-
with exn when Lwt.filter_exception exn ->
185+
with exn when Lwt.Exception_filter.run exn ->
186186
result := Result.Error exn
187187
in
188188
get_worker () >>= fun worker ->

src/unix/lwt_timeout.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ let rec loop () =
7272
(*XXX Should probably report any exception *)
7373
try
7474
x.action ()
75-
with e when Lwt.filter_exception e ->
75+
with e when Lwt.Exception_filter.run e ->
7676
!handle_exn e
7777
done;
7878
curr := (!curr + 1) mod (Array.length !buckets);

0 commit comments

Comments
 (0)