Skip to content

A closed __namespace__ stream subclass in a reference cycle is never freed #186

Backlog

Description

@jhonabreul

What happens

With the fix for #169 (#176), an instance of a __namespace__ subclass is never freed when all of these are true:

  • its .NET base class suppresses its own finalizer on Close() or Dispose(), as MemoryStream does
  • the instance is part of a Python reference cycle, such as s.me = s
  • it is closed and then dropped

The fix for #169 gives the .NET object one reference to the Python object, and the generated .NET finalizer releases it. A suppressed finalizer never runs, so the reference is never released and everything the instance holds stays alive.

This does not happen on master before #176.

Reproduction

From the review of #176: #176 (comment)

class S(MemoryStream) with __namespace__, 10 instances with s.me = s and s.payload = Payload() (a plain Python class). Call s.Close() and drop each one. After running both collectors and draining the finalizer queue:

payloads alive streams alive
master before #176 0 0
with #176 10 10

Without Close(), both give 0.

Related

Unreferenced cycles of __namespace__ instances now wait for a .NET GC before they are freed. In the same review, 6,000 throwaway self-cycles holding 64 KiB each retained about 395 MB with #176 and about 8 MB on master, with no .NET GC on either. Both dropped to the same level once both collectors ran.

Priority

Low. It needs a __namespace__ subclass of a disposable .NET type that is kept in a reference cycle, which is not code algorithm authors normally write. It was left out of #176 on purpose to keep that fix within the scope of #169, and it is listed there as a known limit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions