Skip to content

Conversation

@efimov-mikhail
Copy link
Member

@efimov-mikhail efimov-mikhail commented Oct 24, 2025

@sergey-miryanov
Copy link
Contributor

For the following repro:

d = {}

class X(object):
    def __hash__(self):
        return 1
    def __eq__(self, other):
        d.clear()

d.setdefault(X(), None)
d.setdefault(X(), None)
d.setdefault(X(), None)

assert len(d) == 1
print(d)

It crashes inside dict_setdefault_ref_lock_held -> insertion_resize -> dictresize -> unicode_get_hash

@sergey-miryanov
Copy link
Contributor

For the following repro:

d = {}

class X(object):
    def __hash__(self):
        return 1
    def __eq__(self, other):
        d.clear()

d.setdefault(X(), None)
d.setdefault(X(), None)

assert len(d) == 1
print(d)

It crashes also inside unicode_get_hash but for line print(d).

Both repro tests on Windows 11 on main branch.

…e-140551.-9swrl.rst

Co-authored-by: Sergey Miryanov <sergey.miryanov@gmail.com>
@sergey-miryanov
Copy link
Contributor

Another one I found on main:

d = {}

class X(object):
    def __hash__(self):
        return 1
    def __eq__(self, other):
        d.clear()

d[X()] = None
d[X()] = None
for _ in range(10):
    d.pop(X(), None)

It is aborted with Assertion failed: PyUnicode_CheckExact(ep->me_key), file X:\Work\cpython\main\Objects\dictobject.c, line 1048

@efimov-mikhail
Copy link
Member Author

efimov-mikhail commented Oct 24, 2025

Another one I found on main:

d[X()] = None
d[X()] = None
for _ in range(10):
    d.pop(X(), None)

Yes, it's expected that d is in incorrect state after two assignments.
There're many ways to trigger assertion at that state.
All of them have been fixed with this PR, I suppose.

Copy link
Contributor

@sergey-miryanov sergey-miryanov left a comment

Choose a reason for hiding this comment

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

LGTM

@methane
Copy link
Member

methane commented Oct 26, 2025

I'm not sure if this is the correct solution.

If the keys of a dictionary change during a dict lookup, can we trust the result of the lookup (e.g. ix)?

Shouldn't it raise an error if the keys of a dict change during a lookup?

@vstinner
Copy link
Member

I like this error:

            PyErr_SetString(PyExc_RuntimeError,
                    "dict mutated during update");

@colesbury
Copy link
Contributor

If the keys of a dictionary change during a dict lookup, can we trust the result of the lookup

CPython behavior has been to retry the lookup if the key changes (since Python 2.2, 453163d). I don't think we should change it.

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Copy link
Contributor

@colesbury colesbury left a comment

Choose a reason for hiding this comment

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

LGTM, but I will let @methane review and merge

Copy link
Contributor

@sergey-miryanov sergey-miryanov left a comment

Choose a reason for hiding this comment

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

Looks good to me. I'm not expert, but code looks clear and tests seem actual.

@efimov-mikhail
Copy link
Member Author

efimov-mikhail commented Oct 28, 2025

IIUC, iOS problem in CI is unrelated to this PR, see: #140190 (comment)

@vstinner
Copy link
Member

IIUC, iOS problem in CI is unrelated to this PR, see: #140190 (comment)

I created #140702 for test_UnixDatagramServer() timeout.

@efimov-mikhail
Copy link
Member Author

I created #140702 for test_UnixDatagramServer() timeout.

IIUC, problem is fixed and CI is green.

@kumaraditya303 kumaraditya303 merged commit 02202c1 into python:main Oct 29, 2025
46 checks passed
@miss-islington-app
Copy link

Thanks @efimov-mikhail for the PR, and @kumaraditya303 for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14.
🐍🍒⛏🤖

@miss-islington-app
Copy link

Sorry, @efimov-mikhail and @kumaraditya303, I could not cleanly backport this to 3.14 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 02202c117b5702f3325e62b07ccdeaa125fc8722 3.14

@miss-islington-app
Copy link

Sorry, @efimov-mikhail and @kumaraditya303, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 02202c117b5702f3325e62b07ccdeaa125fc8722 3.13

@efimov-mikhail
Copy link
Member Author

Thanks for the merge, @kumaraditya303.
I'll make backports manually.

efimov-mikhail added a commit to efimov-mikhail/cpython that referenced this pull request Oct 29, 2025
…age (python#140558)

Co-authored-by: Inada Naoki <songofacandy@gmail.com>
@bedevere-app
Copy link

bedevere-app bot commented Oct 29, 2025

GH-140743 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 Oct 29, 2025
efimov-mikhail added a commit to efimov-mikhail/cpython that referenced this pull request Oct 29, 2025
…age (python#140558)

Co-authored-by: Inada Naoki <songofacandy@gmail.com>
@bedevere-app
Copy link

bedevere-app bot commented Oct 29, 2025

GH-140744 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Oct 29, 2025
@efimov-mikhail
Copy link
Member Author

FYI, @kumaraditya303
The backports are ready and have green CI.

kumaraditya303 pushed a commit that referenced this pull request Oct 30, 2025
…H-140558) (#140743)

gh-140551: Fix `dict` crash if `clear` is called at `lookup` stage (#140558)

Co-authored-by: Inada Naoki <songofacandy@gmail.com>
kumaraditya303 pushed a commit that referenced this pull request Oct 30, 2025
…H-140558) (#140744)

* gh-140551: Fix `dict` crash if `clear` is called at `lookup` stage (#140558)

Co-authored-by: Inada Naoki <songofacandy@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants