Skip to content

Commit e4ce996

Browse files
authored
Improve handling of SIGINT interrupts (#621)
1 parent eb6a66f commit e4ce996

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

src/MOI_wrapper/MOI_callbacks.jl

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,15 @@ function _gurobi_callback_wrapper(
3232
cb_where::Cint,
3333
p_user_data::Ptr{Cvoid},
3434
)
35-
user_data = unsafe_pointer_to_objref(p_user_data)::_CallbackUserData
3635
try
37-
user_data.callback(
38-
CallbackData(user_data.model, cb_data, cb_where),
39-
cb_where,
40-
)
36+
reenable_sigint() do
37+
user_data = unsafe_pointer_to_objref(p_user_data)::_CallbackUserData
38+
user_data.callback(
39+
CallbackData(user_data.model, cb_data, cb_where),
40+
cb_where,
41+
)
42+
return
43+
end
4144
catch ex
4245
GRBterminate(p_model)
4346
if !(ex isa InterruptException)

src/MOI_wrapper/MOI_wrapper.jl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2791,13 +2791,14 @@ function MOI.optimize!(model::Optimizer)
27912791
# Catch [CTRL+C], even when Julia is run from a script not in interactive
27922792
# mode. If `true`, then a script would call `atexit` without throwing the
27932793
# `InterruptException`. `false` is the default in interactive mode.
2794-
#
2795-
# TODO(odow): Julia 1.5 exposes `Base.exit_on_sigint(::Bool)`.
2796-
ccall(:jl_exit_on_sigint, Cvoid, (Cint,), false)
2797-
model.ret_GRBoptimize = GRBoptimize(model)
2794+
Base.exit_on_sigint(false)
2795+
disable_sigint() do
2796+
model.ret_GRBoptimize = GRBoptimize(model)
2797+
return
2798+
end
27982799
_check_ret_GRBoptimize(model)
27992800
if !isinteractive()
2800-
ccall(:jl_exit_on_sigint, Cvoid, (Cint,), true)
2801+
Base.exit_on_sigint(true)
28012802
end
28022803

28032804
# Post-optimize caching to speed up the checks in VariablePrimal and

0 commit comments

Comments
 (0)