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

Gurobi.free_env segfaults with low probability #110

Closed
rdeits opened this issue Nov 5, 2017 · 4 comments
Closed

Gurobi.free_env segfaults with low probability #110

rdeits opened this issue Nov 5, 2017 · 4 comments
Labels
Wrapper: C API Issue is specific to low-level C API

Comments

@rdeits
Copy link
Collaborator

rdeits commented Nov 5, 2017

Running the following:

using Gurobi, JuMP

while true
    env = Gurobi.Env()
    m = Model(solver=GurobiSolver(env, OutputFlag=0))
    @variable m x
    @objective m Min x^2
    solve(m)
    Gurobi.free_env(env)
end

causes Julia to segfault after a few seconds with:

*** Error in `julia': corrupted double-linked list: 0x0000000003735d20 ***

gdb shows the following:

*** Error in `/usr/local/bin/julia': corrupted double-linked list: 0x0000000002f025f0 ***

Program received signal SIGABRT, Aborted.
0x00007ffff6d24c37 in __GI_raise (sig=sig@entry=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
56      ../nptl/sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) bt
#0  0x00007ffff6d24c37 in __GI_raise (sig=sig@entry=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
#1  0x00007ffff6d28028 in __GI_abort () at abort.c:89
#2  0x00007ffff6d612a4 in __libc_message (do_abort=do_abort@entry=1, 
    fmt=fmt@entry=0x7ffff6e6f6b0 "*** Error in `%s': %s: 0x%s ***\n") at ../sysdeps/posix/libc_fatal.c:175
#3  0x00007ffff6d6cb02 in malloc_printerr (ptr=0x2f025f0, str=0x7ffff6e6b7e4 "corrupted double-linked list", 
    action=1) at malloc.c:4996
#4  malloc_consolidate (av=av@entry=0x7ffff70ac760 <main_arena>) at malloc.c:4157
#5  0x00007ffff6d6d45d in _int_free (av=0x7ffff70ac760 <main_arena>, p=<optimized out>, have_lock=0)
    at malloc.c:4057
#6  0x00007fffcda0c1be in PRIVATE00000000000620d1 ()
   from /home/rdeits/Applications/gurobi750/linux64/lib/libgurobi75.so
#7  0x00007fffcda0addc in GRBfreemodel () from /home/rdeits/Applications/gurobi750/linux64/lib/libgurobi75.so

I'm running the following Julia version:

julia> versioninfo()
Julia Version 0.6.0
Commit 9036443 (2017-06-19 13:05 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Core(TM) i7-3920XM CPU @ 2.90GHz
  WORD_SIZE: 64
  BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Sandybridge)
  LAPACK: libopenblas64_
  LIBM: libopenlibm
  LLVM: libLLVM-3.9.1 (ORCJIT, ivybridge)

and the following Gurobi version:

julia> Gurobi.version
v"7.5.0"

julia> Pkg.installed("Gurobi")
v"0.3.3+"

I'm not entirely sure that this is a Gurobi.jl issue, but I have noticed that just constructing and freeing environments seems to work fine. That is, the following does not crash:

while true
  env = Gurobi.Env()
  Gurobi.free_env(env)
end
@odow
Copy link
Member

odow commented Mar 22, 2018

I can reproduce this on Windows.

However putting in short sleep seems to fix the problem (at least with sleep(0.1). I still crashed it with sleep(0.01)). Looks like Gurobi is trying to free the env before it has been released from the solve or something.

using Gurobi, JuMP

while true
    env = Gurobi.Env()
    m = Model(solver=GurobiSolver(env, OutputFlag=0))
    @variable m x
    @objective m Min x^2
    solve(m)
    sleep(0.1)
    Gurobi.free_env(env)
end

It just took a really long time.

@rdeits
Copy link
Collaborator Author

rdeits commented Jul 3, 2018

By the way, I'm still getting this from time to time. The only reliable solution has been to never ever free a Gurobi environment.

rdeits added a commit to rdeits/LCPSim.jl that referenced this issue Jul 3, 2018
@mlubin
Copy link
Member

mlubin commented Jul 3, 2018

Related: JuliaLang/julia#3067

@odow odow added the Wrapper: C API Issue is specific to low-level C API label Dec 14, 2018
@odow
Copy link
Member

odow commented Sep 28, 2020

master now implements a finalizer for env. Env will only be freed if all of the models which use it have also been freed.

If you are using Gurobi.Optimizer, never use GRBfreeenv. Instead, use finalize(env).

The current suggested version of the original MWE is:

using Gurobi, JuMP

while true
    env = Gurobi.Env()
    model = Model(() -> Gurobi.Optimizer(env))
    set_silent(model)
    @variable(model, x)
    @objective(model, Min, x^2)
    optimize!(model)
    # To ensure `env` is finalized, you must also finalize any models using env.
    # If you use `Gurobi.Optimizer`, the order of finalizing `.model` and `env` 
    # doesn't matter. If you use the C API directly, `.model` _MUST_ be 
    # finalized first.
    finalize(backend(model).optimizer.model)
    finalize(env)
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Wrapper: C API Issue is specific to low-level C API
Development

No branches or pull requests

3 participants