type result = {status: Unix.process_status; stdout: string; stderr: string} type job = {program: string; args: string list; cwd: string} type stdin_policy = Inherit_stdin | Null_stdin exception Error of string let decode_utf8_lossy value = if String.is_valid_utf_8 value then value else let output = Buffer.create (String.length value) in let rec loop index = if index < String.length value then ( let decoded = String.get_utf_8_uchar value index in let length = max 1 (Uchar.utf_decode_length decoded) in if Uchar.utf_decode_is_valid decoded then Buffer.add_substring output value index length else Buffer.add_utf_8_uchar output Uchar.rep; loop (index + length)) in loop 0; Buffer.contents output let succeeded result = result.status = Unix.WEXITED 0 let status_string = function | Unix.WEXITED code -> Printf.sprintf "exit code %d" code | Unix.WSIGNALED signal -> Printf.sprintf "signal %d" signal | Unix.WSTOPPED signal -> Printf.sprintf "stopped by signal %d" signal type capture = { thread: Thread.t; outcome: (string, exn) Stdlib.result option ref; } type child_wait = { thread: Thread.t; direct_outcome: (Unix.process_status, exn) Stdlib.result option Atomic.t; outcome: (result, exn) Stdlib.result option Atomic.t; } type running = {process: Platform.process; child_wait: child_wait} type launch_ownership = { mutable stdin: Unix.file_descr option; mutable stdout_read: Unix.file_descr option; mutable stdout_write: Unix.file_descr option; mutable stderr_read: Unix.file_descr option; mutable stderr_write: Unix.file_descr option; mutable stdout_capture: capture option; mutable stderr_capture: capture option; mutable process: Platform.process option; } let empty_launch_ownership () = { stdin = None; stdout_read = None; stdout_write = None; stderr_read = None; stderr_write = None; stdout_capture = None; stderr_capture = None; process = None; } type completion_notifier = { mutex: Mutex.t; condition: Condition.t; mutable generation: int; mutable stopped: bool; } (* Mutex.protect needs OCaml 5.1; the minimum supported version is 5.0. *) let with_lock mutex action = Mutex.lock mutex; Fun.protect ~finally:(fun () -> Mutex.unlock mutex) action let create_completion_notifier () = { mutex = Mutex.create (); condition = Condition.create (); generation = 0; stopped = false; } let notify_completion notifier = with_lock notifier.mutex (fun () -> if not notifier.stopped then ( notifier.generation <- notifier.generation + 1; Condition.broadcast notifier.condition)) let notifier_generation notifier = with_lock notifier.mutex (fun () -> notifier.generation) let await_notification notifier generation = with_lock notifier.mutex (fun () -> while notifier.generation = generation && not notifier.stopped do Condition.wait notifier.condition notifier.mutex done; notifier.generation) let with_completion_notifier ~ticker_enabled action = (* The scheduler needs immediate child completion without repeatedly asking the operating system about every running PID. A condition variable wakes it when status and captured output are both ready; one ticker also wakes a supplied watch poll callback every five milliseconds while children are busy. *) let notifier = create_completion_notifier () in let rec send_tick () = Thread.delay 0.005; let continue = with_lock notifier.mutex (fun () -> if notifier.stopped then false else ( notifier.generation <- notifier.generation + 1; Condition.broadcast notifier.condition; true)) in if continue then send_tick () in let ticker = if ticker_enabled then Some (Thread.create send_tick ()) else None in let stop () = with_lock notifier.mutex (fun () -> notifier.stopped <- true; Condition.broadcast notifier.condition); Option.iter Thread.join ticker in Fun.protect ~finally:stop (fun () -> action notifier) let close_noerr descriptor = try Unix.close descriptor with Unix.Unix_error _ -> () let start_capture ?on_chunk descriptor : capture = let outcome = ref None in let thread = Thread.create (fun () -> outcome := Some (try Fun.protect ~finally:(fun () -> close_noerr descriptor) (fun () -> let output = Buffer.create 4096 in let bytes = Bytes.create 65536 in let rec read () = try match Unix.read descriptor bytes 0 (Bytes.length bytes) with | 0 -> () | count -> (match on_chunk with | Some on_chunk -> on_chunk bytes count | None -> Buffer.add_subbytes output bytes 0 count); read () with Unix.Unix_error (Unix.EINTR, _, _) -> read () in read (); Ok (match on_chunk with | Some _ -> "" | None -> Buffer.contents output |> decode_utf8_lossy)) with exn -> Error exn)) () in {thread; outcome} let capture_outcome (capture : capture) = Thread.join capture.thread; match !(capture.outcome) with | Some outcome -> outcome | None -> Error (Failure "subprocess output reader did not finish") let capture_error exn = Error ("failed to capture subprocess output: " ^ Printexc.to_string exn) let start_child_wait pid notifier stdout_capture stderr_capture : child_wait = let direct_outcome = Atomic.make None in let outcome = Atomic.make None in let rec wait () = try let _, status = Unix.waitpid [] pid in status with Unix.Unix_error (Unix.EINTR, _, _) -> wait () in let thread = Thread.create (fun () -> let status = try Ok (wait ()) with exn -> Error exn in Atomic.set direct_outcome (Some status); let stdout = capture_outcome stdout_capture in let stderr = capture_outcome stderr_capture in let result = match (status, stdout, stderr) with | Ok status, Ok stdout, Ok stderr -> Ok {status; stdout; stderr} | Error exn, _, _ -> Error exn | _, Error exn, _ | _, _, Error exn -> Error (capture_error exn) in Atomic.set outcome (Some result); notify_completion notifier) () in {thread; direct_outcome; outcome} let fail_launch ownership launch_error = let termination_error = match ownership.process with | None -> None | Some process -> ( match Platform.signal_process_tree process Sys.sigkill with | Ok () -> (try ignore (Unix.waitpid [] (Platform.process_id process)) with Unix.Unix_error _ -> ()); None | Error message -> Some message) in List.iter (fun descriptor -> Option.iter close_noerr descriptor) [ ownership.stdout_write; ownership.stderr_write; ownership.stdout_read; ownership.stderr_read; ownership.stdin; ]; if Option.is_none termination_error && Option.is_some ownership.process then ( Option.iter (fun (capture : capture) -> Thread.join capture.thread) ownership.stdout_capture; Option.iter (fun (capture : capture) -> Thread.join capture.thread) ownership.stderr_capture); let release_error = try Option.iter Platform.release_process ownership.process; None with release_exn -> Some release_exn in let error = match termination_error with | Some message -> Error ("Could not terminate a partially launched subprocess tree: " ^ message) | None -> Option.value release_error ~default:launch_error in raise error let launch ?env ?stdout_chunk ?stderr_chunk ?(stdin = Null_stdin) ~notifier job = let ownership = empty_launch_ownership () in try let pipes = Platform_common.create_capture_pipes () in let (stdout_read, stdout_write), (stderr_read, stderr_write) = pipes in ownership.stdout_read <- Some stdout_read; ownership.stdout_write <- Some stdout_write; ownership.stderr_read <- Some stderr_read; ownership.stderr_write <- Some stderr_write; let stdout = start_capture ?on_chunk:stdout_chunk stdout_read in ownership.stdout_read <- None; ownership.stdout_capture <- Some stdout; let stderr = start_capture ?on_chunk:stderr_chunk stderr_read in ownership.stderr_read <- None; ownership.stderr_capture <- Some stderr; let stdin = match stdin with | Inherit_stdin -> Unix.stdin | Null_stdin -> let descriptor = Unix.openfile Platform.null_device [Unix.O_RDONLY; Unix.O_CLOEXEC] 0 in ownership.stdin <- Some descriptor; descriptor in let process = Platform.spawn ~env ~cwd:job.cwd ~program:job.program ~args:job.args ~stdin ~stdout:stdout_write ~stderr:stderr_write in ownership.process <- Some process; let pid = Platform.process_id process in Option.iter close_noerr ownership.stdin; ownership.stdin <- None; ownership.stdout_write <- None; close_noerr stdout_write; ownership.stderr_write <- None; close_noerr stderr_write; let child_wait = start_child_wait pid notifier stdout stderr in {process; child_wait} with exn -> fail_launch ownership exn let wait_for_running ~poll notifier active = let rec find_completed = function | [] -> None | (child : running) :: rest -> ( match Atomic.get child.child_wait.outcome with | Some outcome -> Some (child, outcome) | None -> find_completed rest) in let rec wait generation = match find_completed active with | Some (child, Ok result) -> (child, result) | Some (_, Error exn) -> raise exn | None -> poll (); await_notification notifier generation |> wait in notifier_generation notifier |> wait let signal_running (children : running list) = if children <> [] then ( let completion_ready (child : running) = Option.is_some (Atomic.get child.child_wait.outcome) in let root_identity_lost (child : running) = Option.is_some (Atomic.get child.child_wait.direct_outcome) in let signal_all signal = List.fold_left (fun errors child -> if completion_ready child then errors else match Platform.signal_process_tree child.process signal with | Ok () -> errors | Error message -> (child, message) :: errors) [] children in let graceful_errors = signal_all Sys.sigterm in let deadline = Unix.gettimeofday () +. 0.25 in let rec wait_until_deadline children = let remaining = List.filter (fun child -> not (root_identity_lost child)) children in if remaining <> [] && Unix.gettimeofday () < deadline then ( ignore (Unix.select [] [] [] 0.01); wait_until_deadline remaining) else remaining in ignore (wait_until_deadline children); (* Every original process group needs escalation because a direct child can exit while a PPX or helper in its group remains alive. Children whose result is published are skipped. A group whose direct child was reaped but whose result is not yet published could in theory have had its ID reused by an unrelated new group in that instant; with sequential PIDs and a window of microseconds this is not a practical concern. *) let termination_errors = if Platform.escalate_process_groups then signal_all Sys.sigkill else graceful_errors in (* On Darwin, signalling a group containing only zombies returns EPERM. Pipe closure and waiter publication happen on separate threads, so the process result can become ready just after that harmless failure. A bounded wait distinguishes this race from a live tree that the platform genuinely could not terminate. *) let completion_deadline = Unix.gettimeofday () +. 0.25 in let rec unresolved errors = let errors = List.filter (fun (child, _) -> not (completion_ready child)) errors in if errors <> [] && Unix.gettimeofday () < completion_deadline then ( ignore (Unix.select [] [] [] 0.01); unresolved errors) else errors in match unresolved termination_errors |> List.rev with | [] -> () | errors -> raise (Error ("Could not terminate a subprocess tree: " ^ String.concat "; " (List.map snd errors)))) let release_running (child : running) = Thread.join child.child_wait.thread; Platform.release_process child.process let release_after_completion child = ignore (Thread.create release_running child) let terminate_running (children : running list) = if children <> [] then ( try signal_running children; List.iter release_running children with exn -> List.iter release_after_completion children; raise exn)