Skip to content

Intermittent GC segfault during compilation with SCCNonlinearProblem + custom rule #3026

@ChrisRackauckas-Claude

Description

@ChrisRackauckas-Claude

Description

Enzyme.gradient through SCCNonlinearProblem (from NonlinearSolve.jl) intermittently segfaults during compilation on Julia 1.10. The crash occurs in gc_mark_obj8 during type inference triggered by Enzyme's thunk generator. When it doesn't crash, the gradient is correct.

Approximately 50% crash rate across fresh Julia processes.

MWE

using Enzyme, NonlinearSolve, SCCNonlinearSolve, SciMLSensitivity

function f1(du, u, p); du[1] = u[1]^2 - p[1]; end
function f2(du, u, p); du[1] = u[1] - p[1] * p[2]; end
explicitfun1!(p, sols) = nothing
function explicitfun2!(p, sols); p[1] = sols[1].u[1]; end

alg = SCCNonlinearSolve.SCCAlg(nlalg = NewtonRaphson())

function loss(p_val)
    p1 = copy(p_val); p2 = copy(p_val)
    sccprob = SciMLBase.SCCNonlinearProblem(
        (NonlinearProblem(f1, [1.0], p1), NonlinearProblem(f2, [1.0], p2)),
        (explicitfun1!, explicitfun2!))
    sum(solve(sccprob, alg).u)
end

# Crashes ~50% of runs with signal 11 (Segmentation fault) in gc_mark_obj8
g = Enzyme.gradient(Enzyme.set_runtime_activity(Enzyme.Reverse), loss, [4.0, 3.0])
println(g)  # ([1.0, 2.0],) when it works

Crash trace

[PID] signal (11.1): Segmentation fault
gc_mark_obj8 at julia/src/gc.c:1855
gc_mark_outrefs at julia/src/gc.c:2634
gc_mark_loop_serial_ at julia/src/gc.c:2690
...
typeinf at julia/compiler/typeinfer.jl:216
const_prop_call at julia/compiler/abstractinterpretation.jl:1211
...
thunk_generator at Enzyme/src/compiler.jl:7055

Notes

  • Simple NonlinearProblem (no SCC) does NOT segfault
  • The SCC code path involves a @generated function (iteratively_build_sols) with the NonlinearSolveBase Enzyme rule on solve_up
  • Requires SciMLSensitivity loaded (provides the Enzyme rule via NonlinearSolveBase extension)
  • set_runtime_activity(Reverse) is required; without it, EnzymeRuntimeActivityError
  • When it doesn't crash, gradient is correct: [1.0, 2.0]

Versions

  • Julia 1.10.11
  • Enzyme 0.13.134
  • NonlinearSolve 4.16.0
  • NonlinearSolveBase 2.18.0
  • SCCNonlinearSolve 1.12.0
  • SciMLSensitivity 7.99.0 (dev branch with diff_tunables support)

🤖 Generated with Claude Code

Co-Authored-By: Chris Rackauckas accounts@chrisrackauckas.com

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions