123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116openCoreopenImportincludeCore.Commandtype'awith_options=?extract_exn:bool->'aletshutdown_with_errore=Caml.at_exit(fun()->(* We use [Core] printing rather than [Async] printing, because the program may
already be shutting down, which could cause the error to be omitted because
shutdown only waits for flush of the output written before [shutdown] was called.
We want to make sure the error is seen. We delay it until [at_exit] to avoid
interleaving this with flushing of Async writers. *)Core.prerr_endline(Error.to_string_hume));shutdown1;;letmaybe_print_error_and_shutdown=function|Ok()->shutdown0|Errore->shutdown_with_errore;;(* For command line applications, we want the following behavior: [async-command-exe |
less] should never truncate the output of the exe.
Two behaviors in async prevent that from happening:
1. the at_shutdown handlers in writer.ml that waits for pipes to be flushed at most 5s
even if the data hasn't made it to the OS
2. the default 10s timeout before forced shutdown in shutdown.ml
Here is what we do about it:
1. Changing writer.ml might be ok, but only for stdout/stderr, not in general: if the
process has pipes connecting it to other processes it spawned, we most likely don't
want to block shutdown until they stop (could deadlock if there is a pipe in the
other direction and the other one process waits for it to die).
So instead, we wait for stdout and stderr to be flushed with no timeout, or that
their consumer has left.
2. We don't force shutdown until stdout/stderr is flushed.
That wait can be bypassed by redefining [Shutdown.default_force], or passing
[~force] to [shutdown], which looks like the desired behavior.
These two behavior changes seem fine for servers as well (where stdout/stderr should
contain almost nothing, or even be /dev/null), so we make them all the time.
*)letin_async?extract_exnparamon_result=Param.mapparam~f:(funstaged_main()->letmain=Or_error.try_with(fun()->unstage(staged_main()))inmatchmainwith|Errore->shutdown_with_errore;(never_returns(Scheduler.go()):unit)|Okmain->letbefore_shutdown()=Deferred.List.iter~how:`ParallelWriter.[forcestdout;forcestderr]~f:(funwriter->Deferred.any_unit[Writer.close_finishedwriter;Writer.consumer_leftwriter;Writer.flushedwriter])inShutdown.at_shutdownbefore_shutdown;Shutdown.set_default_force(letprev=Shutdown.default_force()infun()->Deferred.all_unit(* The 1s gives a bit of time to the process to stop silently rather than with
a "shutdown forced" message and exit 1 if [prev ()] finished first. *)[prev();(before_shutdown()>>=fun()->after(sec1.))]);upon(Deferred.Or_error.try_with?extract_exn(fun()->main`Scheduler_started))on_result;(never_returns(Scheduler.go()):unit));;type'rstaged=([`Scheduler_started]->'r)Staged.tmoduleStaged=structletasync?extract_exn~summary?readmeparam=leton_result=maybe_print_error_and_shutdowninbasic~summary?readme(in_async?extract_exnparamon_result);;letasync_spec?extract_exn~summary?readmespecmain=async?extract_exn~summary?readme(Spec.to_paramspecmain);;letasync_or_error?extract_exn~summary?readmeparam=leton_resultres=maybe_print_error_and_shutdown(Or_error.joinres)inbasic~summary?readme(in_async?extract_exnparamon_result);;letasync_spec_or_error?extract_exn~summary?readmespecmain=async_or_error?extract_exn~summary?readme(Spec.to_paramspecmain);;endletstage_param=Param.map~f:(funmain()->stage(fun`Scheduler_started->main()))letasync?extract_exn~summary?readmeparam=Staged.async?extract_exn~summary?readme(stage_paramparam);;letasync_or_error?extract_exn~summary?readmeparam=Staged.async_or_error?extract_exn~summary?readme(stage_paramparam);;letasync_spec?extract_exn~summary?readmespecmain=async?extract_exn~summary?readme(Spec.to_paramspecmain);;letasync_spec_or_error?extract_exn~summary?readmespecmain=async_or_error?extract_exn~summary?readme(Spec.to_paramspecmain);;