Skip to content

gh-145376: Fix reference leaks in _collectionsmodule.c#145421

Open
eendebakpt wants to merge 1 commit intopython:mainfrom
eendebakpt:refleak_collections
Open

gh-145376: Fix reference leaks in _collectionsmodule.c#145421
eendebakpt wants to merge 1 commit intopython:mainfrom
eendebakpt:refleak_collections

Conversation

@eendebakpt
Copy link
Contributor

@eendebakpt eendebakpt commented Mar 2, 2026

Make deque_append_lock_held and deque_appendleft_lock_held consume the second argument (the methods already consumed the second argument, but not in the error path).

Issues found using Claude.

iternext = *Py_TYPE(it)->tp_iternext;
while ((item = iternext(it)) != NULL) {
if (deque_appendleft_lock_held(deque, item, maxlen) == -1) {
Py_DECREF(item);
Copy link
Contributor

Choose a reason for hiding this comment

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

iternext returns a new reference. Should we leave this line unchanged?

Copy link
Member

Choose a reason for hiding this comment

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

I agree, this is fishy. Does deque_append[left]_lock_held steal item, or not?

Copy link
Member

Choose a reason for hiding this comment

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

With this change, deque_appendleft_lock_held() always steal its item argument. So removing Py_DECREF(item) here is correct.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The deque_appendleft_lock_held indeed steals the second argument reference. I wrote this in the OP, but did not add a comment in the code.
If you agree, I will add a comment to the start of deque_append_lock_held/deque_appendleft_lock_held.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see now, thanks!
Maybe it is worth to add a comment, that deque_appendleft_lock_held steals reference to the second argument?

Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

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

LGTM

iternext = *Py_TYPE(it)->tp_iternext;
while ((item = iternext(it)) != NULL) {
if (deque_appendleft_lock_held(deque, item, maxlen) == -1) {
Py_DECREF(item);
Copy link
Member

Choose a reason for hiding this comment

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

With this change, deque_appendleft_lock_held() always steal its item argument. So removing Py_DECREF(item) here is correct.

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