Skip to content

gh-153210: Do not DECREF ArrayType in array_modexc when errors occur#153239

Closed
stestagg wants to merge 3 commits into
python:mainfrom
stestagg:gh-153210-2
Closed

gh-153210: Do not DECREF ArrayType in array_modexc when errors occur#153239
stestagg wants to merge 3 commits into
python:mainfrom
stestagg:gh-153210-2

Conversation

@stestagg

@stestagg stestagg commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

ArrayType is initialized directly on the array module state struct.

This means that the reference ownership of ArrayType belongs to the state, NOT the local function.

In two error cases, ArrayType was being DECREF'd by the function. Both of these DECREFS unbalanced the refcount because the ArrayType pointer is left populated on the module state. When the module is eventually cleaned up (probably by GC), then ArrayType is DECREF'd again.

This change just removes the DECREF's and relies on the module tp_clear to walk the deps and clean up the state for us.

Note, the test only catches this issue in a debug build. I don't know enough to know if there's a better way to write this test, but because ArrayType is a Type, reasoning about specific refcount numbers is hard, this approach should be robust.

stestagg added 2 commits July 7, 2026 00:22
… initialized on the array mod state, so GC / module cleanup will do this. (Ownership belongs to the state struct)
@stestagg

stestagg commented Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

@sobolevn made a PR at the same time, but theirs is more comprehensive!

@stestagg stestagg closed this Jul 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant