Skip to content

Instantly share code, notes, and snippets.

@devdanzin
Created March 18, 2026 10:07
Show Gist options
  • Select an option

  • Save devdanzin/9f1ce3fece9407aacef0e16cd419ffb2 to your computer and use it in GitHub Desktop.

Select an option

Save devdanzin/9f1ce3fece9407aacef0e16cd419ffb2 to your computer and use it in GitHub Desktop.
socketmodule.c: Reference and buffer leaks via audit hook failures

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

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")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment