Skip to content

Commit

Permalink
Add error catcher in scheduler.
Browse files Browse the repository at this point in the history
  • Loading branch information
toots committed Jul 5, 2023
1 parent 3de96a7 commit 8dce9f6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
14 changes: 12 additions & 2 deletions src/duppy.ml
Expand Up @@ -64,6 +64,7 @@ type 'a t = {
}

type 'a scheduler = {
on_error : exn -> Printexc.raw_backtrace -> unit;
out_pipe : Unix.file_descr;
in_pipe : Unix.file_descr;
compare : 'a -> 'a -> int;
Expand All @@ -84,9 +85,10 @@ let clear_tasks s =
s.tasks <- [];
Mutex.unlock s.tasks_m

let create ?(compare = compare) () =
let create ?(on_error = Printexc.raise_with_backtrace) ?(compare = compare) () =
let out_pipe, in_pipe = Unix.pipe () in
{
on_error;
out_pipe;
in_pipe;
compare;
Expand Down Expand Up @@ -273,7 +275,15 @@ let exec s (priorities : 'a -> bool) =
| (_, task), remaining ->
s.ready <- remaining;
Mutex.unlock s.ready_m;
add_t s (task ());
let tasks =
match task () with
| exception exn ->
let bt = Printexc.get_raw_backtrace () in
s.on_error exn bt;
[]
| v -> v
in
add_t s tasks;
true
| exception Not_found -> false

Expand Down
6 changes: 5 additions & 1 deletion src/duppy.mli
Expand Up @@ -61,7 +61,11 @@ type 'a scheduler
(** Initiate a new scheduler
* @param compare the comparison function used to sort tasks according to priorities.
* Works as in [List.sort] *)
val create : ?compare:('a -> 'a -> int) -> unit -> 'a scheduler
val create :
?on_error:(exn -> Printexc.raw_backtrace -> unit) ->
?compare:('a -> 'a -> int) ->
unit ->
'a scheduler

(** [queue ~log ~priorities s name]
* starts a queue, on the scheduler [s] only processing priorities [p]
Expand Down

0 comments on commit 8dce9f6

Please sign in to comment.