FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-145376: Fix various reference leaks objects and modules by eendebakpt · Pull Request #145385 · python/cpython · GitHub

Repository navigation

gh-145376: Fix various reference leaks objects and modules - #145385

Merged
vstinner merged 10 commits into
python:mainfrom
eendebakpt:refleaks
Mar 9, 2026
Merged

vstinner merged 10 commits into
python:mainfrom
eendebakpt:refleaks

Conversation

eendebakpt commented Mar 1, 2026 •
edited by bedevere-app Bot
Loading

Copy link
Copy Markdown
Contributor

eendebakpt requested a review from rhettinger as a code owner March 1, 2026 10:09
eendebakpt marked this pull request as draft March 1, 2026 10:10
eendebakpt marked this pull request as ready for review March 1, 2026 10:28

Copy link
Copy Markdown
Member

Is this going to be a competition between Claude and Codex now 😆?

eendebakpt commented Mar 1, 2026 •
edited
Loading

Copy link
Copy Markdown
Contributor Author

Is this going to be a competition between Claude and Codex now 😆?

As long as the competition is on quality and not quantity we should be fine :-)

Update: I checked import.c which was changed in the PR by @JelleZijlstra. Claude found 4 (potential) issues, including the one in the other PR.

eendebakpt changed the title gh-145376: Fix various reference leaks gh-145376: Fix various reference leaks objects and modules Mar 1, 2026

encukou commented Mar 3, 2026

Copy link
Copy Markdown
Member

It defintely shouldn't be on quantity of PRs. Could you combine these rather than send several PRs at once?

Copy link
Copy Markdown
Contributor Author

It defintely shouldn't be on quantity of PRs. Could you combine these rather than send several PRs at once?

I know some other core devs strongly prefer to keep PRs small and isolated. For that reason I created more PRs targeting only a single module at a time. To give an idea: analyzing the Modules folder resulted in 60 potential bugs in 30 files. I estimate that about 20 of them are false positives, or tricky cases. With more prompting to the LLM additional potential issues might be found.

Let me know how you would like to group the remaining cases.

encukou commented Mar 4, 2026

Copy link
Copy Markdown
Member

IMO, you should limit the number of similar PRs you have “in flight”, so if a common issue is found in one of them1 you don't need to update all the others. And to focus the review in one place.
Queue up PRs on your side, or combine similar changes to multiple files, or even send drafts :)


Footnotes

  1. for example: What needs a news entry? Or even: how much should you combine the PRs? ↩

Comment thread Modules/itertoolsmodule.c Outdated
Comment thread Objects/enumobject.c Outdated
Comment thread Modules/itertoolsmodule.c
Comment on lines 3531 to 3553
static PyObject *
count_nextlong(countobject *lz)
{
PyObject *long_cnt;
PyObject *stepped_up;

long_cnt = lz->long_cnt;
if (long_cnt == NULL) {
/* Switch to slow_mode */
long_cnt = PyLong_FromSsize_t(PY_SSIZE_T_MAX);
if (long_cnt == NULL)
if (long_cnt == NULL) {
return NULL;
}
lz->long_cnt = long_cnt;
}
assert(lz->cnt == PY_SSIZE_T_MAX && long_cnt != NULL);

stepped_up = PyNumber_Add(long_cnt, lz->long_step);
if (stepped_up == NULL)
return NULL;
lz->long_cnt = stepped_up;
return long_cnt;
}

encukou Mar 4, 2026 •
edited
Loading

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Is this the way to make it less confusing?

static PyObject *
count_nextlong(countobject *lz)
{
    if (lz->long_cnt == NULL) {
        /* Switch to slow_mode */
        lz->long_cnt = PyLong_FromSsize_t(PY_SSIZE_T_MAX);
        if (lz->long_cnt == NULL) {
            return NULL;
        }
    }
    assert(lz->cnt == PY_SSIZE_T_MAX && lz->long_cnt != NULL);

    // We hold one reference to "result" (a.k.a. the old value of
    // lz->long_cnt); we'll either return it or keep it in lz->long_cnt.
    PyObject *result = lz->long_cnt;

    PyObject *stepped_up = PyNumber_Add(result, lz->long_step);
    if (stepped_up == NULL) {
        return NULL;
    }
    lz->long_cnt = stepped_up;

    return result;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Yeah, this looks less confusing.

Comment thread Modules/itertoolsmodule.c Outdated
@@ -0,0 +1 @@
Fix reference leaks in various unusual error scenarios.

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I don't think that a NEWS entry is needed since it's unlikely to hit the issue, users are not directly impacted.

vstinner commented Mar 4, 2026

Copy link
Copy Markdown
Member

The current count_nextlong() code doesn't leak at if (long_cnt == NULL) return NULL; but it does leak long_cnt at if (stepped_up == NULL) return NULL;.

I like @encukou's code. It's more readable and doesn't leak on error.

Comment thread Objects/enumobject.c
Comment thread Objects/enumobject.c Outdated

vstinner left a comment

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

LGTM

vstinner merged commit 886bc6e into python:main Mar 9, 2026
48 checks passed

vstinner commented Mar 9, 2026 •
edited
Loading

Copy link
Copy Markdown
Member

Merged. Thanks for the fixes!

ljfp pushed a commit to ljfp/cpython that referenced this pull request Apr 25, 2026
…python#145385)

Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants


Back | FazBrowse Home | New Git URL