Skip to content

Commit 266f173

Browse files
authored
Merge pull request #1008 from ocsigen/more-raise-less-fail
More raise less fail
2 parents 68cf601 + 4d98a0a commit 266f173

18 files changed

+113
-97
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

src/ppx/ppx_lwt.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ let add_wildcard_case cases =
2323
if not has_wildcard
2424
then cases
2525
@ (let loc = Location.none in
26-
[case ~lhs:[%pat? exn] ~guard:None ~rhs:[%expr Lwt.fail exn]])
26+
[case ~lhs:[%pat? exn] ~guard:None ~rhs:[%expr Lwt.reraise exn]])
2727
else cases
2828

2929
(** {3 Internal names} *)
@@ -154,11 +154,11 @@ let lwt_expression mapper exp attributes ext_loc =
154154
Some (mapper#expression { new_exp with pexp_attributes })
155155

156156
(* [assert%lwt $e$] ≡
157-
[try Lwt.return (assert $e$) with exn -> Lwt.fail exn] *)
157+
[try Lwt.return (assert $e$) with exn -> Lwt.reraise exn] *)
158158
| Pexp_assert e ->
159159
let new_exp =
160160
let loc = !default_loc in
161-
[%expr try Lwt.return (assert [%e e]) with exn -> Lwt.fail exn]
161+
[%expr try Lwt.return (assert [%e e]) with exn -> Lwt.reraise exn]
162162
in
163163
Some (mapper#expression { new_exp with pexp_attributes })
164164

src/ppx/ppx_lwt.mli

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ catch (fun () -> f x)
8080
prerr_endline msg;
8181
return ()
8282
| exn ->
83-
Lwt.fail exn)
83+
Lwt.reraise exn)
8484
]}
8585
86-
Note that the [exn -> Lwt.fail exn] branch is automatically added
86+
Note that the [exn -> Lwt.reraise exn] branch is automatically added
8787
when needed.
8888
8989
- finalizer:

src/unix/lwt_io.ml

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ let perform_io : type mode. mode _channel -> int Lwt.t = fun ch ->
231231
(function
232232
| Unix.Unix_error (Unix.EPIPE, _, _) ->
233233
Lwt.return 0
234-
| exn -> Lwt.fail exn) [@ocaml.warning "-4"]
234+
| exn -> Lwt.reraise exn) [@ocaml.warning "-4"]
235235
else
236236
perform ch.buffer ptr len
237237
in
@@ -525,7 +525,7 @@ let make :
525525
max = (match mode with
526526
| Input -> 0
527527
| Output -> size);
528-
close = lazy(Lwt.catch close Lwt.fail);
528+
close = lazy(Lwt.catch close Lwt.reraise);
529529
abort_waiter = abort_waiter;
530530
abort_wakener = abort_wakener;
531531
main = wrapper;
@@ -537,7 +537,7 @@ let make :
537537
perform_io,
538538
fun pos cmd ->
539539
try seek pos cmd
540-
with e when Lwt.Exception_filter.run e -> Lwt.fail e
540+
with e when Lwt.Exception_filter.run e -> Lwt.reraise e
541541
);
542542
} and wrapper = {
543543
state = Idle;
@@ -678,7 +678,7 @@ struct
678678
let ptr = ic.ptr in
679679
if ptr = ic.max then
680680
refill ic >>= function
681-
| 0 -> Lwt.fail End_of_file
681+
| 0 -> raise End_of_file
682682
| _ -> read_char ic
683683
else begin
684684
ic.ptr <- ptr + 1;
@@ -690,7 +690,7 @@ struct
690690
(fun () -> read_char ic >|= fun ch -> Some ch)
691691
(function
692692
| End_of_file -> Lwt.return_none
693-
| exn -> Lwt.fail exn)
693+
| exn -> Lwt.reraise exn)
694694

695695
let read_line ic =
696696
let buf = Buffer.create 128 in
@@ -711,7 +711,7 @@ struct
711711
if cr_read then Buffer.add_char buf '\r';
712712
Lwt.return(Buffer.contents buf)
713713
| exn ->
714-
Lwt.fail exn)
714+
Lwt.reraise exn)
715715
in
716716
read_char ic >>= function
717717
| '\r' -> loop true
@@ -723,7 +723,7 @@ struct
723723
(fun () -> read_line ic >|= fun ch -> Some ch)
724724
(function
725725
| End_of_file -> Lwt.return_none
726-
| exn -> Lwt.fail exn)
726+
| exn -> Lwt.reraise exn)
727727

728728
let unsafe_read_into' ic blit buf ofs len =
729729
let avail = ic.max - ic.ptr in
@@ -771,7 +771,7 @@ struct
771771
let rec loop ic buf ofs len =
772772
read_into ic buf ofs len >>= function
773773
| 0 ->
774-
Lwt.fail End_of_file
774+
raise End_of_file
775775
| n ->
776776
let len = len - n in
777777
if len = 0 then
@@ -985,7 +985,7 @@ struct
985985
if ic.max - ic.ptr < size then
986986
refill ic >>= function
987987
| 0 ->
988-
Lwt.fail End_of_file
988+
raise End_of_file
989989
| _ ->
990990
read_block_unsafe ic size f
991991
else begin
@@ -1440,7 +1440,7 @@ let open_temp_file ?buffer ?flags ?perm ?temp_dir ?prefix ?(suffix = "") () =
14401440
Lwt.return (fname, chan))
14411441
(function
14421442
| Unix.Unix_error _ when n < 1000 -> attempt (n + 1)
1443-
| exn -> Lwt.fail exn)
1443+
| exn -> Lwt.reraise exn)
14441444
in
14451445
attempt 0
14461446

@@ -1468,7 +1468,7 @@ let create_temp_dir
14681468
Lwt.return name)
14691469
(function
14701470
| Unix.Unix_error (Unix.EEXIST, _, _) when n < 1000 -> attempt (n + 1)
1471-
| exn -> Lwt.fail exn)
1471+
| exn -> Lwt.reraise exn)
14721472
in
14731473
attempt 0
14741474

@@ -1489,10 +1489,10 @@ let win32_unlink fn =
14891489
(* If everything succeeded but the final removal still failed,
14901490
restore original permissions *)
14911491
Lwt_unix.chmod fn st_perm >>= fun () ->
1492-
Lwt.fail exn)
1492+
Lwt.reraise exn)
14931493
)
1494-
(fun _ -> Lwt.fail exn)
1495-
| exn -> Lwt.fail exn)
1494+
(fun _ -> Lwt.reraise exn)
1495+
| exn -> Lwt.reraise exn)
14961496

14971497
let unlink =
14981498
if Sys.win32 then
@@ -1549,7 +1549,7 @@ let close_socket fd =
15491549
(function
15501550
(* Occurs if the peer closes the connection first. *)
15511551
| Unix.Unix_error (Unix.ENOTCONN, _, _) -> Lwt.return_unit
1552-
| exn -> Lwt.fail exn) [@ocaml.warning "-4"])
1552+
| exn -> Lwt.reraise exn) [@ocaml.warning "-4"])
15531553
(fun () ->
15541554
Lwt_unix.close fd)
15551555

@@ -1574,7 +1574,7 @@ let open_connection ?fd ?in_buffer ?out_buffer sockaddr =
15741574
~mode:output (Lwt_bytes.write fd)))
15751575
(fun exn ->
15761576
Lwt_unix.close fd >>= fun () ->
1577-
Lwt.fail exn)
1577+
Lwt.reraise exn)
15781578

15791579
let with_close_connection f (ic, oc) =
15801580
(* If the user already tried to close the socket and got an exception, we
@@ -1639,7 +1639,7 @@ let establish_server_generic
16391639
(function
16401640
| Unix.Unix_error (Unix.ECONNABORTED, _, _) ->
16411641
Lwt.return `Try_again
1642-
| e -> Lwt.fail e)
1642+
| e -> Lwt.reraise e)
16431643
in
16441644

16451645
Lwt.pick [try_to_accept; should_stop] >>= function

src/unix/lwt_process.cppo.ml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ let read_opt read ic =
378378
(function
379379
| Unix.Unix_error (Unix.EPIPE, _, _) | End_of_file ->
380380
Lwt.return_none
381-
| exn -> Lwt.fail exn) [@ocaml.warning "-4"]
381+
| exn -> Lwt.reraise exn) [@ocaml.warning "-4"]
382382

383383
let recv_chars pr =
384384
let ic = pr#stdout in
@@ -512,8 +512,8 @@ let pmap ?timeout ?env ?cwd ?stderr cmd text =
512512
| Lwt.Canceled as exn ->
513513
(* Cancel the getter if the sender was canceled. *)
514514
Lwt.cancel getter;
515-
Lwt.fail exn
516-
| exn -> Lwt.fail exn)
515+
Lwt.reraise exn
516+
| exn -> Lwt.reraise exn)
517517

518518
let pmap_chars ?timeout ?env ?cwd ?stderr cmd chars =
519519
let pr = open_process ?timeout ?env ?cwd ?stderr cmd in
@@ -534,8 +534,8 @@ let pmap_line ?timeout ?env ?cwd ?stderr cmd line =
534534
| Lwt.Canceled as exn ->
535535
(* Cancel the getter if the sender was canceled. *)
536536
Lwt.cancel getter;
537-
Lwt.fail exn
538-
| exn -> Lwt.fail exn)
537+
Lwt.reraise exn
538+
| exn -> Lwt.reraise exn)
539539

540540
let pmap_lines ?timeout ?env ?cwd ?stderr cmd lines =
541541
let pr = open_process ?timeout ?env ?cwd ?stderr cmd in

src/unix/lwt_unix.cppo.ml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ let auto_pause timeout =
147147

148148
exception Timeout
149149

150-
let timeout d = sleep d >>= fun () -> Lwt.fail Timeout
150+
let timeout d = sleep d >>= fun () -> raise Timeout
151151

152152
let with_timeout d f = Lwt.pick [timeout d; Lwt.apply f ()]
153153

@@ -582,7 +582,7 @@ let wrap_syscall event ch action =
582582
| Retry_write ->
583583
register_action Write ch action
584584
| e when Lwt.Exception_filter.run e ->
585-
Lwt.fail e
585+
Lwt.reraise e
586586

587587
(* +-----------------------------------------------------------------+
588588
| Basic file input/output |
@@ -636,7 +636,7 @@ let wait_read ch =
636636
Lwt.return_unit
637637
else
638638
register_action Read ch ignore)
639-
Lwt.fail
639+
Lwt.reraise
640640

641641
external stub_read : Unix.file_descr -> Bytes.t -> int -> int -> int = "lwt_unix_read"
642642
external read_job : Unix.file_descr -> Bytes.t -> int -> int -> int job = "lwt_unix_read_job"
@@ -694,7 +694,7 @@ let wait_write ch =
694694
Lwt.return_unit
695695
else
696696
register_action Write ch ignore)
697-
Lwt.fail
697+
Lwt.reraise
698698

699699
external stub_write : Unix.file_descr -> Bytes.t -> int -> int -> int = "lwt_unix_write"
700700
external write_job : Unix.file_descr -> Bytes.t -> int -> int -> int job = "lwt_unix_write_job"
@@ -1034,7 +1034,7 @@ let file_exists name =
10341034
(fun e ->
10351035
match e with
10361036
| Unix.Unix_error _ -> Lwt.return_false
1037-
| _ -> Lwt.fail e) [@ocaml.warning "-4"]
1037+
| _ -> Lwt.reraise e) [@ocaml.warning "-4"]
10381038

10391039
external utimes_job : string -> float -> float -> unit job =
10401040
"lwt_unix_utimes_job"
@@ -1140,7 +1140,7 @@ struct
11401140
(fun e ->
11411141
match e with
11421142
| Unix.Unix_error _ -> Lwt.return_false
1143-
| _ -> Lwt.fail e) [@ocaml.warning "-4"]
1143+
| _ -> Lwt.reraise e) [@ocaml.warning "-4"]
11441144

11451145
end
11461146

@@ -1408,7 +1408,7 @@ let files_of_directory path =
14081408
(fun () -> readdir_n handle chunk_size)
14091409
(fun exn ->
14101410
closedir handle >>= fun () ->
1411-
Lwt.fail exn) >>= fun entries ->
1411+
Lwt.reraise exn) >>= fun entries ->
14121412
if Array.length entries < chunk_size then begin
14131413
state := LDS_done;
14141414
closedir handle >>= fun () ->
@@ -1423,7 +1423,7 @@ let files_of_directory path =
14231423
(fun () -> readdir_n handle chunk_size)
14241424
(fun exn ->
14251425
closedir handle >>= fun () ->
1426-
Lwt.fail exn) >>= fun entries ->
1426+
Lwt.reraise exn) >>= fun entries ->
14271427
if Array.length entries < chunk_size then begin
14281428
state := LDS_done;
14291429
closedir handle >>= fun () ->
@@ -2395,7 +2395,7 @@ let () =
23952395
let _waitpid flags pid =
23962396
Lwt.catch
23972397
(fun () -> Lwt.return (Unix.waitpid flags pid))
2398-
Lwt.fail
2398+
Lwt.reraise
23992399

24002400
let waitpid =
24012401
if Sys.win32 then

0 commit comments

Comments
 (0)