Skip to content

gh-162412: Fix a ctypes callback argument reference leak on Windows - #162413

Open
manabeai wants to merge 4 commits into
python:mainfrom
manabeai:gh-162412-ctypes-callback-leak
Open

manabeai wants to merge 4 commits into
python:mainfrom
manabeai:gh-162412-ctypes-callback-leak

Conversation

@manabeai

@manabeai manabeai commented Nov 22, 2026 •

Copy link
Copy Markdown

Fixes #162412.

Release the current callback argument when TryAddRef() fails on Windows. The shared cleanup loop releases only the arguments converted before the failure.

The Windows regression test checks that the callback is skipped and that the temporary arguments are released. The test method failed at the weak-reference assertion on stock CPython 3.14.10 (Windows x64) and passed on a local 3.14.10 build with the fix. The standalone probe reported 128 retained arguments before the fix and zero after it.

@python-cla-bot

python-cla-bot Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-22T03:30:05.449464Z 7d58fb9 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7d58fb9782

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread Modules/_ctypes/callbacks.c Resolved
Comment on lines +144 to +146
@unittest.skipUnless(sys.platform == 'win32',
'COM callback argument handling is Windows-only')
def test_argument_conversion_error_does_not_leak(self):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please remove this. Tests that check for leaks are often fragile, complicated, and not very useful in practice (because it's unlikely that an exact code path will leak again).

Comment on lines 85 to 87
static int
TryAddRef(PyObject *cnv, CDataObject *obj)
NeedsComAddRef(PyObject *cnv)
{

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why is this PR doing extra refactoring? The proposed fix in the issue (adding a single Py_DECREF) seems fine.

@bedevere-app

bedevere-app Bot commented Nov 22, 2026

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@ZeroIntensity ZeroIntensity added the AI-needs-explanation AI-assisted work that needs further explanation and follow up from the contributor label Nov 22, 2026
@bedevere-app

bedevere-app Bot commented Nov 22, 2026

Copy link
Copy Markdown

Thank you for your interest in contributing to Python. This is a community-driven project, and we want it to be a welcoming place to learn and collaborate.

Contributors are responsible for reviewing and understanding what they submit, checking its claims, and being able to explain changes in their own words. This helps reviewers focus on the contribution and leaves more time to support other contributors.

This contribution appears to have been authored with AI assistance. Please explain your contribution in your own words and describe how you checked its correctness.

Please read our guidelines for using AI tools.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI-needs-explanation AI-assisted work that needs further explanation and follow up from the contributor awaiting changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants