@@ -556,7 +556,6 @@ module Low_level = struct
556556 type t = {
557557 proc : Luv.Process .t ;
558558 status : (int * int64 ) Promise .t ;
559- hook : Switch .hook ;
560559 }
561560
562561 let pid t = Luv.Process. pid t.proc
@@ -565,24 +564,28 @@ module Low_level = struct
565564 let parent_pipe = Handle. to_luv parent_pipe in
566565 Luv.Process. to_parent_pipe ?readable_in_child ?writable_in_child ?overlapped ~fd ~parent_pipe ()
567566
568- let await_exit t =
569- let res = Promise. await t.status in
570- Switch. remove_hook t.hook;
571- res
567+ let await_exit t = Promise. await t.status
572568
573569 let has_exited t = Promise. is_resolved t.status
574570
575571 let send_signal t i = Luv.Process. kill t.proc i |> or_raise
576572
577573 let spawn ?cwd ?env ?uid ?gid ?(redirect =[] ) ~sw cmd args =
578- let promise, resolve = Promise. create () in
574+ let status, set_status = Promise. create () in
575+ let hook = ref None in
579576 let on_exit _ ~exit_status ~term_signal =
580- let status = (term_signal, exit_status) in
581- Promise. resolve resolve status
577+ Option. iter Switch. remove_hook ! hook;
578+ Promise. resolve set_status (term_signal, exit_status)
582579 in
583580 let proc = Luv.Process. spawn ?environment:env ?uid ?gid ~loop: (get_loop () ) ?working_directory:cwd ~redirect ~on_exit cmd args |> or_raise in
584- let hook = Switch. on_release_cancellable sw (fun () -> Luv.Process. kill proc Luv.Signal. sigkill |> or_raise) in
585- { proc; status = promise; hook }
581+ if not (Promise. is_resolved status) then (
582+ let h = Switch. on_release_cancellable sw (fun () ->
583+ Luv.Process. kill proc Luv.Signal. sigkill |> or_raise;
584+ ignore (Promise. await status)
585+ ) in
586+ hook := Some h
587+ );
588+ { proc; status }
586589 end
587590
588591 module Poll = Poll
0 commit comments