123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117(*
* Copyright (c) 2022-2022 Tarides <contact@tarides.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*)open!ImportincludeAsync_intfmoduleUnix=structletkill_no_errpid=tryUnix.killpid9withUnix.Unix_error(e,s1,s2)->[%log.warn"Killing process with pid %d failed with error (%s, %s, %s)"pid(Unix.error_messagee)s1s2](** [Exit] is a stack of PIDs that will be killed [at_exit]. *)moduleExit=structletproc_list=ref[]letm=Mutex.create()letaddpid=Mutex.lockm;proc_list:=pid::!proc_list;Mutex.unlockmletremovepid=Mutex.lockm;proc_list:=List.filter(funpid'->pid<>pid')!proc_list;Mutex.unlockmlet()=at_exit@@fun()->List.iterkill_no_err!proc_listendtypeoutcome=[`Success|`Cancelled|`Failureofstring][@@derivingirmin]typestatus=[`Running|`Success|`Cancelled|`Failureofstring][@@derivingirmin]typet={pid:int;mutablestatus:status}letasyncf=Stdlib.flush_all();matchLwt_unix.fork()with|0->Lwt_main.Exit_hooks.remove_all();Lwt_main.abandon_yielded_and_paused();(tryf()withe->[%log.err"Unhandled exception in child process %s"(Printexc.to_stringe)]);(* Once finished, the child process kills itself to avoid calling
at_exit functions and also to avoid executing the rest of the call
stack (this would typically be a problem with alcotest). *)Unix.kill(Unix.getpid())9;assertfalse(* unreachable *)|pid->Exit.addpid;{pid;status=`Running}letstatus_of_process_outcome=function|Lwt_unix.WSIGNALEDxwhenx=Sys.sigkill->(* x is actually -7; -7 is the Sys.sigkill definition (not the OS' 9 as
might be expected) *)`Success(* the child is killing itself when it's done *)|Lwt_unix.WSIGNALEDn->`Failure(Fmt.str"Signaled %d"n)|Lwt_unix.WEXITEDn->`Failure(Fmt.str"Exited %d"n)|Lwt_unix.WSTOPPEDn->`Failure(Fmt.str"Stopped %d"n)letcancelt=matcht.statuswith|`Running->letpid,_=Unix.waitpid[Unix.WNOHANG]t.pidinifpid=0then((* Child process is still running. *)kill_no_errt.pid;Exit.removet.pid;t.status<-`Cancelled;true)elsefalse|_->falseletstatust=matcht.statuswith|`Running->letpid,status=Unix.waitpid[Unix.WNOHANG]t.pidinifpid=0then`Runningelselets=status_of_process_outcomestatusinExit.removepid;t.status<-s;s|#outcomeass->sletawaitt=matcht.statuswith|`Running->let+pid,status=Lwt_unix.waitpid[]t.pidinlets=status_of_process_outcomestatusinExit.removepid;t.status<-s;s|#outcomeass->Lwt.returnsend