Repository navigation
feat(mypyc): proper weakref support - #19056
BobTheBuidler wants to merge 27 commits into
Conversation
|
I've added a I have not yet added any logic for setting supports_weakref, it is currently always set == cls.has_dict. I'm thinking to implement one or more of the options listed here: mypyc/mypyc#1102 Something in this PR has broken your serialization tests, it would be helpful if somebody could share some pointers wrt where I should look. |
|
Well... I tried fixing the serialization issue and now I just get segfaults instead. Not sure where to go from here. |
|
Great, the segfaults are fixed and the serialization tests are green Next steps:
Distant todo (maybe?):
|
|
Hell yeah, it works! |
|
TODO: I found this in cpython's docs: The only further addition is that tp_dealloc needs to clear any weak references (by calling PyObject_ClearWeakRefs()) if the field is non-NULL: https://docs.python.org/3.8/extending/newtypes.html#weak-reference-support |
|
I added some logic to the dealloc function as per the python docs above, it fails on all python versions for python 3.8-3.11 it fails due to the if check, and the fact that weakreflist is not a member of the struct. I believe this can either be fixed by adding the member to the struct (difficult, I noticed you have this commented in the code as a #TODO) or looking up the weakref field instead (potentially easier). I am blocked by not knowing how to access the weakref field here, or whether we can even access a python attribute in the dealloc function. for python 3.12 and 3.13 I think it segfaults because we need to #include weakref.h so we can call PyObject_ClearWeakRefs. I'm not sure of this however, and don't know how to emit the #include so I can test my theory. Leaving this PR as-is for now, will come back in a few days and work on it some more. |
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
* Fix weakref support in native classes * Update util.py
`NativeBase` gives a native class `__weakref__`, and its docstring said instances
"can then be weakly referenced" and stopped there. A reader can fairly take that as
"weak references work", and they do not: the compiled deallocator omits
`PyObject_ClearWeakRefs()`, so the reference reads as cleared while its callback
never runs, and anything built on that callback keeps a stale entry.
Measured on a `mypyc` wheel, holding a `Function` in a plain `weakref.WeakSet` and
dropping it:
ref() is None : True <- looks fine
callback fired : False <- but nothing is notified
len(WeakSet) : 1 <- so the entry is never dropped
list(WeakSet) : []
exit status : 139 <- and teardown crashes
Reproduced 3/3 with a plain `WeakSet`, 0/3 through an interpreted handle.
This is mypyc/mypyc#1102; python/mypy#19056 is the fix in progress. `jax.jit`, the
reason beartype#318 wanted weak references at all, is unaffected: it holds the reference
itself rather than asking to be told when the referent dies.
The measurement above stays out of the docstring and lives with
`_function._LiveFunctions`, the one place that exists because of this, so there is
one canonical account rather than two to drift apart.
Docstring only; no behaviour change.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
* docs(mypyc): say what a `NativeBase` weak reference does not do
`NativeBase` gives a native class `__weakref__`, and its docstring said instances
"can then be weakly referenced" and stopped there. A reader can fairly take that as
"weak references work", and they do not: the compiled deallocator omits
`PyObject_ClearWeakRefs()`, so the reference reads as cleared while its callback
never runs, and anything built on that callback keeps a stale entry.
Measured on a `mypyc` wheel, holding a `Function` in a plain `weakref.WeakSet` and
dropping it:
ref() is None : True <- looks fine
callback fired : False <- but nothing is notified
len(WeakSet) : 1 <- so the entry is never dropped
list(WeakSet) : []
exit status : 139 <- and teardown crashes
Reproduced 3/3 with a plain `WeakSet`, 0/3 through an interpreted handle.
This is mypyc/mypyc#1102; python/mypy#19056 is the fix in progress. `jax.jit`, the
reason #318 wanted weak references at all, is unaffected: it holds the reference
itself rather than asking to be told when the referent dies.
The measurement above stays out of the docstring and lives with
`_function._LiveFunctions`, the one place that exists because of this, so there is
one canonical account rather than two to drift apart.
Docstring only; no behaviour change.
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
|
The issue has been fixed by #22387 |
This is the beginning of a PR intended to fix a compatibility issue between mypyc and weakrefs
Fixes mypyc/mypyc#1102