Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566open Bos
type t = { cmd : string list; time : float; output_file : Fpath.t option; output : string; errors : string; status : [ `Exited of int | `Signaled of int ];}
let src = Logs.Src.create "day11.exec.run" ~doc:"Subprocess execution"module Log = (val Logs.src_log src)
(* Delegate fork+exec to the fork helper daemon via a Unix socket. The helper is a tiny process (~5MB) so its forks are sub-millisecond, compared to ~100ms when forking the main 1.7GB process. The socket I/O runs in a systhread so it doesn't block the Eio event loop. *)let run_via_helper cmd env_arr output_file = let helper = Fork_client.get_instance () in let argv = Array.of_list cmd in let file_path = Option.map Fpath.to_string output_file in Eio_unix.run_in_systhread (fun () -> Fork_client.spawn helper ~env_arr ~argv ~output_file:file_path)
let run _env cmd output_file = let cmd = Cmd.to_list cmd in Log.debug (fun m -> m "Executing: %s" (String.concat " " cmd)); let t_start = Unix.gettimeofday () in let env_arr = let env = OS.Env.current () |> Result.get_ok in Astring.String.Map.fold (fun k v acc -> (k ^ "=" ^ v) :: acc) env [] |> Array.of_list in let rec run_with_retry retries = try run_via_helper cmd env_arr output_file with exn -> if retries > 0 then begin Log.warn (fun m -> m "Fork helper failed (%s), retrying (%d left)" (Printexc.to_string exn) retries); Unix.sleepf 0.1; run_with_retry (retries - 1) end else raise exn in let output, errors, status = run_with_retry 3 in let t_end = Unix.gettimeofday () in let time = t_end -. t_start in let result = { cmd; time; output_file; output; errors; status } in (match result.status with | `Exited 0 -> () | _ -> let verb, n = match result.status with | `Exited n -> ("exited", n) | `Signaled n -> ("signaled", n) in Log.err (fun m -> m "Process %s with %d: '%s'\nStdout:\n%s\nStderr:\n%s" verb n (String.concat " " result.cmd) result.output result.errors)); result