Skip to content

Guard against infinite recursion when NonlinearProblem(prob) makes no progress - #1327

Closed
ChrisRackauckas-Claude wants to merge 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:fix/steadystate-nothing-recursion-guard
Closed

ChrisRackauckas-Claude wants to merge 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:fix/steadystate-nothing-recursion-guard

Conversation

@ChrisRackauckas-Claude

@ChrisRackauckas-Claude ChrisRackauckas-Claude commented Sep 23, 2026 •

Copy link
Copy Markdown
Member

Please ignore until reviewed by @ChrisRackauckas.

Context

This is a companion to a review finding on SciML/SciMLBase.jl#1614. The original bug report was GROUP=SymbolicIndexingInterface red on SciMLBase.jl (CI run https://github.com/SciML/SciMLBase.jl/actions/runs/35839187919): init(ssprob, DynamicSS(Tsit5())) on an MTK-lowered SCC steady-state problem throws FieldError: type SCCNonlinearProblem has no field u0. A reviewer of the first fix attempt (SciMLBase PR #1614, which added NonlinearProblem(prob::SCCNonlinearProblem) = prob) found that identity alone turns that FieldError into an infinite recursion / StackOverflowError on this repo's src/default.jl:59-62:

function SciMLBase.__init(prob::SciMLBase.AbstractSteadyStateProblem, ::Nothing, args...; kwargs...)
    nlprob = SciMLBase.NonlinearProblem(prob)
    return SciMLBase.__init(nlprob, nothing, args...; kwargs...)
end

AbstractSteadyStateProblem is a type alias for AbstractNonlinearProblem, so this method also matches an SCCNonlinearProblem. First call: NonlinearProblem(ssprob) returns the stored SCCNonlinearProblem — correct, and already tested. Second call (recursing on that result): NonlinearProblem(scc) returns scc itself (once SciMLBase's identity ships) — and the method recurses on the same object forever.

The actual CI failure is fixed at its real cause in SciML/SteadyStateDiffEq.jl#173 (adding DynamicSS/SICNM-specific __init there means this default-algorithm path is never reached for that case — confirmed by testing that SteadyStateDiffEq's fix alone, with today's unmodified SciMLBase and this repo, already resolves the reported failure). This PR is defense-in-depth for the general case: any algorithm without its own __init/__solve for a SteadyStateProblem, or a bare init(ssprob)/solve(ssprob) with no algorithm at all, still falls through to this converter.

What changed

Guard both __init and __solve for AbstractSteadyStateProblem with ::Nothing: if NonlinearProblem(prob) returns prob unchanged (no progress was made toward an actual NonlinearProblem), raise a clear ArgumentError instead of recursing.

Verification

This repo's own test suite passes unchanged (GROUP=Core, tail):

     Testing NonlinearSolve tests passed

The guard's nlprob === prob branch is unreachable against the currently-released SciMLBase: NonlinearProblem(::SCCNonlinearProblem) still throws a FieldError one line earlier there, so this PR is a no-op change today (confirmed: adding a @test_throws ArgumentError regression test to test/Core/default_alg_tests__item1.jl and running GROUP=Core against the released SciMLBase reproduces the pre-existing FieldError, not the new ArgumentError — so that test was not committed; it cannot pass until SciMLBase/SciMLBase.jl#1614 merges and releases). Instead, verified by Pkg.develop-ing this branch together with a local checkout of SciMLBase.jl#1614 (Pkg.develop on both) and running:

using SciMLBase, NonlinearSolve
inner_prob = NonlinearProblem((du, u, p) -> (du .= u .^ 2 .- p), [1.0], 4.0)
sccprob = SciMLBase.SCCNonlinearProblem((inner_prob,), (Returns(nothing),))
ode_f = ODEFunction((du, u, p, t) -> (du .= -u .+ p))
ssprob = SteadyStateProblem(ode_f, [1.0], 4.0; lowered_problem = sccprob)
init(ssprob)   # no algorithm at all

Before this PR (SciMLBase's identity + unmodified NonlinearSolve):

Warning: detected a stack overflow; program state may be corrupted, so further execution might be unreliable.
ERROR: LoadError: StackOverflowError:

After this PR (SciMLBase's identity + this guard):

ERROR: LoadError: ArgumentError: No default nonlinear algorithm exists for `SCCNonlinearProblem` here: `SciMLBase.NonlinearProblem` on it returns the problem itself rather than a plain `NonlinearProblem` (e.g. an `SCCNonlinearProblem` lowering), which this default-algorithm conversion cannot solve. Specify an algorithm explicitly.

What I did not verify / anything to push back on

  • No regression test is committed here (see above — it cannot pass until the SciMLBase companion PR merges and releases; a follow-up PR bumping this repo's SciMLBase compat bound and adding the @test_throws ArgumentError test would be the natural next step once that happens).
  • Did not run the full GROUP=Everything/QA suites, only GROUP=Core.
  • This PR has no effect by itself; it only matters paired with Raise a clear ArgumentError converting an SCCNonlinearProblem to NonlinearProblem SciMLBase.jl#1614. Reviewer's call on whether to merge ahead of time (harmless, forward-compatible) or wait.

Runic --check and typos are clean on the changed file (Runic reformatted the throw(ArgumentError(...)) call).

Related

🤖 Generated with Claude Code

https://claude.ai/code/session_01NB3oFTNzGW79UtDt8AyjsM

… progress

SciMLBase.__init/__solve for an AbstractSteadyStateProblem with no algorithm
convert via SciMLBase.NonlinearProblem(prob) and recurse on the result. That
conversion returns some inputs verbatim (a stored NonlinearProblem or
LinearProblem lowering already tested elsewhere, or - once
SciMLBase.NonlinearProblem(::SCCNonlinearProblem) is an identity, see
SciML/SciMLBase.jl#1614 - an SCCNonlinearProblem, which has no u0 field to
rebuild from). Recursing when nlprob === prob makes no progress and previously
stack-overflowed instead of raising a clear error.

This guard is a no-op against the currently released SciMLBase, where
NonlinearProblem(::SCCNonlinearProblem) still throws a FieldError one line
earlier; it only matters once the SciMLBase companion PR ships. See that PR's
body for a full fail-before/pass-after trace using a locally `Pkg.develop`'d
SciMLBase.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude Sonnet <[email protected]>
Agent-Harness: Claude Code 2.1.251 (subagent)
Agent-Model: claude-sonnet
Agent-Session: https://claude.ai/code/session_01NB3oFTNzGW79UtDt8AyjsM
@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member Author

Closing: SciMLBase.jl#1614 was changed to have NonlinearProblem(::SCCNonlinearProblem) raise an ArgumentError instead of returning the same object unchanged. With SciMLBase.NonlinearProblem(prob) never returning prob itself, the nlprob === prob recursion this PR guarded against cannot occur, so the guard is unnecessary.

ChrisRackauckas-Claude pushed a commit to ChrisRackauckas-Claude/SteadyStateDiffEq.jl that referenced this pull request Oct 7, 2026
…inearSolve

DynamicSS and SICNM only had __solve; init(ssprob, DynamicSS(...)) had no
matching __init here, so it fell through to NonlinearSolveBase's generic
fallback, which treats "no algorithm-specific __init exists" the same as "no
algorithm given" and rewrites the call to NonlinearSolve's default-algorithm
converter with ::Nothing in place of the algorithm - silently discarding
DynamicSS/SICNM. For a plain SteadyStateProblem that converter still finds a
NonlinearProblem to solve, so init() "worked" but ignored the requested
algorithm; for a SteadyStateProblem whose lowered_problem is an
SCCNonlinearProblem, the converter's NonlinearProblem(prob) call hits a
FieldError (SCCNonlinearProblem has no u0 field) - the reported CI failure,
init(ssprob, DynamicSS(Tsit5())) in SciMLBase.jl's downstream
SymbolicIndexingInterface test group.

Add SciMLBase.__init(prob::AbstractSteadyStateProblem, alg::DynamicSS/SICNM,
...), mirroring the existing __solve methods (extracted into shared
__dynamicss_ode_setup/__sicnm_ode_setup helpers): for a plain problem it
returns the live ODE/DAE integrator so init/solve!/step! compose normally; for
an SCC lowering there is no single integrator to hand back (blocks solve
sequentially), so it eagerly solves via the existing __solve_scc_lowering and
returns the finished solution, matching how a stored NonlinearProblem/
LinearProblem lowering is already handled verbatim elsewhere in this file.

Being a more specific method than NonlinearSolveBase's generic fallback, this
is picked by ordinary dispatch and the buggy conversion path is never reached
for these two algorithms - independent of SciML/SciMLBase.jl#1614 (the
generic converter itself is fixed separately there and in
SciML/NonlinearSolve.jl#1327).

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude Sonnet <[email protected]>
Agent-Harness: Claude Code 2.1.251 (subagent)
Agent-Model: claude-sonnet
Agent-Session: https://claude.ai/code/session_01NB3oFTNzGW79UtDt8AyjsM
ChrisRackauckas-Claude pushed a commit to ChrisRackauckas-Claude/SteadyStateDiffEq.jl that referenced this pull request Oct 21, 2026
…inearSolve

DynamicSS and SICNM only had __solve; init(ssprob, DynamicSS(...)) had no
matching __init here, so it fell through to NonlinearSolveBase's generic
fallback, which treats "no algorithm-specific __init exists" the same as "no
algorithm given" and rewrites the call to NonlinearSolve's default-algorithm
converter with ::Nothing in place of the algorithm - silently discarding
DynamicSS/SICNM. For a plain SteadyStateProblem that converter still finds a
NonlinearProblem to solve, so init() "worked" but ignored the requested
algorithm; for a SteadyStateProblem whose lowered_problem is an
SCCNonlinearProblem, the converter's NonlinearProblem(prob) call hits a
FieldError (SCCNonlinearProblem has no u0 field) - the reported CI failure,
init(ssprob, DynamicSS(Tsit5())) in SciMLBase.jl's downstream
SymbolicIndexingInterface test group.

Add SciMLBase.__init(prob::AbstractSteadyStateProblem, alg::DynamicSS/SICNM,
...), mirroring the existing __solve methods (extracted into shared
__dynamicss_ode_setup/__sicnm_ode_setup helpers): for a plain problem it
returns the live ODE/DAE integrator so init/solve!/step! compose normally; for
an SCC lowering there is no single integrator to hand back (blocks solve
sequentially), so it eagerly solves via the existing __solve_scc_lowering and
returns the finished solution, matching how a stored NonlinearProblem/
LinearProblem lowering is already handled verbatim elsewhere in this file.

Being a more specific method than NonlinearSolveBase's generic fallback, this
is picked by ordinary dispatch and the buggy conversion path is never reached
for these two algorithms - independent of SciML/SciMLBase.jl#1614 (the
generic converter itself is fixed separately there and in
SciML/NonlinearSolve.jl#1327).

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude Sonnet <[email protected]>
Agent-Harness: Claude Code 2.1.251 (subagent)
Agent-Model: claude-sonnet
Agent-Session: https://claude.ai/code/session_01NB3oFTNzGW79UtDt8AyjsM
ChrisRackauckas added a commit to SciML/SteadyStateDiffEq.jl that referenced this pull request Oct 31, 2026
…inearSolve's default (#173)

* Add __init for DynamicSS/SICNM so init() doesn't fall through to NonlinearSolve

DynamicSS and SICNM only had __solve; init(ssprob, DynamicSS(...)) had no
matching __init here, so it fell through to NonlinearSolveBase's generic
fallback, which treats "no algorithm-specific __init exists" the same as "no
algorithm given" and rewrites the call to NonlinearSolve's default-algorithm
converter with ::Nothing in place of the algorithm - silently discarding
DynamicSS/SICNM. For a plain SteadyStateProblem that converter still finds a
NonlinearProblem to solve, so init() "worked" but ignored the requested
algorithm; for a SteadyStateProblem whose lowered_problem is an
SCCNonlinearProblem, the converter's NonlinearProblem(prob) call hits a
FieldError (SCCNonlinearProblem has no u0 field) - the reported CI failure,
init(ssprob, DynamicSS(Tsit5())) in SciMLBase.jl's downstream
SymbolicIndexingInterface test group.

Add SciMLBase.__init(prob::AbstractSteadyStateProblem, alg::DynamicSS/SICNM,
...), mirroring the existing __solve methods (extracted into shared
__dynamicss_ode_setup/__sicnm_ode_setup helpers): for a plain problem it
returns the live ODE/DAE integrator so init/solve!/step! compose normally; for
an SCC lowering there is no single integrator to hand back (blocks solve
sequentially), so it eagerly solves via the existing __solve_scc_lowering and
returns the finished solution, matching how a stored NonlinearProblem/
LinearProblem lowering is already handled verbatim elsewhere in this file.

Being a more specific method than NonlinearSolveBase's generic fallback, this
is picked by ordinary dispatch and the buggy conversion path is never reached
for these two algorithms - independent of SciML/SciMLBase.jl#1614 (the
generic converter itself is fixed separately there and in
SciML/NonlinearSolve.jl#1327).

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude Sonnet <[email protected]>
Agent-Harness: Claude Code 2.1.251 (subagent)
Agent-Model: claude-sonnet
Agent-Session: https://claude.ai/code/session_01NB3oFTNzGW79UtDt8AyjsM

* Update test comment to match SciMLBase's final ArgumentError behavior

The comment still described SciMLBase's NonlinearProblem(::SCCNonlinearProblem)
conversion as an identity that could recurse forever; it now raises an
ArgumentError, and this repo's new __init methods are more specific than the
default path anyway, so it is never reached.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude Sonnet <[email protected]>
Agent-Harness: Claude Code 2.1.251 (subagent)
Agent-Model: claude-sonnet
Agent-Session: https://claude.ai/code/session_01NB3oFTNzGW79UtDt8AyjsM

* Forward save_idxs in DynamicSS/SICNM init; trim comments

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Agent-Harness: Claude Code (subagent of ci-green head)
Agent-Model: claude-opus-5-5[1m]
Agent-Session: https://claude.ai/code/session_011aK5Xvp9NpdkzKuEwtiMGd

* Return steady-state caches from DynamicSS/SICNM init

`init(prob, ::DynamicSS/SICNM)` returned the raw ODE integrator, so
`solve!` skipped the termination-condition finalization of `solve`:
finite-time nonconvergence reported Success and a protective safe
termination reported Terminated instead of Unstable. For SCC lowerings
`init` returned a finished solution that `solve!` could not accept.

`init` now returns a `SteadyStateODECache` (ODE integrator plus the
setup holding the termination cache) or a `SteadyStateSCCCache`; `solve!`
on either runs the same finalization as `__solve`, and `step!` advances
the ODE integrator. Documents the init/solve! contract, including
SICNM's extended `[y; z]` integrator state.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Agent-Harness: Claude Code (subagent of ci-green head)
Agent-Model: claude-opus-5-5[1m]
Agent-Session: https://claude.ai/code/session_011aK5Xvp9NpdkzKuEwtiMGd

* Restore steady-state cache indexing and reinitialization

Expose symbolic state access and current parameters on DynamicSS/SICNM caches.
Rebuild the integration and termination setup on reinitialization so SICNM
uses the updated parameters and consistent Newton direction. Cover cache
access, repeated solves, termination modes, and solution slicing in Core.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Codex <[email protected]>
Agent-Harness: Codex CLI 0.163.0
Agent-Model: gpt-6.1-sol
Agent-Session: local session 01a157c8-4f1a-7691-afcc-019806096637 (no session URL)

* Allow type-changing steady-state cache reset options

Install rebuilt cache state in one assignment so slicing, ODE-option and
termination-mode overrides can change concrete types. Preserve cache identity
and retain the previous state when initialization fails. Add regressions for
option transitions, retained data, failed rebuilds and subsequent reuse.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Codex <[email protected]>
Agent-Harness: Codex CLI 0.163.0
Agent-Model: gpt-6.1-sol
Agent-Session: local session 01a157c8-4f1a-7691-afcc-019806096637 (no session URL)

* Document that step!/reinit!/indexing apply only to the ODE integrator cache

Review (Grok, grok-4.7): clarify that step!, reinit!, and symbolic indexing
are defined only on SteadyStateODECache; calling any of them on a
SteadyStateSCCCache (the SCC-lowering case) throws MethodError, since only
solve! is supported on it. Confirmed by running both calls directly.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude Sonnet <[email protected]>
Agent-Harness: Claude Code 2.1.251 (subagent)
Agent-Model: claude-sonnet
Agent-Session: https://claude.ai/code/session_01NB3oFTNzGW79UtDt8AyjsM

* Restore cache.ps and symbolic state assignment on SteadyStateODECache

init caches forwarded :ps to a NamedTuple and had getindex without setindex!,
so cache.ps[:a] and cache[:x] = value failed after the wrapper was introduced.
Expose the parameter proxy, assign states through setu, and keep SICNM's
residual and Newton direction consistent with live parameters after mutation.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Cursor Agent <[email protected]>
Agent-Harness: Cursor Agent CLI
Agent-Model: auto
Agent-Session: local fixer job SciML__SteadyStateDiffEq.jl__173 (no session URL)

* Accept u0 as a keyword in SteadyStateODECache reinit!

reinit!(cache; u0 = ..., p = ...) forwarded the keyword u0 through
kwargs to the ODE initializer instead of remake. SICNM then saw an
initial state half the extended [y; z] length and threw
IncompatibleMassMatrixError, while DynamicSS stored u0 among the
persistent initialization options so a later positional reset restarted
from the stale keyword value. Consume the keyword through remake and
keep it out of the stored ODE options.

Fail-before (unfixed head, keyword-then-positional reset probe):
  DynamicSS keyword reset state=[-3.0]
  DynamicSS positional reset state=[-3.0]   (expected [7.0])
  SICNM keyword reset ERROR IncompatibleMassMatrixError
Pass-after:
  DynamicSS keyword reset state=[-3.0]
  DynamicSS positional reset state=[7.0]
  SICNM keyword reset state=[-3.0]
  SICNM positional reset state=[7.0]
  keyword then positional cache reset | Pass 4  Total 4

The new "keyword u0 cache reset" testset in test/cache_interface.jl
covers both algorithms, iip and oop: it fails 22 and errors 4 on the
unfixed source and passes after. GROUP=Core and GROUP=QA pass.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Devin <[email protected]>
Agent-Harness: Devin CLI 3000.11.228
Agent-Model: swe-2-high
Agent-Session: local fixer job SciML__SteadyStateDiffEq.jl__173 (no session URL)

* Defer SICNM direction refresh until batched state writes complete

Track pending direction synchronization in the steady-state cache. Refresh
on state/integrator reads and before stepping or solving, and clear the
flag after synchronization or reinitialization. Integrator notification
uses the public mutation interface.

Add coupled-domain regressions for batched symbolic and direct SII setters,
plus scalar changes after stepping. Document deferred synchronization.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Codex <[email protected]>
Agent-Harness: Codex CLI 0.164.0
Agent-Model: gpt-6.1-sol
Agent-Session: local job 20261028T065010Z-codex-gpt-6.1-sol (no session URL)

---------

Co-authored-by: ChrisRackauckas-Claude <[email protected]>
Co-authored-by: Claude Sonnet <[email protected]>
Co-authored-by: Codex <[email protected]>
Co-authored-by: Cursor Agent <[email protected]>
Co-authored-by: Devin <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants