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

gh-122208: Don't delivery PyDict_EVENT_ADDED until it can't fail #122207

Merged
merged 3 commits into from
Jul 24, 2024

Conversation

DinoV
Copy link
Contributor

@DinoV DinoV commented Jul 23, 2024

Currently PyDict_EVENT_ADDED can be delivered when the insert is going to fail due to an OOM. This means the listener has no capability of knowing the true state of the dictionary if it fails.

This modifies the events to be delivered before the change has happened but only after any underlying necessary allocations have occurred.

@DinoV DinoV changed the title Don't delivery PyDict_EVENT_ADDED until it can't fail gh-122208: Don't delivery PyDict_EVENT_ADDED until it can't fail Jul 23, 2024
@DinoV DinoV requested a review from carljm July 24, 2024 00:01
@DinoV DinoV added needs backport to 3.12 bug and security fixes needs backport to 3.13 bugs and security fixes labels Jul 24, 2024
@DinoV DinoV marked this pull request as ready for review July 24, 2024 00:23
Copy link
Member

@carljm carljm 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, thanks for the fix! Sorry about the debugging session that I presume must have led to this find :)

@@ -1745,16 +1750,11 @@ insertdict(PyInterpreterState *interp, PyDictObject *mp,
MAINTAIN_TRACKING(mp, key, value);

if (ix == DKIX_EMPTY) {
assert(!_PyDict_HasSplitTable(mp));
Copy link
Member

Choose a reason for hiding this comment

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

Was it intentional to remove this assertion? Doesn't seem related to the dict watcher stuff.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, thanks for catching that, I'll remove it.

Luckily there was no debugging involved! Just trying to re-factor some existing code to use dict watchers and I was worried about what would happen when the updates would fail (as the existing code handled it). So this will actually make that somewhat cleaner!

@DinoV DinoV merged commit 5592399 into python:main Jul 24, 2024
37 checks passed
@miss-islington-app
Copy link

Thanks @DinoV for the PR 🌮🎉.. I'm working now to backport this PR to: 3.12, 3.13.
🐍🍒⛏🤖

facebook-github-bot pushed a commit to facebookincubator/cinder that referenced this pull request Jul 24, 2024
Summary:
Back port of: python/cpython#122207 fixing python/cpython#122208

The current dictionary watchers implementation delivers the added event before it checks to see if we need to re-size the dictionary. This resize can fail so the value isn't added, and then the tracker is out of sync with the true state of the dictionary.

This moves the delivery of the event to after any necessary allocations have happened.

Reviewed By: jbower-fb

Differential Revision: D60182094

fbshipit-source-id: f34940e98ce1caadeee364f9d126d35839661961
facebook-github-bot pushed a commit to facebookincubator/cinder that referenced this pull request Jul 24, 2024
Summary:
Back port of: python/cpython#122207 fixing python/cpython#122208

The current dictionary watchers implementation delivers the added event before it checks to see if we need to re-size the dictionary. This resize can fail so the value isn't added, and then the tracker is out of sync with the true state of the dictionary.

This moves the delivery of the event to after any necessary allocations have happened.

Reviewed By: jbower-fb

Differential Revision: D60182094

fbshipit-source-id: f34940e98ce1caadeee364f9d126d35839661961
nohlson pushed a commit to nohlson/cpython that referenced this pull request Jul 24, 2024
nohlson pushed a commit to nohlson/cpython that referenced this pull request Jul 24, 2024
@DinoV DinoV deleted the dict-event-added branch July 26, 2024 21:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs backport to 3.12 bug and security fixes needs backport to 3.13 bugs and security fixes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants