GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-122527: Fix a crash on deallocation of `PyStructSequence` by colesbury · Pull Request #122577 · python/cpython

Conversation

@colesbury

@colesbury colesbury commented Aug 1, 2024 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

The PyStructSequence destructor would crash if it was deallocated after its type's dictionary was cleared by the GC, because it couldn't compute the "real size" of the instance. This could occur with relatively straightforward code in the free-threaded build or with a reference cycle involving the type in the default build, due to differing orders in which tp_clear() was called.

Account for the non-sequence fields in tp_basicsize and use that, along with Py_SIZE(), to compute the "real" size of a PyStructSequence in the dealloc function. This avoids the accesses to the type's dictionary during dealloc, which were unsafe.

  • Issue: Crash on deallocator of type 'time.struct_time' during finalizing for free-threaded Python #122527

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@colesbury
pythongh-122527: Fix a crash on deallocation of PyStructSequence
e677b27
The `PyStructSequence` destructor would crash if it was deallocated after
its type's dictionary was cleared by the GC, because it couldn't compute
the "real size" of the instance. This could occur with relatively
straightforward code in the free-threaded build or with a reference
cycle involving the type in the default build, due to differing orders
in which `tp_clear()` was called.

Account for the non-sequence fields in `tp_basicsize` and use that,
along with `Py_SIZE()`, to compute the "real" size of a
`PyStructSequence` in the dealloc function. This avoids the accesses to
the type's dictionary during dealloc, which were unsafe.
@bedevere-app bedevere-app Bot mentioned this pull request Aug 1, 2024
Crash on deallocator of type 'time.struct_time' during finalizing for free-threaded Python #122527
Closed
@colesbury
Add missing cast
fc79413
@colesbury
colesbury marked this pull request as ready for review August 1, 2024 20:10
@bedevere-app bedevere-app Bot added the awaiting core review label Aug 1, 2024
@colesbury colesbury mentioned this pull request Aug 2, 2024
sys.flags.gil should be a "sequence" attribute OR stop adding to the sequence #122575
Closed
encukou
encukou approved these changes Aug 2, 2024
View reviewed changes

@encukou encukou left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

This looks great, thank you for the fix!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting core review labels Aug 2, 2024
@encukou
encukou merged commit 4b63cd1 into python:main Aug 2, 2024
@bedevere-app bedevere-app Bot removed the awaiting merge label Aug 2, 2024
@colesbury
colesbury deleted the gh-122527-structseq branch August 2, 2024 16:12
@colesbury

colesbury commented Aug 2, 2024

Copy link
Copy Markdown
Contributor Author

I think we should probably backport this to 3.13. And maybe 3.12?

What do you think?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@colesbury

colesbury commented Aug 2, 2024

Copy link
Copy Markdown
Contributor Author

I'll add the labels to get the PRs setup and CI started. If the decision ends up not to backport, we can just close those PRs.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@colesbury colesbury added needs backport to 3.12 only security fixes needs backport to 3.13 only security fixes labels Aug 2, 2024
@miss-islington-app

miss-islington-app Bot commented Aug 2, 2024

Copy link
Copy Markdown

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@miss-islington-app

miss-islington-app Bot commented Aug 2, 2024

Copy link
Copy Markdown

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@miss-islington-app

miss-islington-app Bot commented Aug 2, 2024

Copy link
Copy Markdown

Sorry, @colesbury and @encukou, I could not cleanly backport this to 3.12 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 4b63cd170e5dd840bffc80922f09f2d69932ff5c 3.12
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@miss-islington-app miss-islington-app Bot assigned encukou Aug 2, 2024
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Aug 2, 2024
@colesbury @miss-islington
pythongh-122527: Fix a crash on deallocation of PyStructSequence (p…
5ff87cc
…ythonGH-122577)

The `PyStructSequence` destructor would crash if it was deallocated after
its type's dictionary was cleared by the GC, because it couldn't compute
the "real size" of the instance. This could occur with relatively
straightforward code in the free-threaded build or with a reference
cycle involving the type in the default build, due to differing orders
in which `tp_clear()` was called.

Account for the non-sequence fields in `tp_basicsize` and use that,
along with `Py_SIZE()`, to compute the "real" size of a
`PyStructSequence` in the dealloc function. This avoids the accesses to
the type's dictionary during dealloc, which were unsafe.
(cherry picked from commit 4b63cd1)

Co-authored-by: Sam Gross <[email protected]>
@bedevere-app

bedevere-app Bot commented Aug 2, 2024

Copy link
Copy Markdown

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the needs backport to 3.13 only security fixes label Aug 2, 2024
@bedevere-app

bedevere-app Bot commented Aug 2, 2024

Copy link
Copy Markdown

GH-122626 is a backport of this pull request to the 3.12 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the needs backport to 3.12 only security fixes label Aug 2, 2024
@encukou

encukou commented Aug 3, 2024

Copy link
Copy Markdown
Member

For 3.13.1, definitely.
For the RC period, where we aim for no code changes, I don't think it meets the bar. A crash on interpreter shutdown shouldn't interfere with testing the experimental build.

3.12, doesn't have free-threading, and skipping a version when backporting is confusing, so I'd backport it after 3.13.1.

Does that sound reasonable?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

brandtbucher pushed a commit to brandtbucher/cpython that referenced this pull request Aug 7, 2024
@colesbury @brandtbucher
pythongh-122527: Fix a crash on deallocation of PyStructSequence (p…
2657042
…ythonGH-122577)

The `PyStructSequence` destructor would crash if it was deallocated after
its type's dictionary was cleared by the GC, because it couldn't compute
the "real size" of the instance. This could occur with relatively
straightforward code in the free-threaded build or with a reference
cycle involving the type in the default build, due to differing orders
in which `tp_clear()` was called.

Account for the non-sequence fields in `tp_basicsize` and use that,
along with `Py_SIZE()`, to compute the "real" size of a
`PyStructSequence` in the dealloc function. This avoids the accesses to
the type's dictionary during dealloc, which were unsafe.
serhiy-storchaka
serhiy-storchaka reviewed Aug 12, 2024
View reviewed changes
Comment thread Objects/structseq.c
#define REAL_SIZE_TP(tp) \
get_type_attr_as_size(tp, &_Py_ID(n_fields))
#define REAL_SIZE(op) REAL_SIZE_TP(Py_TYPE(op))
#define REAL_SIZE(op) get_real_size((PyObject *)op)

@serhiy-storchaka serhiy-storchaka Aug 12, 2024

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Why not change also REAL_SIZE_TP?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@colesbury colesbury Aug 12, 2024

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

The new calculation involves Py_SIZE(op), so we can't do it on the type alone.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

serhiy-storchaka reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
serhiy-storchaka
serhiy-storchaka reviewed Aug 12, 2024
View reviewed changes
Comment thread Lib/test/test_structseq.py
script_helper.assert_python_ok("-c", textwrap.dedent(r"""
import time
t = time.gmtime()
type(t).refcyle = t

@serhiy-storchaka serhiy-storchaka Aug 12, 2024

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Typo: refcyle.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

colesbury reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
blhsing pushed a commit to blhsing/cpython that referenced this pull request Aug 22, 2024
@colesbury @blhsing
pythongh-122527: Fix a crash on deallocation of PyStructSequence (p…
adf8322
…ythonGH-122577)

The `PyStructSequence` destructor would crash if it was deallocated after
its type's dictionary was cleared by the GC, because it couldn't compute
the "real size" of the instance. This could occur with relatively
straightforward code in the free-threaded build or with a reference
cycle involving the type in the default build, due to differing orders
in which `tp_clear()` was called.

Account for the non-sequence fields in `tp_basicsize` and use that,
along with `Py_SIZE()`, to compute the "real" size of a
`PyStructSequence` in the dealloc function. This avoids the accesses to
the type's dictionary during dealloc, which were unsafe.
Yhg1s pushed a commit that referenced this pull request Sep 2, 2024
@miss-islington @colesbury
[3.13] gh-122527: Fix a crash on deallocation of PyStructSequence (G…
57ba3b0
…H-122577) (#122625)

gh-122527: Fix a crash on deallocation of `PyStructSequence` (GH-122577)

The `PyStructSequence` destructor would crash if it was deallocated after
its type's dictionary was cleared by the GC, because it couldn't compute
the "real size" of the instance. This could occur with relatively
straightforward code in the free-threaded build or with a reference
cycle involving the type in the default build, due to differing orders
in which `tp_clear()` was called.

Account for the non-sequence fields in `tp_basicsize` and use that,
along with `Py_SIZE()`, to compute the "real" size of a
`PyStructSequence` in the dealloc function. This avoids the accesses to
the type's dictionary during dealloc, which were unsafe.
(cherry picked from commit 4b63cd1)

Co-authored-by: Sam Gross <[email protected]>
ambv pushed a commit that referenced this pull request Sep 6, 2024
@colesbury
[3.12] gh-122527: Fix a crash on deallocation of PyStructSequence (G…
1bfb58c
…H-122577) (#122626)

The `PyStructSequence` destructor would crash if it was deallocated after
its type's dictionary was cleared by the GC, because it couldn't compute
the "real size" of the instance. This could occur with relatively
straightforward code in the free-threaded build or with a reference
cycle involving the type in the default build, due to differing orders
in which `tp_clear()` was called.

Account for the non-sequence fields in `tp_basicsize` and use that,
along with `Py_SIZE()`, to compute the "real" size of a
`PyStructSequence` in the dealloc function. This avoids the accesses to
the type's dictionary during dealloc, which were unsafe.
(cherry picked from commit 4b63cd1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@serhiy-storchaka serhiy-storchaka serhiy-storchaka left review comments
@encukou encukou encukou approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@encukou encukou

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@colesbury @encukou @serhiy-storchaka
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.