We are using custom scripts in a Linux/shell/pipeline environment shared by users with different language backgrounds. Julia would be a nice fit to some of the process stages.
As any such scripts might later be used and also read by people (yet) unfamiliar with Julia (but with, say, background in bash), I’d like to avoid anything that could be slightly frightening, and make a reader go “:q!”.
In our pipelines, we like to ignore SIGPIPE.
#!/usr/bin/env julia
function main()
foreach(println, 1:1_000_000)
end
main()
bash> run.jl | head -n1
1
ERROR: LoadError: IOError: write: broken pipe (EPIPE)
Stacktrace:
[1] uv_write(s::Base.PipeEndpoint, p::Ptr{UInt8}, n::UInt64)
@ Base ./stream.jl:1010
...
To avoid this error/backtrace, we suppress an exception:
try
main()
catch e
isa(e, Base.IOError) && e.code == Base.UV_EPIPE && exit(0)
rethrow()
end
(
I found the code UP_EPIPE
value used in julia/test/read.jl: julia/read.jl at b4ab6eff13c3afe8188b7f7fa8b7589ea212ef24 · JuliaLang/julia · GitHub
There was also a 2015 thread remove unwarranted handling of SIGPIPE instead of ignoring it. by amitmurthy · Pull Request #9956 · JuliaLang/julia · GitHub and a mention of SIGPIPE / sigdie_handler()
in JuliaDocumentation.pdf.
)
My question would be if there is another, maybe more elegant/Julian way to ignore SIGPIPE?
I found nothing equivalent to, say, Base.exit_on_sigint()
/ atexit()
…
Maybe playing with the default signal handlers could work? Any link on more background on those would be appreciated!