Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace key if not identical to old key in dict #31685

Closed
wants to merge 1 commit into from

Conversation

malthe
Copy link

@malthe malthe commented Mar 4, 2022

This fixes an issue in dict and by extension in weakref.WeakKeyDictionary which arises when a key that is equal to an existing key is used to set a new value – where the key itself is not replaced.

To appreciate this situation, consider when the keys are type weakref.ref.

In this case, the lifetime of the key after replacing the value is now that of the initial key and not the one that's been set subsequently – since the key entry is still the initial object. If the first key falls to refcount zero, the key is removed from the weakref.WeakKeyDictionary – which is unexpected because we have set a newer key which might have refcount greater than zero.

In the general case where a value is replaced for the exact same key (object identity), there is no practical difference with this change – and when a new key is used to replace a value, the cost is only a write to memory.

Thanks to @potiuk and @ashb for helping finding this issue which was discovered during flaky test runs in Apache Airflow.

@potiuk
Copy link

potiuk commented Mar 4, 2022

Nice :)

potiuk added a commit to apache/airflow that referenced this pull request Mar 4, 2022
There is a very probable WeakKeyDict bug in Python standard
library (to be confirmed and investigated further) that
manifests itself in a very rare failure of the
test_stacktrace_on_failure_starts_with_task_execute_method

This turned out to be related to an unexpected behaviour
(and most likely a bug - to be confirmed) of WeakKeyDict
when you have potentially two different objects with the
same `equals` and `hash` values added to the same
WeakKeyDict as keys.

More info on similar report (but raised for a bit different
reason) bug in Python can be found here:

https://bugs.python.org/issue44140

We submitted a PR to fix the problem found 
python/cpython#31685
@malthe malthe force-pushed the fix-weakref-key-replacement branch from f572737 to 4f8c6b6 Compare March 4, 2022 23:10
@malthe malthe changed the title Keys should always be replaced in WeakKeyDictionary Replace key if not identical to old key in dict Mar 4, 2022
@malthe malthe force-pushed the fix-weakref-key-replacement branch from 4f8c6b6 to 0fd074a Compare March 4, 2022 23:20
@methane
Copy link
Member

methane commented Mar 5, 2022

Thank you for first pull request. But we need an issue on bugs.python.org unless the pull request don't change any behavior (e.g. fixing typo, code cleanup).

But we are planning to moving issue tracker from bugs.python.org to GitHub issues in this month.
https://discuss.python.org/t/github-issues-migration-is-coming-soon/13791

  • All issues are copied to the GitHub issues.
  • During the migration (3-5 days estimated), you can not use both of bugs.python.org and GitHub issues.
  • If you don't want to learn bugs.python.org only for few days, I recommend to wait the migration finished.

In the general case where a value is replaced for the exact same key (object identity), there is no practical difference with this change – and when a new key is used to replace a value, the cost is only a write to memory.

This change is not so easy as you think. In Python, True == 1, False == 0, 2.0 == 2. We guaranteed "first wins" semantics for very long time. So it is not an option to change the dict.

I recommend you to start discussion in:

Both discussion can be used during issue tracker migration.

When posting your idea on the discussion, please describe your use case more concretely.

@rhettinger rhettinger closed this Mar 5, 2022
@ashb
Copy link

ashb commented Mar 7, 2022

@malthe Might be worth turning this in to a doc update to mention this "gotcha" for the next people?

@potiuk
Copy link

potiuk commented Mar 7, 2022

@malthe Might be worth turning this in to a doc update to mention this "gotcha" for the next people?

Yep. We already asked for it https://bugs.python.org/issue46925 and Python maintainers agreed it's a good thing to do and reopened the issue to be "doc-issue".

ephraimbuddy pushed a commit to apache/airflow that referenced this pull request Mar 23, 2022
There is a very probable WeakKeyDict bug in Python standard
library (to be confirmed and investigated further) that
manifests itself in a very rare failure of the
test_stacktrace_on_failure_starts_with_task_execute_method

This turned out to be related to an unexpected behaviour
(and most likely a bug - to be confirmed) of WeakKeyDict
when you have potentially two different objects with the
same `equals` and `hash` values added to the same
WeakKeyDict as keys.

More info on similar report (but raised for a bit different
reason) bug in Python can be found here:

https://bugs.python.org/issue44140

We submitted a PR to fix the problem found
python/cpython#31685

(cherry picked from commit 1949f5d)
ephraimbuddy pushed a commit to apache/airflow that referenced this pull request Mar 24, 2022
There is a very probable WeakKeyDict bug in Python standard
library (to be confirmed and investigated further) that
manifests itself in a very rare failure of the
test_stacktrace_on_failure_starts_with_task_execute_method

This turned out to be related to an unexpected behaviour
(and most likely a bug - to be confirmed) of WeakKeyDict
when you have potentially two different objects with the
same `equals` and `hash` values added to the same
WeakKeyDict as keys.

More info on similar report (but raised for a bit different
reason) bug in Python can be found here:

https://bugs.python.org/issue44140

We submitted a PR to fix the problem found
python/cpython#31685

(cherry picked from commit 1949f5d)
ephraimbuddy pushed a commit to apache/airflow that referenced this pull request Mar 26, 2022
There is a very probable WeakKeyDict bug in Python standard
library (to be confirmed and investigated further) that
manifests itself in a very rare failure of the
test_stacktrace_on_failure_starts_with_task_execute_method

This turned out to be related to an unexpected behaviour
(and most likely a bug - to be confirmed) of WeakKeyDict
when you have potentially two different objects with the
same `equals` and `hash` values added to the same
WeakKeyDict as keys.

More info on similar report (but raised for a bit different
reason) bug in Python can be found here:

https://bugs.python.org/issue44140

We submitted a PR to fix the problem found
python/cpython#31685

(cherry picked from commit 1949f5d)
leahecole pushed a commit to GoogleCloudPlatform/composer-airflow that referenced this pull request Jun 4, 2022
There is a very probable WeakKeyDict bug in Python standard
library (to be confirmed and investigated further) that
manifests itself in a very rare failure of the
test_stacktrace_on_failure_starts_with_task_execute_method

This turned out to be related to an unexpected behaviour
(and most likely a bug - to be confirmed) of WeakKeyDict
when you have potentially two different objects with the
same `equals` and `hash` values added to the same
WeakKeyDict as keys.

More info on similar report (but raised for a bit different
reason) bug in Python can be found here:

https://bugs.python.org/issue44140

We submitted a PR to fix the problem found
python/cpython#31685

(cherry picked from commit 1949f5d76b5842d56db91c868ae4655bb7a7689f)

GitOrigin-RevId: 91b95ea6633bc14ca9bdefe5eac0ac948465dfc9
kosteev pushed a commit to GoogleCloudPlatform/composer-airflow that referenced this pull request Jul 10, 2022
There is a very probable WeakKeyDict bug in Python standard
library (to be confirmed and investigated further) that
manifests itself in a very rare failure of the
test_stacktrace_on_failure_starts_with_task_execute_method

This turned out to be related to an unexpected behaviour
(and most likely a bug - to be confirmed) of WeakKeyDict
when you have potentially two different objects with the
same `equals` and `hash` values added to the same
WeakKeyDict as keys.

More info on similar report (but raised for a bit different
reason) bug in Python can be found here:

https://bugs.python.org/issue44140

We submitted a PR to fix the problem found
python/cpython#31685

GitOrigin-RevId: 1949f5d76b5842d56db91c868ae4655bb7a7689f
leahecole pushed a commit to GoogleCloudPlatform/composer-airflow that referenced this pull request Aug 30, 2022
There is a very probable WeakKeyDict bug in Python standard
library (to be confirmed and investigated further) that
manifests itself in a very rare failure of the
test_stacktrace_on_failure_starts_with_task_execute_method

This turned out to be related to an unexpected behaviour
(and most likely a bug - to be confirmed) of WeakKeyDict
when you have potentially two different objects with the
same `equals` and `hash` values added to the same
WeakKeyDict as keys.

More info on similar report (but raised for a bit different
reason) bug in Python can be found here:

https://bugs.python.org/issue44140

We submitted a PR to fix the problem found
python/cpython#31685

GitOrigin-RevId: 1949f5d76b5842d56db91c868ae4655bb7a7689f
leahecole pushed a commit to GoogleCloudPlatform/composer-airflow that referenced this pull request Oct 4, 2022
There is a very probable WeakKeyDict bug in Python standard
library (to be confirmed and investigated further) that
manifests itself in a very rare failure of the
test_stacktrace_on_failure_starts_with_task_execute_method

This turned out to be related to an unexpected behaviour
(and most likely a bug - to be confirmed) of WeakKeyDict
when you have potentially two different objects with the
same `equals` and `hash` values added to the same
WeakKeyDict as keys.

More info on similar report (but raised for a bit different
reason) bug in Python can be found here:

https://bugs.python.org/issue44140

We submitted a PR to fix the problem found
python/cpython#31685

GitOrigin-RevId: 1949f5d76b5842d56db91c868ae4655bb7a7689f
aglipska pushed a commit to GoogleCloudPlatform/composer-airflow that referenced this pull request Oct 7, 2022
There is a very probable WeakKeyDict bug in Python standard
library (to be confirmed and investigated further) that
manifests itself in a very rare failure of the
test_stacktrace_on_failure_starts_with_task_execute_method

This turned out to be related to an unexpected behaviour
(and most likely a bug - to be confirmed) of WeakKeyDict
when you have potentially two different objects with the
same `equals` and `hash` values added to the same
WeakKeyDict as keys.

More info on similar report (but raised for a bit different
reason) bug in Python can be found here:

https://bugs.python.org/issue44140

We submitted a PR to fix the problem found
python/cpython#31685

GitOrigin-RevId: 1949f5d76b5842d56db91c868ae4655bb7a7689f
leahecole pushed a commit to GoogleCloudPlatform/composer-airflow that referenced this pull request Dec 7, 2022
There is a very probable WeakKeyDict bug in Python standard
library (to be confirmed and investigated further) that
manifests itself in a very rare failure of the
test_stacktrace_on_failure_starts_with_task_execute_method

This turned out to be related to an unexpected behaviour
(and most likely a bug - to be confirmed) of WeakKeyDict
when you have potentially two different objects with the
same `equals` and `hash` values added to the same
WeakKeyDict as keys.

More info on similar report (but raised for a bit different
reason) bug in Python can be found here:

https://bugs.python.org/issue44140

We submitted a PR to fix the problem found
python/cpython#31685

GitOrigin-RevId: 1949f5d76b5842d56db91c868ae4655bb7a7689f
leahecole pushed a commit to GoogleCloudPlatform/composer-airflow that referenced this pull request Jan 27, 2023
There is a very probable WeakKeyDict bug in Python standard
library (to be confirmed and investigated further) that
manifests itself in a very rare failure of the
test_stacktrace_on_failure_starts_with_task_execute_method

This turned out to be related to an unexpected behaviour
(and most likely a bug - to be confirmed) of WeakKeyDict
when you have potentially two different objects with the
same `equals` and `hash` values added to the same
WeakKeyDict as keys.

More info on similar report (but raised for a bit different
reason) bug in Python can be found here:

https://bugs.python.org/issue44140

We submitted a PR to fix the problem found
python/cpython#31685

GitOrigin-RevId: 1949f5d76b5842d56db91c868ae4655bb7a7689f
kosteev pushed a commit to kosteev/composer-airflow-test-copybara that referenced this pull request Sep 12, 2024
There is a very probable WeakKeyDict bug in Python standard
library (to be confirmed and investigated further) that
manifests itself in a very rare failure of the
test_stacktrace_on_failure_starts_with_task_execute_method

This turned out to be related to an unexpected behaviour
(and most likely a bug - to be confirmed) of WeakKeyDict
when you have potentially two different objects with the
same `equals` and `hash` values added to the same
WeakKeyDict as keys.

More info on similar report (but raised for a bit different
reason) bug in Python can be found here:

https://bugs.python.org/issue44140

We submitted a PR to fix the problem found
python/cpython#31685

GitOrigin-RevId: 1949f5d76b5842d56db91c868ae4655bb7a7689f
kosteev pushed a commit to GoogleCloudPlatform/composer-airflow that referenced this pull request Sep 17, 2024
There is a very probable WeakKeyDict bug in Python standard
library (to be confirmed and investigated further) that
manifests itself in a very rare failure of the
test_stacktrace_on_failure_starts_with_task_execute_method

This turned out to be related to an unexpected behaviour
(and most likely a bug - to be confirmed) of WeakKeyDict
when you have potentially two different objects with the
same `equals` and `hash` values added to the same
WeakKeyDict as keys.

More info on similar report (but raised for a bit different
reason) bug in Python can be found here:

https://bugs.python.org/issue44140

We submitted a PR to fix the problem found
python/cpython#31685

GitOrigin-RevId: 1949f5d76b5842d56db91c868ae4655bb7a7689f
kosteev pushed a commit to GoogleCloudPlatform/composer-airflow that referenced this pull request Nov 7, 2024
There is a very probable WeakKeyDict bug in Python standard
library (to be confirmed and investigated further) that
manifests itself in a very rare failure of the
test_stacktrace_on_failure_starts_with_task_execute_method

This turned out to be related to an unexpected behaviour
(and most likely a bug - to be confirmed) of WeakKeyDict
when you have potentially two different objects with the
same `equals` and `hash` values added to the same
WeakKeyDict as keys.

More info on similar report (but raised for a bit different
reason) bug in Python can be found here:

https://bugs.python.org/issue44140

We submitted a PR to fix the problem found
python/cpython#31685

GitOrigin-RevId: 1949f5d76b5842d56db91c868ae4655bb7a7689f
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.

7 participants