Skip to content

Conversation

@hyongtao-code
Copy link
Contributor

@hyongtao-code hyongtao-code commented Dec 28, 2025

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

This change ensures that buffers are released on failure by clearing the overlapped state, following the same pattern used in other overlapped error paths. This issue is similar in nature to commit 5485085, which fixed related overlapped cleanup problems.

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 without the patch

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

Result with the patch

d:\MyCode\cpython\PCbuild\amd64>python_d.exe py_overlapped_leak.py
  1000: totalrefcount delta = +10
  2000: totalrefcount delta = +10
  3000: totalrefcount delta = +10
  4000: totalrefcount delta = +10
  5000: totalrefcount delta = +10
......
 19000: totalrefcount delta = +10
 20000: totalrefcount delta = +10
done: N=20000, totalrefcount delta = +9

picnixz
picnixz previously approved these changes Dec 28, 2025
Copy link
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll need a NEWS entry for that and some tests as well. Please add them so that we can run them under the refleak bot as well.

cc @zooba

@picnixz picnixz dismissed their stale review December 28, 2025 14:41

wrong button

@picnixz picnixz requested a review from zooba December 28, 2025 14:44
@hyongtao-code
Copy link
Contributor Author

and some tests as well

Thanks for the review. I will add a suitable test case.

Co-authored-by: Bénédikt Tran <[email protected]>
@serhiy-storchaka serhiy-storchaka added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes labels Dec 29, 2025
Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. 👍

@kumaraditya303 kumaraditya303 merged commit 103a384 into python:main Jan 13, 2026
54 checks passed
@miss-islington-app
Copy link

Thanks @hyongtao-code for the PR, and @kumaraditya303 for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jan 13, 2026
…tart on windows (pythonGH-143250)

(cherry picked from commit 103a384)

Co-authored-by: Yongtao Huang <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jan 13, 2026
…tart on windows (pythonGH-143250)

(cherry picked from commit 103a384)

Co-authored-by: Yongtao Huang <[email protected]>
@bedevere-app
Copy link

bedevere-app bot commented Jan 13, 2026

GH-143795 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.14 bugs and security fixes label Jan 13, 2026
@bedevere-app
Copy link

bedevere-app bot commented Jan 13, 2026

GH-143796 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Jan 13, 2026
kumaraditya303 pushed a commit that referenced this pull request Jan 13, 2026
…start on windows (GH-143250) (#143796)

gh-143249: Fix buffer leak when overlapped operation fails to start on windows (GH-143250)
(cherry picked from commit 103a384)

Co-authored-by: Yongtao Huang <[email protected]>
kumaraditya303 pushed a commit that referenced this pull request Jan 13, 2026
…start on windows (GH-143250) (#143795)

gh-143249: Fix buffer leak when overlapped operation fails to start on windows (GH-143250)
(cherry picked from commit 103a384)

Co-authored-by: Yongtao Huang <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants