Repository navigation
Commit 294da5e
Keep a __namespace__ subclass's Python state while .NET holds it in a cycle (#176)
* Keep a __namespace__ subclass's Python state while .NET holds it in a cycle
The cyclic GC can't see .NET's reference to a __namespace__ subclass
instance, so it cleared the instance's cycle, its __dict__ included, while
.NET still held the object. The type's tp_finalize now hands the object
to its .NET object instead: the .NET object keeps one reference to it, and
the Python object holds the .NET object weakly. Python takes that
reference back when the object returns to Python, and the .NET object
releases it when it's finalized, after which Python frees the cycle.
The wrapper a Python object holds weakly is now kept alive as long as its
.NET object, so the object still converts back to .NET after a .NET GC.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Keep the .NET object of a __namespace__ subclass that Python resurrects, and support Python 3.7
When another object's __del__ gave Python back an instance the cycle GC had handed to .NET, its
.NET object could be collected while Python still used it. The finalized .NET object now stays
reachable until the queued finalization runs, which gives the Python object a strong handle to it
again when Python still references it.
On Python 3.7, the GC's finalized flag is in gc_refs, and tp_finalize only runs for types with
Py_TPFLAGS_HAVE_FINALIZE.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Run a __namespace__ subclass's base finalizer once, and keep it when .NET gets it back
Deferring the .NET finalization of an object Python may still reference,
instead of re-registering an already finalized object, runs the base type's
finalizer once. The finalizer decides atomically whether the cleanup is
deferred, so .NET getting the object back before the queued cleanup runs
no longer frees a live object.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Run a __namespace__ subclass's base finalizer while the finalizer is disabled, and drop Python 3.7 code
While Finalizer.Instance.Enable is false, the deferred finalization isn't
queued, so nothing would finalize the object again: run the base type's
finalizer then, as master does. Python 3.7 isn't supported, so the finalized
flag is always bit 0 of _gc_prev, and CLR types no longer set
Py_TPFLAGS_HAVE_FINALIZE.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Release a __namespace__ subclass's cycle when .NET collects it unfinalized, and keep its state across .NET owners
A closed stream's .NET finalizer is suppressed, so the reference its .NET
object kept in tp_finalize was never released: a finalizable object tied to
the .NET object now releases it once .NET collects that object, and gives the
.NET object back to Python if Python still references it. Passing the object
to .NET again keeps its cycle from being cleared, and __del__ can be assigned
after the class is defined again.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Keep only the __del__ assignment and .NET owner handoff fixes
As decided on the pull request, the closed stream case is left for #186:
this removes the finalizable object that released a .NET object's Python
reference when .NET collected it unfinalized, and the deferred finalizer's
re-registration goes back to the queued cleanup.
Assigning __del__ is allowed only over the wrapper a __namespace__ class
declares, since its tp_finalize adds it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Assign the subclass's __del__ first in the __del__ assignment test
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---------
Co-authored-by: quantconnect-kronos[bot] <346399058+quantconnect-kronos[bot]@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>1 parent aa64d2a commit 294da5e
8 files changed
Lines changed: 610 additions & 17 deletions
File tree
- src/runtime
- Native
- Types
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
552 | 552 | | |
553 | 553 | | |
554 | 554 | | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
555 | 559 | | |
556 | 560 | | |
557 | 561 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
154 | 154 | | |
155 | 155 | | |
156 | 156 | | |
157 | | - | |
| 157 | + | |
| 158 | + | |
158 | 159 | | |
159 | 160 | | |
160 | 161 | | |
161 | 162 | | |
162 | 163 | | |
163 | 164 | | |
164 | | - | |
| 165 | + | |
165 | 166 | | |
166 | 167 | | |
167 | | - | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
168 | 172 | | |
169 | 173 | | |
| 174 | + | |
170 | 175 | | |
171 | 176 | | |
172 | 177 | | |
| |||
254 | 259 | | |
255 | 260 | | |
256 | 261 | | |
257 | | - | |
| 262 | + | |
258 | 263 | | |
259 | 264 | | |
260 | 265 | | |
| |||
367 | 372 | | |
368 | 373 | | |
369 | 374 | | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
370 | 383 | | |
371 | 384 | | |
372 | 385 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
188 | 188 | | |
189 | 189 | | |
190 | 190 | | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
191 | 194 | | |
192 | 195 | | |
193 | 196 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
52 | 52 | | |
53 | 53 | | |
54 | 54 | | |
| 55 | + | |
55 | 56 | | |
56 | 57 | | |
57 | 58 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
59 | 59 | | |
60 | 60 | | |
61 | 61 | | |
| 62 | + | |
62 | 63 | | |
63 | 64 | | |
64 | 65 | | |
| |||
0 commit comments