Skip to content

Dense mat keep alive - #313

Merged
filoferra merged 2 commits into
JuliaParallel:mainfrom
filoferra:dense-mat-keep-alive
Oct 19, 2026
Merged

filoferra merged 2 commits into
JuliaParallel:mainfrom
filoferra:dense-mat-keep-alive

Conversation

@filoferra

Copy link
Copy Markdown
Collaborator

Fix two lifetime bugs where an exception or a garbage collection broke an object's lifetime rules

  • A dense matrix made from a Julia array (PetscMat(petsclib, A::Matrix), or PetscMat(petsclib, m, n; type = :dense)) could lose its storage while a KSP still used it. Once the Julia handle was collected, the next solve read freed memory, which gave a wrong answer or a zero-pivot error. The array now lives as long as the PETSc matrix, as it already did for CSR matrices and vectors on Julia arrays.
  • The DM constructors and fe_create_default left their options pushed on PETSc's options stack when PETSc threw. Later objects read those options, and destroying them failed. They're now popped in every case.

A dense matrix made from a Julia array kept that array alive only as long as its Julia handle. A KSP holds the matrix past that point, so once the handle was collected the next solve read freed memory: a wrong answer, or a PETSc zero-pivot error.

PetscMat(petsclib, A::Matrix) now keeps the array with the PETSc object, as CSR matrices and vectors on Julia arrays already did.
PetscMat(petsclib, m, n; type = :dense) lets PETSc allocate the storage, so no Julia array is involved.
The DMDA, DMStag and DMPlex constructors and fe_create_default pushed their options onto PETSc's options stack around a PETSc call and popped them afterwards, but not when the call threw. The options then stayed on the stack: every object created afterwards read them, and they could no longer be destroyed.

The options are now popped whether or not the call throws.

Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
@filoferra
filoferra merged commit 1faddd8 into JuliaParallel:main Oct 19, 2026
5 checks passed
@filoferra
filoferra deleted the dense-mat-keep-alive branch October 19, 2026 13:16
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.

1 participant