Skip to content

_remote_debugging.RemoteUnwinder leaks resources when __init__() is called more than once #156876

Description

@liuzhijie-0614

Bug report

Bug description:

_remote_debugging.RemoteUnwinder.__init__() can be called explicitly on an
already initialized instance. The implementation assumes that initialization
only happens once and overwrites owned resources without releasing them first.

A minimal reproducer:

import gc
import os
import tracemalloc

from _remote_debugging import RemoteUnwinder

tracemalloc.start()

unwinder = RemoteUnwinder(os.getpid(), cache_frames=True)
gc.collect()
before = tracemalloc.get_traced_memory()[0]

for _ in range(100):
    unwinder.__init__(os.getpid(), cache_frames=True)

del unwinder
gc.collect()
after = tracemalloc.get_traced_memory()[0]

print(f"before: {before:,}")
print(f"after:  {after:,}")
print(f"growth: {after - before:,}")

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions