Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Base.exit_on_sigint to wrap jl_exit_on_sigint #29411

Merged
merged 3 commits into from
May 1, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions base/c.jl
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,22 @@ function reenable_sigint(f::Function)
res
end

"""
exit_on_sigint(on::Bool)

Set `exit_on_sigint` flag of the julia runtime. If `false`, Ctrl-C
(SIGINT) is capturable as [`InterruptException`](@ref) in `try` block.
This is the default behavior in REPL, any code run via `-e` and `-E`
and in Julia script run with `-i` option.

If `true`, `InterruptException` is not thrown by Ctrl-C. Running code
upon such event requires [`atexit`](@ref). This is the default
behavior in Julia script run without `-i` option.
"""
function exit_on_sigint(on::Bool)
ccall(:jl_exit_on_sigint, Cvoid, (Cint,), on)
end

function ccallable(f::Function, rt::Type, argt::Type, name::Union{AbstractString,Symbol}=string(f))
ccall(:jl_extern_c, Cvoid, (Any, Any, Any, Cstring), f, rt, argt, name)
end
Expand Down
2 changes: 1 addition & 1 deletion base/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ function exec_options(opts)
if arg_is_program
# program
if !is_interactive
ccall(:jl_exit_on_sigint, Cvoid, (Cint,), 1)
exit_on_sigint(true)
end
try
include(Main, PROGRAM_FILE)
Expand Down
12 changes: 12 additions & 0 deletions base/docs/basedocs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1423,6 +1423,18 @@ TypeError
InterruptException()

The process was stopped by a terminal interrupt (CTRL+C).

Note that, in Julia script started without `-i` (interactive) option,
`InterruptException` is not thrown by default. Calling
[`Base.exit_on_sigint(false)`](@ref Base.exit_on_sigint) in the script
can recover the behavior of the REPL. Alternatively, a Julia script
can be started with

```sh
julia -e "include(popfirst!(ARGS))" script.jl
```

to let `InterruptException` be thrown by CTRL+C during the execution.
"""
InterruptException

Expand Down
1 change: 1 addition & 0 deletions doc/src/base/c.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Base.pointer_from_objref
Base.unsafe_pointer_to_objref
Base.disable_sigint
Base.reenable_sigint
Base.exit_on_sigint
Base.systemerror
Base.windowserror
Core.Ptr
Expand Down
4 changes: 2 additions & 2 deletions stdlib/REPL/test/repl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function fake_repl(@nospecialize(f); options::REPL.Options=REPL.Options(confirm_
end

# Writing ^C to the repl will cause sigint, so let's not die on that
ccall(:jl_exit_on_sigint, Cvoid, (Cint,), 0)
Base.exit_on_sigint(false)

# make sure `run_interface` can normally handle `eof`
# without any special handling by the user
Expand Down Expand Up @@ -764,7 +764,7 @@ fake_repl() do stdin_write, stdout_read, repl
Base.wait(repltask)
end

ccall(:jl_exit_on_sigint, Cvoid, (Cint,), 1)
Base.exit_on_sigint(true)

let exename = Base.julia_cmd()
# Test REPL in dumb mode
Expand Down
4 changes: 2 additions & 2 deletions test/stress.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ end # !Sys.iswindows

# sig 2 is SIGINT per the POSIX.1-1990 standard
if !Sys.iswindows()
ccall(:jl_exit_on_sigint, Cvoid, (Cint,), 0)
Base.exit_on_sigint(false)
@test_throws InterruptException begin
ccall(:kill, Cvoid, (Cint, Cint,), getpid(), 2)
for i in 1:10
Libc.systemsleep(0.1)
ccall(:jl_gc_safepoint, Cvoid, ()) # wait for SIGINT to arrive
end
end
ccall(:jl_exit_on_sigint, Cvoid, (Cint,), 1)
Base.exit_on_sigint(true)
end