Skip to content

gh-123657: Fix crash and refleak in decimal.getcontext() #123703

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

Merged
merged 6 commits into from
Sep 6, 2024
Merged

gh-123657: Fix crash and refleak in decimal.getcontext() #123703

merged 6 commits into from
Sep 6, 2024

Conversation

neonene
Copy link
Contributor

@neonene neonene commented Sep 4, 2024

@neonene
Copy link
Contributor Author

neonene commented Sep 4, 2024

@ZeroIntensity Could you verify this fix?

cc @kumaraditya303 @erlend-aasland

@ZeroIntensity
Copy link
Member

I can take a look in an hour or so.

Copy link
Member

@ZeroIntensity ZeroIntensity left a comment

Choose a reason for hiding this comment

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

Confirmed that this fixes the assertion failure, as well as the memory leak:

./python -Xshowrefcount -c "import decimal; decimal.getcontext()"
[0 refs, 0 blocks]

@ZeroIntensity
Copy link
Member

@encukou, this needs the 3.13 backport label.

@rhettinger rhettinger removed their request for review September 5, 2024 03:11
@Eclips4 Eclips4 added the needs backport to 3.13 bugs and security fixes label Sep 5, 2024
@encukou
Copy link
Member

encukou commented Sep 5, 2024

This makes setcontext leak in the --with-decimal-contextvar=no build:

$ ./python -Xshowrefcount -c "import decimal; decimal.getcontext(); decimal.setcontext(decimal.Context())"
[7122 refs, 4233 blocks]

Since cached_context is now an owned reference, PyDec_SetCurrentContext needs a Py_CLEAR instead of just setting to NULL.
Please also adjust cached_context's comment in the decimal_state struct: explicity mention it's a strong reference.

@encukou encukou merged commit 853588e into python:main Sep 6, 2024
39 checks passed
@miss-islington-app
Copy link

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

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Sep 6, 2024
@bedevere-app
Copy link

bedevere-app bot commented Sep 6, 2024

GH-123774 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 Sep 6, 2024
@encukou
Copy link
Member

encukou commented Sep 6, 2024

Thanks!
(FWIW, A big reason why I wanted “owned reference” mentioned is that it's not immediately clear that this is a PyObject subclass and so it even can have strong reference. It didn't need to be emphasized this much. But, that's a minor point.)

@neonene neonene deleted the noctxvar branch September 6, 2024 11:37
@vstinner
Copy link
Member

vstinner commented Sep 6, 2024

Thanks for this nice fix. It does fix this leak.

Before:

$ ./python -X showrefcount -c 'import decimal; decimal.getcontext()'
[207 refs, 138 blocks]

After (with this fix):

$ ./python -X showrefcount -c 'import decimal; decimal.getcontext()'
[0 refs, 0 blocks]

cc @picnixz

encukou pushed a commit that referenced this pull request Sep 6, 2024
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.

5 participants