Skip to content

Conversation

@LindaSummer
Copy link
Contributor

@LindaSummer LindaSummer commented Dec 13, 2025

Issue

#142495

Proposed Changes

  • Use PyDict_SetDefaultRef to guard the object update and keep existed value.
  • Add a test case for the racing case

Comment

The PyDict_SetDefaultRef would increase the reference count of the setted item, so we must decrement the refrence count on __missing__ created object to avoid memory leak.

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.

Does it need threads to reproduce? Would not the following code be enough to reproduce the issue?

        count = 0
        def default_factory():
            nonlocal count
            count += 1
            if count == 1:
                test_dict[key]
            return count

@LindaSummer
Copy link
Contributor Author

Does it need threads to reproduce? Would not the following code be enough to reproduce the issue?

        count = 0
        def default_factory():
            nonlocal count
            count += 1
            if count == 1:
                test_dict[key]
            return count

Hi @serhiy-storchaka ,

Thanks very much for your suggestion! ❤

Yes, this case could be designed simply without threads.

I have updated the test case as suggested.

Best Regards,
Edward

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.

Great! But similar tests usually use a local function with a nonlocal variable.

@LindaSummer
Copy link
Contributor Author

LindaSummer commented Dec 15, 2025

Great! But similar tests usually use a local function with a nonlocal variable.

Hi @serhiy-storchaka ,

Thanks very much for your suggestion! 😊
Got it. I will refactor the test case with nonlocal variables for simplicity.

Best Regards,
Edward

}
return value;
PyObject *result = NULL;
(void)PyDict_SetDefaultRef(op, key, value, &result);
Copy link
Contributor

Choose a reason for hiding this comment

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

If we don't need the return value, could we use PyDict_SetDefault?

Copy link
Member

Choose a reason for hiding this comment

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

PyDict_SetDefault returns a borrowed reference and I find this easier to read (at least the result is know to be a strong reference and we can directly return it).

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.

Does the new test reproduce the issue? It seems that __missing__() is not called recursively in your current code, unlike to the code in my suggestion.

@LindaSummer
Copy link
Contributor Author

LindaSummer commented Dec 16, 2025

Does the new test reproduce the issue? It seems that __missing__() is not called recursively in your current code, unlike to the code in my suggestion.

Hi @serhiy-storchaka ,

Thanks very much for your correction!❤

Sorry for misunderstanding the test case comment.
I have updated the test case.
I use a local copy of count to verify that the first time return value 1 of __missing__ does not overwrite the second time assigned value 2.

Please correct me if I misunderstand.

Best Regards,
Edward

def test_factory_conflict_with_set_value(self):
key = "conflict_test"
count = 0
test_dict = None
Copy link
Member

Choose a reason for hiding this comment

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

Is this needed? test_dict is defined at line 203.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @serhiy-storchaka ,

Thanks for your correction! 😊
I have removed the redundant declaration of test_dict.

Best Regards,
Edward


def default_factory():
nonlocal count
nonlocal test_dict
Copy link
Member

Choose a reason for hiding this comment

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

This is not needed, as the variable is not modified in the function (the variable is bound to the same object, even if its value is changed).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @serhiy-storchaka ,

Got it! 😊
I have removed this line.

Best Regards,
Edward

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. 👍

Thank you for your contribution. In the future, try to use chatbots less (could you use a simple online translator instead?). They create an unpleasant, creepy impression.

@serhiy-storchaka serhiy-storchaka merged commit a043407 into python:main Dec 16, 2025
50 checks passed
@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 16, 2025
@miss-islington-app
Copy link

Thanks @LindaSummer for the PR, and @serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖

@miss-islington-app
Copy link

Thanks @LindaSummer for the PR, and @serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14.
🐍🍒⛏🤖

@miss-islington-app
Copy link

Sorry, @LindaSummer and @serhiy-storchaka, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker a0434075108efe6acdfba34f42545f4d80ac9a5e 3.13

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Dec 16, 2025
…th `__missing__` (pythonGH-142668)

(cherry picked from commit a043407)

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

bedevere-app bot commented Dec 16, 2025

GH-142832 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 Dec 16, 2025
@LindaSummer
Copy link
Contributor Author

LGTM. 👍

Thank you for your contribution. In the future, try to use chatbots less (could you use a simple online translator instead?). They create an unpleasant, creepy impression.

Got it. Thanks for your review and help!
Haha, in fact I didn't use a chatbot and I try to write in email style because I thought it was polite and friendly.
I will improve my wording and avoid fixed patterns, which looks like a bot.

@bedevere-bot
Copy link

⚠️⚠️⚠️ Buildbot failure ⚠️⚠️⚠️

Hi! The buildbot AMD64 Debian root 3.x (tier-1) has failed when building commit a043407.

What do you need to do:

  1. Don't panic.
  2. Check the buildbot page in the devguide if you don't know what the buildbots are or how they work.
  3. Go to the page of the buildbot that failed (https://buildbot.python.org/#/builders/345/builds/12953) and take a look at the build logs.
  4. Check if the failure is related to this commit (a043407) or if it is a false positive.
  5. If the failure is related to this commit, please, reflect that on the issue and make a new Pull Request with a fix.

You can take a look at the buildbot page here:

https://buildbot.python.org/#/builders/345/builds/12953

Summary of the results of the build (if available):

==

Click to see traceback logs
Traceback (most recent call last):
  File "/root/buildarea/3.x.angelico-debian-amd64/build/Lib/test/support/__init__.py", line 935, in gc_collect
    gc.collect()
    ~~~~~~~~~~^^
ResourceWarning: unclosed file <_io.FileIO name=13 mode='wb' closefd=True>

serhiy-storchaka pushed a commit that referenced this pull request Dec 16, 2025
…ith `__missing__` (GH-142668) (GH-142832)

(cherry picked from commit a043407)

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

Labels

needs backport to 3.13 bugs and security fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants