Skip to content

Commit ae202d9

Browse files
raphael-proustsmorimoto
authored andcommitted
Use raise and reraise instead of fail in more places in core
1 parent 4a251f6 commit ae202d9

File tree

7 files changed

+56
-40
lines changed

7 files changed

+56
-40
lines changed

src/core/lwt_pool.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ let validate_and_return p c =
108108
resolver is waiting. *)
109109
dispose p c >>= fun () ->
110110
replace_disposed p;
111-
Lwt.fail e)
111+
Lwt.reraise e)
112112
113113
(* Acquire a pool member. *)
114114
let acquire p =

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.Exception_filter.run exc -> Lwt.fail exc
273+
| exception exc when Lwt.Exception_filter.run exc -> Lwt.reraise exc
274274

275275
let rec unfold_lwt f u () =
276276
let* x = f u in
@@ -299,7 +299,7 @@ let rec of_seq seq () =
299299
| Seq.Nil -> return_nil
300300
| Seq.Cons (x, next) ->
301301
Lwt.return (Cons (x, (of_seq next)))
302-
| exception exn when Lwt.Exception_filter.run exn -> Lwt.fail exn
302+
| exception exn when Lwt.Exception_filter.run exn -> Lwt.reraise exn
303303

304304
let rec of_seq_lwt (seq: 'a Lwt.t Seq.t): 'a t = fun () ->
305305
match seq () with
@@ -315,4 +315,4 @@ let of_seq_lwt (seq: 'a Lwt.t Seq.t): 'a t = fun () ->
315315
let+ x = x in
316316
let next = of_seq_lwt next in
317317
Cons (x, next)
318-
| exception exc when Lwt.Exception_filter.run exc -> Lwt.fail exc
318+
| exception exc when Lwt.Exception_filter.run exc -> Lwt.reraise exc

src/core/lwt_stream.ml

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -279,9 +279,9 @@ class ['a] bounded_push_impl (info : 'a push_bounded) wakener_cell last close =
279279
let waiter, wakener = Lwt.task () in
280280
info.pushb_push_waiter <- waiter;
281281
info.pushb_push_wakener <- wakener;
282-
Lwt.fail exn
282+
Lwt.reraise exn
283283
| _ ->
284-
Lwt.fail exn)
284+
Lwt.reraise exn)
285285
end else begin
286286
(* Push the element at the end of the queue. *)
287287
enqueue' (Some x) last;
@@ -367,11 +367,18 @@ let feed s =
367367
else begin
368368
(* Otherwise request a new element. *)
369369
let thread =
370-
from.from_create () >>= fun x ->
371-
(* Push the element to the end of the queue. *)
372-
enqueue x s;
373-
if x = None then Lwt.wakeup s.close ();
374-
Lwt.return_unit
370+
(* The function [from_create] can raise an exception (with
371+
[raise], rather than returning a failed promise with
372+
[Lwt.fail]). In this case, we have to catch the exception
373+
and turn it into a safe failed promise. *)
374+
Lwt.catch
375+
(fun () ->
376+
from.from_create () >>= fun x ->
377+
(* Push the element to the end of the queue. *)
378+
enqueue x s;
379+
if x = None then Lwt.wakeup s.close ();
380+
Lwt.return_unit)
381+
Lwt.reraise
375382
in
376383
(* Allow other threads to access this thread. *)
377384
from.from_thread <- thread;
@@ -1070,7 +1077,7 @@ let parse s f =
10701077
(fun () -> f s)
10711078
(fun exn ->
10721079
s.node <- node;
1073-
Lwt.fail exn)
1080+
Lwt.reraise exn)
10741081

10751082
let hexdump stream =
10761083
let buf = Buffer.create 80 and num = ref 0 in

test/core/test_lwt.ml

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,15 @@ let catch_tests = suite "catch" [
549549
state_is (Lwt.Return Exception) p
550550
end;
551551

552+
test "rejected (raise)" begin fun () ->
553+
let p =
554+
Lwt.catch
555+
(fun () -> raise Exception)
556+
(fun exn -> Lwt.return exn)
557+
in
558+
state_is (Lwt.Return Exception) p
559+
end;
560+
552561
(* This is an analog of the "bind quirk," see
553562

554563
https://github.com/ocsigen/lwt/issues/329 *)
@@ -696,7 +705,7 @@ let backtrace_catch_tests = suite "backtrace_catch" [
696705
test "rejected" begin fun () ->
697706
let p =
698707
Lwt.backtrace_catch add_loc
699-
(fun () -> Lwt.fail Exception)
708+
(fun () -> raise Exception)
700709
(fun exn -> Lwt.return exn)
701710
in
702711
state_is (Lwt.Return Exception) p
@@ -789,7 +798,7 @@ let try_bind_tests = suite "try_bind" [
789798
test "rejected" begin fun () ->
790799
let p =
791800
Lwt.try_bind
792-
(fun () -> Lwt.fail Exception)
801+
(fun () -> raise Exception)
793802
(fun _ -> Lwt.return Exit)
794803
(fun exn -> Lwt.return exn)
795804
in
@@ -810,7 +819,7 @@ let try_bind_tests = suite "try_bind" [
810819
test "rejected, h raises" begin fun () ->
811820
try
812821
ignore @@ Lwt.try_bind
813-
(fun () -> Lwt.fail Exit)
822+
(fun () -> raise Exit)
814823
(fun _ -> Lwt.return_unit)
815824
(fun _ -> raise Exception);
816825
Lwt.return_false
@@ -961,7 +970,7 @@ let backtrace_try_bind_tests = suite "backtrace_try_bind" [
961970
test "rejected" begin fun () ->
962971
let p =
963972
Lwt.backtrace_try_bind add_loc
964-
(fun () -> Lwt.fail Exception)
973+
(fun () -> raise Exception)
965974
(fun _ -> Lwt.return Exit)
966975
(fun exn -> Lwt.return exn)
967976
in
@@ -1132,7 +1141,7 @@ let finalize_tests = suite "finalize" [
11321141
test "rejected, f' raises" begin fun () ->
11331142
try
11341143
ignore @@ Lwt.finalize
1135-
(fun () -> Lwt.fail Exit)
1144+
(fun () -> raise Exit)
11361145
(fun () -> raise Exception);
11371146
Lwt.return_false
11381147
with Exception ->
@@ -1169,7 +1178,7 @@ let finalize_tests = suite "finalize" [
11691178
let p =
11701179
Lwt.finalize
11711180
(fun () -> p)
1172-
(fun () -> Lwt.fail Exception)
1181+
(fun () -> raise Exception)
11731182
in
11741183
Lwt.wakeup r ();
11751184
state_is (Lwt.Fail Exception) p
@@ -1232,7 +1241,7 @@ let finalize_tests = suite "finalize" [
12321241
let p =
12331242
Lwt.finalize
12341243
(fun () -> p)
1235-
(fun () -> Lwt.fail Exception)
1244+
(fun () -> raise Exception)
12361245
in
12371246
Lwt.wakeup_exn r Exit;
12381247
state_is (Lwt.Fail Exception) p
@@ -1347,7 +1356,7 @@ let backtrace_finalize_tests = suite "backtrace_finalize" [
13471356
let f'_ran = ref false in
13481357
let p =
13491358
Lwt.backtrace_finalize add_loc
1350-
(fun () -> Lwt.fail Exception)
1359+
(fun () -> raise Exception)
13511360
(fun () -> f'_ran := true; Lwt.return_unit)
13521361
in
13531362
Lwt.bind (state_is (Lwt.Fail Exception) p) (fun correct ->
@@ -1367,7 +1376,7 @@ let backtrace_finalize_tests = suite "backtrace_finalize" [
13671376
test "rejected, f' raises" begin fun () ->
13681377
try
13691378
ignore @@ Lwt.backtrace_finalize add_loc
1370-
(fun () -> Lwt.fail Exit)
1379+
(fun () -> raise Exit)
13711380
(fun () -> raise Exception);
13721381
Lwt.return_false
13731382
with Exception ->
@@ -1404,7 +1413,7 @@ let backtrace_finalize_tests = suite "backtrace_finalize" [
14041413
let p =
14051414
Lwt.backtrace_finalize add_loc
14061415
(fun () -> p)
1407-
(fun () -> Lwt.fail Exception)
1416+
(fun () -> raise Exception)
14081417
in
14091418
Lwt.wakeup r ();
14101419
state_is (Lwt.Fail Exception) p
@@ -1439,7 +1448,7 @@ let backtrace_finalize_tests = suite "backtrace_finalize" [
14391448
let p =
14401449
Lwt.backtrace_finalize add_loc
14411450
(fun () -> p)
1442-
(fun () -> Lwt.fail Exception)
1451+
(fun () -> raise Exception)
14431452
in
14441453
Lwt.wakeup_exn r Exit;
14451454
state_is (Lwt.Fail Exception) p
@@ -1803,7 +1812,7 @@ let async_tests = suite "async" [
18031812
let saw = ref None in
18041813
let restore =
18051814
set_async_exception_hook (fun exn -> saw := Some exn) in
1806-
Lwt.async (fun () -> Lwt.fail Exception);
1815+
Lwt.async (fun () -> raise Exception);
18071816
later (fun () ->
18081817
restore ();
18091818
!saw = Some Exception)
@@ -1852,7 +1861,7 @@ let dont_wait_tests = suite "dont_wait" [
18521861
test "rejected" begin fun () ->
18531862
let saw = ref None in
18541863
Lwt.dont_wait
1855-
(fun () -> Lwt.fail Exception)
1864+
(fun () -> raise Exception)
18561865
(fun exn -> saw := Some exn);
18571866
later (fun () -> !saw = Some Exception)
18581867
end;
@@ -3371,7 +3380,7 @@ let cancel_catch_tests = suite "cancel catch" [
33713380
test "task, pending, canceled, on_cancel, forwarded" begin fun () ->
33723381
let on_cancel_2_ran = ref false in
33733382
let p, _ = Lwt.task () in
3374-
let p' = Lwt.catch (fun () -> p) Lwt.fail in
3383+
let p' = Lwt.catch (fun () -> p) Lwt.reraise in
33753384
Lwt.on_cancel p' (fun () -> on_cancel_2_ran := true);
33763385
Lwt.cancel p';
33773386
Lwt.return
@@ -3895,7 +3904,7 @@ let storage_tests = suite "storage" [
38953904
Lwt.with_value key (Some 42) (fun () ->
38963905
let p' =
38973906
Lwt.with_value key (Some 1337) (fun () ->
3898-
Lwt.try_bind (fun () -> p) f Lwt.fail)
3907+
Lwt.try_bind (fun () -> p) f Lwt.reraise)
38993908
in
39003909
Lwt.wakeup r ();
39013910
Lwt.return

test/core/test_lwt_pool.ml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ let suite = suite "lwt_pool" [
1616
end;
1717

1818
test "creator exception" begin fun () ->
19-
let gen = fun () -> Lwt.fail Dummy_error in
19+
let gen = fun () -> raise Dummy_error in
2020
let p = Lwt_pool.create 1 gen in
2121
let u = Lwt_pool.use p (fun _ -> Lwt.return 0) in
2222
Lwt.return (Lwt.state u = Lwt.Fail Dummy_error)
@@ -42,7 +42,7 @@ let suite = suite "lwt_pool" [
4242
test "validation exceptions are propagated to users" begin fun () ->
4343
let c = Lwt_condition.create () in
4444
let gen = (fun () -> let l = ref 0 in Lwt.return l) in
45-
let v l = if !l = 0 then Lwt.return_true else Lwt.fail Dummy_error in
45+
let v l = if !l = 0 then Lwt.return_true else raise Dummy_error in
4646
let p = Lwt_pool.create 1 ~validate:v gen in
4747
let u1 = Lwt_pool.use p (fun l -> l := 1; Lwt_condition.wait c) in
4848
let u2 = Lwt_pool.use p (fun l -> Lwt.return !l) in
@@ -106,7 +106,7 @@ let suite = suite "lwt_pool" [
106106
test "waiter are notified on replacement" begin fun () ->
107107
let c = Lwt_condition.create () in
108108
let gen = (fun () -> let l = ref 0 in Lwt.return l) in
109-
let v l = if !l = 0 then Lwt.return_true else Lwt.fail Dummy_error in
109+
let v l = if !l = 0 then Lwt.return_true else raise Dummy_error in
110110
let p = Lwt_pool.create 1 ~validate:v gen in
111111
let u1 = Lwt_pool.use p (fun l -> l := 1; Lwt_condition.wait c) in
112112
let u2 = Lwt_pool.use p (fun l -> Lwt.return !l) in
@@ -128,9 +128,9 @@ let suite = suite "lwt_pool" [
128128
if !k then
129129
let l = ref 0 in Lwt.return l
130130
else
131-
Lwt.fail Dummy_error
131+
raise Dummy_error
132132
in
133-
let v l = if !l = 0 then Lwt.return_true else Lwt.fail Dummy_error in
133+
let v l = if !l = 0 then Lwt.return_true else raise Dummy_error in
134134
let p = Lwt_pool.create 1 ~validate:v gen in
135135
let u1 = Lwt_pool.use p (fun l -> l := 1; k:= false; Lwt_condition.wait c) in
136136
let u2 = Lwt_pool.use p (fun l -> Lwt.return !l) in
@@ -156,7 +156,7 @@ let suite = suite "lwt_pool" [
156156
let cond = Lwt_condition.create() in
157157
let p = Lwt_pool.create 1 ~validate:v ~check:c gen in
158158
let _ = Lwt_pool.use p (fun l -> l := 1; Lwt_condition.wait cond) in
159-
let _ = Lwt_pool.use p (fun l -> l := 2; Lwt.fail Dummy_error) in
159+
let _ = Lwt_pool.use p (fun l -> l := 2; raise Dummy_error) in
160160
let u3 = Lwt_pool.use p (fun l -> Lwt.return !l) in
161161
let () = Lwt_condition.signal cond "done" in
162162
Lwt.bind u3 (fun v ->
@@ -169,7 +169,7 @@ let suite = suite "lwt_pool" [
169169
let p = Lwt_pool.create 1 gen in
170170
let _ = Lwt_pool.use p (fun l ->
171171
Lwt.bind (Lwt_condition.wait cond)
172-
(fun _ -> l:= 1; Lwt.fail Dummy_error)) in
172+
(fun _ -> l:= 1; raise Dummy_error)) in
173173
let u2 = Lwt_pool.use p (fun l -> Lwt.return !l) in
174174
let () = Lwt_condition.signal cond "done" in
175175
Lwt.bind u2 (fun v ->

test/core/test_lwt_result.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ let suite =
9393

9494
test "catch, error case"
9595
(fun () ->
96-
let x () = Lwt.fail Dummy_error in
96+
let x () = raise Dummy_error in
9797
Lwt.return (Lwt_result.catch x = Lwt_result.fail Dummy_error)
9898
);
9999

test/core/test_lwt_stream.ml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ let expect_exit f =
1111
Lwt.return_false)
1212
(function
1313
| Exit -> Lwt.return_true
14-
| e -> Lwt.fail e)
14+
| e -> Lwt.reraise e)
1515

1616
let suite = suite "lwt_stream" [
1717
test "from"
@@ -351,7 +351,7 @@ let suite = suite "lwt_stream" [
351351
return (Some x)
352352
| (Result.Error e)::l ->
353353
q := l;
354-
Lwt.fail e)
354+
raise e)
355355
in
356356
Lwt_stream.to_list (Lwt_stream.wrap_exn stream) >>= fun l' ->
357357
return (l = l'));
@@ -418,7 +418,7 @@ let suite = suite "lwt_stream" [
418418

419419
test "exception passing: basic, from"
420420
(fun () ->
421-
let stream = Lwt_stream.from (fun () -> Lwt.fail Exit) in
421+
let stream = Lwt_stream.from (fun () -> raise Exit) in
422422
expect_exit (fun () -> Lwt_stream.get stream));
423423

424424
test "exception passing: basic, from_direct"
@@ -428,12 +428,12 @@ let suite = suite "lwt_stream" [
428428

429429
test "exception passing: to_list"
430430
(fun () ->
431-
let stream = Lwt_stream.from (fun () -> Lwt.fail Exit) in
431+
let stream = Lwt_stream.from (fun () -> raise Exit) in
432432
expect_exit (fun () -> Lwt_stream.to_list stream));
433433

434434
test "exception passing: mapped"
435435
(fun () ->
436-
let stream = Lwt_stream.from (fun () -> Lwt.fail Exit) in
436+
let stream = Lwt_stream.from (fun () -> raise Exit) in
437437
let stream = Lwt_stream.map (fun v -> v) stream in
438438
expect_exit (fun () -> Lwt_stream.get stream));
439439

0 commit comments

Comments
 (0)