From 459ef0593243802914eda40ed2859e0a6990980c Mon Sep 17 00:00:00 2001 From: K Pamnany Date: Wed, 4 Oct 2023 16:02:57 -0400 Subject: [PATCH] Add GC-safe region around `Highs_run` Prevent a long-running `Highs_run` call from blocking GC (and freezing the other Julia threads). --- src/MOI_wrapper.jl | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/MOI_wrapper.jl b/src/MOI_wrapper.jl index a7740dd..a45cc69 100644 --- a/src/MOI_wrapper.jl +++ b/src/MOI_wrapper.jl @@ -1812,7 +1812,15 @@ function MOI.optimize!(model::Optimizer) _set_variable_primal_start(model) ret = Highs_zeroAllClocks(model) _check_ret(ret) - ret = Highs_run(model) + + # if `Highs_run` implicitly uses memory or other resources owned by `model`, preserve it + GC.@preserve model begin + # allow Julia to GC while this thread is in `Highs_run` + gc_state = ccall(:jl_gc_safe_enter, Int8, ()) + ret = Highs_run(model) + # leave GC-safe region, waiting for GC to complete if it's running + ccall(:jl_gc_safe_leave, Cvoid, (Int8,), gc_state) + end _store_solution(model, ret) # TODO(odow): resetting the bounds here invalidates previously stored # solutions.