Skip to content

Overlapped I/O leaks buffers when operation fails to start #143249

@hyongtao-code

Description

@hyongtao-code

Bug report

Bug description:

On Windows, some overlapped I/O operations leak buffers when they fail to start, including WSASendTo(), WSARecvFrom(), and WSARecvFromInto().

Python script for tracking leaks

import sys
import _overlapped

def total():
    return sys.gettotalrefcount()

if not hasattr(sys, "gettotalrefcount"):
    raise SystemExit("need a debug build (Py_DEBUG) for gettotalrefcount")

before = total()

N = 20000
STEP = 1000

for i in range(1, N + 1):
    ov = _overlapped.Overlapped()
    buf = memoryview(bytearray(4096))
    try:
        ov.WSASendTo(0x1234, buf, 0, ("127.0.0.1", 1))
    except OSError:
        pass

    if i % STEP == 0:
        now = total()
        print(f"{i:6d}: totalrefcount delta = {now - before:+d}")

after = total()
print(f"done: N={N}, totalrefcount delta = {after - before:+d}")

Result

d:\MyCode\cpython\PCbuild\amd64>python_d.exe py_overlapped_leak.py
  1000: totalrefcount delta = +4007
  2000: totalrefcount delta = +8009
  3000: totalrefcount delta = +11906
  4000: totalrefcount delta = +15906
  5000: totalrefcount delta = +19906
......
 19000: totalrefcount delta = +75906
 20000: totalrefcount delta = +79906
done: N=20000, totalrefcount delta = +79905

CPython versions tested on:

CPython main branch

Operating systems tested on:

Windows

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions