idas: wire NLSstg LSolveFn in IDASetNonlinearSolverSensStg to prevent NULL crash#907
Open
OfficialDelta wants to merge 1 commit intollnl:mainfrom
Open
idas: wire NLSstg LSolveFn in IDASetNonlinearSolverSensStg to prevent NULL crash#907OfficialDelta wants to merge 1 commit intollnl:mainfrom
OfficialDelta wants to merge 1 commit intollnl:mainfrom
Conversation
… NULL crash IDASetNonlinearSolverSensStg() did not set LSolveFn/LSetupFn on the replacement NLSstg. These callbacks were only set by idaNlsInitSensStg() during IDAInitialSetup(). If NLSstg is replaced after IDACalcIC() (which marks ida_SetupDone=TRUE), the next IDASolve() skips IDAInitialSetup() and Newton iterations crash with NULL LSolveFn. Fix: eagerly wire LSolveFn and LSetupFn in IDASetNonlinearSolverSensStg() when the linear solver is already attached.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
IDASetNonlinearSolverSensStg()does not wireLSolveFn(orLSetupFn) on the replacementNLSstg. These callbacks are only set later byidaNlsInitSensStg(), which runs insideIDAInitialSetup(). If the staggered sensitivity NLS is replaced afterIDACalcIC()has already runIDAInitialSetup()(settingida_SetupDone = TRUE), the subsequentIDASolve()call skipsIDAInitialSetup(), and Newton iterations on the newNLSstgdereference a NULLLSolveFn, causing a segmentation fault.Root Cause
IDACalcIC()callsIDAInitialSetup(), which callsidaNlsInitSensStg()— this correctly setsLSolveFn = idaNlsLSolveSensStgonNLSstgand marksida_SetupDone = TRUE.IDASetNonlinearSolverSensStg(ida_mem, NLS_new)to replaceNLSstgwith a freshSUNNonlinSol_NewtonSens. The new NLS hasLSolveFn = NULL.IDASolve()checksida_SetupDoneand skipsIDAInitialSetup()entirely.NLSstgcallsLSolveFn(...)→ NULL pointer dereference → SIGSEGV.The same crash pattern can occur even without explicit user NLS replacement, in any SUNDIALS configuration where
idaNlsInitSensStg()has not yet run when the first Newton iteration fires on the staggered NLS.Fix
Wire
LSolveFnandLSetupFneagerly inIDASetNonlinearSolverSensStg(), immediately after setting up the NLS, if the linear solver is already attached. This mirrors the existing pattern inidaNlsInitSensStg()but ensures the callbacks are present regardless of whetherIDAInitialSetup()runs again.The fix is safe when the linear solver is not yet set (
ida_lsolve == NULL): in that case the early wiring is skipped andidaNlsInitSensStg()handles it during the normal init path.Minimal Reproduction
A 10-species linear chain ODE with staggered FSA (Ns=10) demonstrates the crash:
With the fix applied, the same sequence completes without crash, and FSA gradients match finite-difference gradients with cosine similarity > 0.999.
Testing
Files Changed
src/idas/idas_nls_stg.c: Added earlyLSolveFn/LSetupFnwiring inIDASetNonlinearSolverSensStg()