Skip to content

Free registry callback after capsule construction errors#7

Open
jeet-dekivadia wants to merge 1 commit into
hudson-trading:mainfrom
jeet-dekivadia:free-registry-callback-on-capsule-error
Open

Free registry callback after capsule construction errors#7
jeet-dekivadia wants to merge 1 commit into
hudson-trading:mainfrom
jeet-dekivadia:free-registry-callback-on-capsule-error

Conversation

@jeet-dekivadia

@jeet-dekivadia jeet-dekivadia commented May 31, 2026

Copy link
Copy Markdown

Fixes #6.

pymb_get_registry() allocates the registry and its weakref callback definition separately. If PyCapsule_New() then fails, the error path freed the registry struct directly and skipped the callback allocation.

Route that path through the existing pymb_registry_free() helper. It releases the callback definition and the registry together, matching the capsule destructor path without changing successful initialization.

Validation:

  • git diff --check
  • printf '#include <Python.h>\n#include <stdint.h>\n#include <stdlib.h>\n#include "pymetabind.h"\n' | cc -x c -fsyntax-only - $(python3-config --includes)
  • private Linux smoke extension: compiled, imported, and called pymb_get_registry() twice on CPython 3.9 and 3.13
  • focused Linux failure harness: forced PyCapsule_New() to fail after registry setup and observed 2 tracked header allocations and 2 frees

Signed-off-by: Jeet Dekivadia <jeet.university@gmail.com>
@jeet-dekivadia

Copy link
Copy Markdown
Author

Small follow-up for review context. The failure path is narrowly scoped: registry setup allocates the weakref callback definition separately, then PyCapsule_New can fail while constructing the registry capsule. This patch sends that error path through the existing registry cleanup helper so the callback allocation is released with the rest of the partially built registry.

Validation from preparation: git diff --check; a Python.h/pymetabind.h syntax probe with python3-config includes; a compiled smoke extension imported on CPython 3.9 and 3.13 and called pymb_get_registry twice; and a focused forced PyCapsule_New failure harness observed the tracked header allocations and frees balance at 2 and 2.

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.

pymb_get_registry leaks the weakref callback definition if PyCapsule_New fails

1 participant