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.

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.

4 participants