Skip to content

Hold the owner of a borrowed GraphBLAS handle in TypedOpBase - #587

Open
eriknw wants to merge 3 commits into
mainfrom
07-selectop-borrowed-handle-lifetime
Open

eriknw wants to merge 3 commits into
mainfrom
07-selectop-borrowed-handle-lifetime

Conversation

@eriknw

@eriknw eriknw commented Aug 4, 2026

Copy link
Copy Markdown
Member

SelectOp._from_indexunary reuses the IndexUnaryOp's GrB_IndexUnaryOp
rather than allocating a second one, and marked the borrowing typed op
_owns_gb_obj_inst = False so that only one side would free it. Nothing
kept the owner alive, though: register_anonymous drops the IndexUnaryOp
on the way out, so its typed op is collected and frees the handle while the
SelectOp is still pointing at it.

The result is a dangling pointer rather than a NULL one. __del__ frees
through a synthesized cell, ffi.new(f"{c_type_name}*", gb_obj), so
GrB_*_free clears that temporary and leaves the op's own gb_obj holding
the old address.

Reproduced at 6f1eb02:

def _ne_thunk(x, i, j, thunk):
    return x != thunk
sel = SelectOp.register_anonymous(_ne_thunk)
gc.collect()
Vector.from_coo([0, 1, 2], [1, 5, 9]).select(sel, 5).new()
-> UninitializedObject

Whether it raises depends on whether anything happens to retain the
IndexUnaryOp. With x > thunk it survives, because a traceback caught
inside the per-dtype compile loop keeps a frame that references it.

Replace the per-instance opt-out with _gb_obj_owner, naming the owning
typed op. That suppresses our free and keeps the handle alive for as long
as we can reach it; disclaiming ownership without naming an owner is what
left the handle dangling. The opt-out being replaced was added in 6f1eb02
to stop a double free, so the sequence is leak, double free, opt-out,
dangling handle, and now an owned reference.


Stack created with GitHub Stacks CLIGive Feedback 💬

@eriknw
eriknw marked this pull request as ready for review August 4, 2026 16:07
@eriknw
eriknw force-pushed the 07-selectop-borrowed-handle-lifetime branch from 5c6a883 to 62aa452 Compare August 4, 2026 16:12
@eriknw
eriknw force-pushed the 07-selectop-borrowed-handle-lifetime branch 2 times, most recently from 011d167 to bdfc46d Compare August 5, 2026 03:18
@eriknw
eriknw force-pushed the 07-selectop-borrowed-handle-lifetime branch from bdfc46d to 99a59d4 Compare August 5, 2026 17:44
@eriknw
eriknw force-pushed the 07-selectop-borrowed-handle-lifetime branch from 99a59d4 to 67a9305 Compare August 5, 2026 18:03
Base automatically changed from 06-numpy-monoid-identities to main August 5, 2026 18:05
@eriknw
eriknw force-pushed the 07-selectop-borrowed-handle-lifetime branch from 67a9305 to 1c41f2b Compare August 5, 2026 18:05
@eriknw
eriknw changed the base branch from main to 42-arm-pickle3-numpy1-skip August 6, 2026 07:58
@eriknw
eriknw force-pushed the 07-selectop-borrowed-handle-lifetime branch from 1c41f2b to cfb2cb7 Compare August 6, 2026 07:59
@eriknw
eriknw force-pushed the 07-selectop-borrowed-handle-lifetime branch from cfb2cb7 to 886374b Compare August 6, 2026 15:39
@eriknw
eriknw force-pushed the 07-selectop-borrowed-handle-lifetime branch from 886374b to 0eff3f4 Compare August 6, 2026 15:41
@eriknw
eriknw changed the base branch from 42-arm-pickle3-numpy1-skip to 43-ci-local-rng August 6, 2026 20:35
@eriknw
eriknw force-pushed the 07-selectop-borrowed-handle-lifetime branch from 0eff3f4 to e1d701a Compare August 6, 2026 20:36
@eriknw
eriknw force-pushed the 07-selectop-borrowed-handle-lifetime branch from e1d701a to 092ab62 Compare August 6, 2026 20:42
@eriknw
eriknw force-pushed the 07-selectop-borrowed-handle-lifetime branch from 092ab62 to 252c82c Compare August 7, 2026 02:48
Base automatically changed from 43-ci-local-rng to main August 7, 2026 05:09
@eriknw
eriknw force-pushed the 07-selectop-borrowed-handle-lifetime branch from 252c82c to 22af6b1 Compare August 7, 2026 05:09
eriknw added 3 commits August 26, 2026 12:29
…ibrary

10.5.0 stopped checking, after a build, whether every value happened to be
the same: "GrB_Matrix_build and GrB_Vector_build: no longer do a post-iso
check; they leave the matrix in non-iso format even if all the entries are
the same." Four tests read that check as if it were python-graphblas's own
contract and went red. Each one wanted an iso object rather than an
inference, so each now asks for one with ``ss.build_scalar``, which means
the same thing on every SuiteSparse version and produces the same storage
formats the tests were selecting for. ``from_coo`` is left alone: adding a
scan of the values to recover the old behavior would re-add the cost
upstream just removed.

The version gates had a second, quieter problem. ``graphblas.core.ss``
derived the C library version by parsing ``suitesparse_graphblas.__version__``,
which is the version of the Python wrapper. The two normally agree, but a
development build between releases reports the previous one: psg
10.4.1.0 against a 10.5.0 library here, so every gate saw 10.4.1 and any
10.5 branch would have been silently skipped. Read the library's own
GxB_IMPLEMENTATION_* constants instead, falling back to the old parse where
a build does not expose them.

test_openmp_enabled keeps its hard assertion and gains a message saying what
is wrong and that the fault is in the installed build. It caught a real one:
conda-forge's first graphblas 10.5.0 build on osx-arm64 shipped without
OpenMP. The build 1 rebuild (2026-08-26) restored OpenMP and the assertion
passes again.
The pool stopped at psg 10.3.1.0, so CI installed nothing newer except on
the draw that picks "latest", which does not pin and so never produces a
reproducible combination. Three releases have landed since, and each carries
a SuiteSparse:GraphBLAS this project has to work against: 10.4.0 moved a
field in the matrix struct, and 10.5.0 dropped the post-build iso check that
four tests used to rely on.

psg 10.5.0.0 is on PyPI and, since 2026-08-26, on conda-forge, so the
conda-forge, wheel and source draws all cover it.

`scripts/check_versions.sh` tracks the newest version we test, so its psg
floor moves with the pool.

Only the psg pools change here. The other pools are revised further up this
branch stack, so raising them belongs there rather than in a commit that
would conflict with every rebase.
`SelectOp._from_indexunary` reuses the IndexUnaryOp's `GrB_IndexUnaryOp`
rather than allocating a second one, and marked the borrowing typed op
`_owns_gb_obj_inst = False` so that only one side would free it. Nothing
kept the owner alive, though: `register_anonymous` drops the IndexUnaryOp
on the way out, so its typed op is collected and frees the handle while the
SelectOp is still pointing at it.

The result is a dangling pointer rather than a NULL one. `__del__` frees
through a synthesized cell, `ffi.new(f"{c_type_name}*", gb_obj)`, so
`GrB_*_free` clears that temporary and leaves the op's own `gb_obj` holding
the old address.

Reproduced at 6f1eb02:

    def _ne_thunk(x, i, j, thunk):
        return x != thunk
    sel = SelectOp.register_anonymous(_ne_thunk)
    gc.collect()
    Vector.from_coo([0, 1, 2], [1, 5, 9]).select(sel, 5).new()
    -> UninitializedObject

Whether it raises depends on whether anything happens to retain the
IndexUnaryOp. With `x > thunk` it survives, because a traceback caught
inside the per-dtype compile loop keeps a frame that references it.

Replace the per-instance opt-out with `_gb_obj_owner`, naming the owning
typed op. That suppresses our free and keeps the handle alive for as long
as we can reach it; disclaiming ownership without naming an owner is what
left the handle dangling. The opt-out being replaced was added in 6f1eb02
to stop a double free, so the sequence is leak, double free, opt-out,
dangling handle, and now an owned reference.
@eriknw
eriknw force-pushed the 07-selectop-borrowed-handle-lifetime branch from 22af6b1 to 4f0dabb Compare August 26, 2026 17:30
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