Skip to content

socketmodule.c: Reference and buffer leaks via audit hook failures #146245

@aisk

Description

@aisk

Bug report

Bug description:

Summary

Two leak bugs in socketmodule.c triggered when PySys_Audit raises:

  1. getaddrinfo (line 6983): idna and/or pstr refs leaked when audit hook raises. ~657 objects leaked per 1000 calls.
  2. sock_sendto (line 4810): pbuf Py_buffer not released when audit hook raises.

Reproducer (getaddrinfo leak)

import socket, sys

sys.addaudithook(lambda *a: (_ for _ in ()).throw(RuntimeError("audit")))
before = sys.gettotalrefcount()
for i in range(1000):
    try:
        socket.getaddrinfo("localhost", 80)
    except RuntimeError:
        pass
after = sys.gettotalrefcount()
print(f"Leaked {after - before} objects in 1000 calls")

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Linked PRs

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