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-157176: Make time.struct_time type immutable by ashm-dev · Pull Request #157179 · python/cpython

Conversation

@ashm-dev

@ashm-dev ashm-dev commented Sep 8, 2026 •
edited by bedevere-app Bot
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor
  • Issue: Memory leak on interpreter shutdown when reference cycle exists between structseq type and its instance #157176

Sorry, something went wrong.

Uh oh!

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

All reactions
@ashm-dev
fix(structseq): enable GC tracking in PyStructSequence_New
ad2aa7b
@bedevere-app bedevere-app Bot mentioned this pull request Sep 8, 2026
Memory leak on interpreter shutdown when reference cycle exists between structseq type and its instance #157176
Closed
@bedevere-app bedevere-app Bot added the awaiting review label Sep 8, 2026
@ashm-dev
Merge branch 'main' into pythongh-157176
60b9a41
@ashm-dev

ashm-dev commented Sep 8, 2026

Copy link
Copy Markdown
Contributor Author

@sergey-miryanov, could you please review this PR?

All reactions

Sorry, something went wrong.

Uh oh!

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

sergey-miryanov
sergey-miryanov reviewed Sep 8, 2026
View reviewed changes
Comment thread Objects/structseq.c Resolved

Uh oh!

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

@vstinner

vstinner commented Sep 13, 2026

Copy link
Copy Markdown
Member

The reported issue is a leak when modifying structseq types. But this PR changes how structseq instances are tracked by the GC. Can you explain me the relationship between the two?

All reactions

Sorry, something went wrong.

Uh oh!

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

@ashm-dev

ashm-dev commented Sep 14, 2026

Copy link
Copy Markdown
Contributor Author

The reported issue is a leak when modifying structseq types. But this PR changes how structseq instances are tracked by the GC. Can you explain me the relationship between the two?

They are the same bug. Modifying the type creates the cycle type -> tp_dict -> t -> type. It leaks because t is untracked, so the GC cannot see the t -> type edge. Tracking the instance makes that edge visible and lets the GC collect the cycle.

All reactions

Sorry, something went wrong.

Uh oh!

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

@vstinner

vstinner commented Sep 14, 2026

Copy link
Copy Markdown
Member

See also #157447 change which is limited to sys.unraisablehook.

ashm-dev reacted with eyes emoji
All reactions
  • 👀 1 reaction

Sorry, something went wrong.

Uh oh!

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

@vstinner vstinner mentioned this pull request Sep 14, 2026
gh-157443: Track the argument of sys.unraisablehook in the GC #157447
Open
@serhiy-storchaka

serhiy-storchaka commented Sep 14, 2026

Copy link
Copy Markdown
Member

Claude initially wrote exactly this change. But it is potentially breaking, the following PyObject_GC_Track() in the user code will crash.

All reactions

Sorry, something went wrong.

Uh oh!

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

@ashm-dev

ashm-dev commented Sep 14, 2026

Copy link
Copy Markdown
Contributor Author

Claude initially wrote exactly this change. But it is potentially breaking, the following PyObject_GC_Track() in the user code will crash.

But the fix in your PR doesn't resolve the leak from my issue.

All reactions

Sorry, something went wrong.

Uh oh!

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

@sergey-miryanov

sergey-miryanov commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

But the fix in your PR doesn't resolve the leak from my issue.

Your both issues have a common root but fix from #157447 shouldn't fix your issue.

All reactions

Sorry, something went wrong.

Uh oh!

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

@ashm-dev

ashm-dev commented Oct 3, 2026

Copy link
Copy Markdown
Contributor Author

Friendly ping @serhiy-storchaka @vstinner — what's the plan here?

All reactions

Sorry, something went wrong.

Uh oh!

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

@vstinner

vstinner commented Oct 5, 2026

Copy link
Copy Markdown
Member

Sadly, changing the public PyStructSequence_New() C API sounds dangerous. As @serhiy-storchaka wrote, it can break existing C extensions which call PyObject_GC_Track().

You can change internal C APIs or modify stdlib extensions to call PyObject_GC_Track() after PyStructSequence_New(). I'm not sure of what's the best approach.

Also, if it's recommended to track objects created by PyStructSequence_New() in the GC, you should mention it in PyStructSequence_New() documentation.

All reactions

Sorry, something went wrong.

Uh oh!

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

@ashm-dev

ashm-dev commented Oct 5, 2026

Copy link
Copy Markdown
Contributor Author

Should I revert the PyStructSequence_New() change, add PyObject_GC_Track() to stdlib callers (time, os, etc.) similar to #157447, and document the tracking requirement in PyStructSequence_New() docs?

All reactions

Sorry, something went wrong.

Uh oh!

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

@vstinner

vstinner commented Oct 5, 2026

Copy link
Copy Markdown
Member

Should I revert the PyStructSequence_New() change, add PyObject_GC_Track() to stdlib callers (time, os, etc.) similar to #157447, and document the tracking requirement in PyStructSequence_New() docs?

Yes, but only add PyObject_GC_Track() if an object can contains other objects which can be involved in a reference cycle.

ashm-dev reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

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

@ashm-dev
fix(structseq): track struct_time and asyncgen_hooks in GC
053f101
@ashm-dev
ashm-dev requested review from StanFromIreland, ZeroIntensity, ericsnowcurrently and pganssle as code owners October 5, 2026 21:01
@ashm-dev

ashm-dev commented Oct 5, 2026

Copy link
Copy Markdown
Contributor Author

@vstinner Done. Reverted changes to PyStructSequence_New(), added PyObject_GC_Track() to time.struct_time and sys.get_asyncgen_hooks(), documented the requirement in PyStructSequence_New() docs, and updated tests.

All reactions

Sorry, something went wrong.

Uh oh!

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

vstinner
vstinner reviewed Oct 5, 2026
View reviewed changes

@vstinner vstinner 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.

        # Instances created via Python or stdlib callers that may participate
        # in cycles are GC-tracked; simple instances like os.stat_result are not.
        self.assertTrue(gc.is_tracked(time.gmtime()))

time.gmtime() members are all integers. How can it be involved in a reference cycle?

Code from the issue:

./python -c "import time; t = time.gmtime(); type(t).refcyle = t;"

In your example, you modify the type, not a structseq instance.

I'm not sure that this change is correct.

cc @serhiy-storchaka

Sorry, something went wrong.

Uh oh!

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

All reactions
@vstinner

vstinner commented Oct 5, 2026

Copy link
Copy Markdown
Member

Instead, the type should be made immutable: #157176 (comment).

All reactions

Sorry, something went wrong.

Uh oh!

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

@serhiy-storchaka

serhiy-storchaka commented Oct 6, 2026

Copy link
Copy Markdown
Member

AFAIK only the argument of sys.unraisablehook contains other objects which can be involved in a reference cycle (this is #157447).

vstinner reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

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

@ashm-dev
fix(time): make struct_time type immutable
a5a15b2
@ashm-dev
ashm-dev requested review from sergey-miryanov and vstinner October 6, 2026 21:22
vstinner
vstinner approved these changes Oct 6, 2026
View reviewed changes

@vstinner vstinner 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.

LGTM. I just have a request on the test.

cc @methane

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Lib/test/test_structseq.py Resolved

Uh oh!

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

@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting review labels Oct 6, 2026
methane
methane approved these changes Oct 7, 2026
View reviewed changes
sergey-miryanov
sergey-miryanov approved these changes Oct 7, 2026
View reviewed changes
@ashm-dev
test(structseq): use immutable type check
e996c40
@vstinner

vstinner commented Oct 7, 2026

Copy link
Copy Markdown
Member

The Docs CI failed with:

      File "/home/runner/work/cpython/cpython/Doc/venv/lib/python3.14/site-packages/sphinx/builders/changes.py", line 61, in write_documents
        ttext = self.typemap[changeset.type]
                ~~~~~~~~~~~~^^^^^^^^^^^^^^^^
    KeyError: 'soft-deprecated'

Let me update the branch to see if it does fix the issue.

All reactions

Sorry, something went wrong.

Uh oh!

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

@vstinner
Merge branch 'main' into pythongh-157176
675e4ba
@vstinner
vstinner enabled auto-merge (squash) October 7, 2026 14:30
@vstinner

vstinner commented Oct 7, 2026

Copy link
Copy Markdown
Member

I enabled auto-merged, please don't touch the PR (your branch) anymore.

Thanks for support.check_immutable_type(self, type(time.gmtime())). The intent of the test is now more obvious (at least,to me).

ashm-dev reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

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

@vstinner vstinner changed the title gh-157176: Fix GC tracking in PyStructSequence_New gh-157176: Make time.struct_time type immutable Oct 7, 2026
@vstinner
vstinner merged commit acfa776 into python:main Oct 7, 2026
55 checks passed

Uh oh!

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

@bedevere-app bedevere-app Bot removed the awaiting merge label Oct 7, 2026
@ashm-dev
ashm-dev deleted the gh-157176 branch October 7, 2026 15:03
StanFromIreland
StanFromIreland reviewed Oct 7, 2026
View reviewed changes
Comment thread Misc/NEWS.d/next/Core_and_Builtins/2026-09-08-15-55-00.gh-issue-157176.yZ8a1B.rst

@StanFromIreland StanFromIreland Oct 7, 2026

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 should be in Library.

Sorry, something went wrong.

Uh oh!

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

All reactions

@vstinner vstinner Oct 7, 2026

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.

Oh! I didn't pay attention to the section of the Changelog entry. Sadly, I saw your comment after the PR was merged.

Sorry, something went wrong.

Uh oh!

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

All reactions

@StanFromIreland StanFromIreland Oct 7, 2026

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.

I wrote #158983 to move it.

Sorry, something went wrong.

Uh oh!

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

vstinner reacted with heart emoji
All reactions
  • ❤️ 1 reaction
@vstinner

vstinner commented Oct 7, 2026

Copy link
Copy Markdown
Member

Merged. Thanks for your fix.

All reactions

Sorry, something went wrong.

Uh oh!

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@vstinner vstinner vstinner approved these changes
@StanFromIreland StanFromIreland StanFromIreland left review comments
@sergey-miryanov sergey-miryanov sergey-miryanov approved these changes
@methane methane methane approved these changes
@pganssle pganssle Awaiting requested review from pganssle pganssle is a code owner
@ericsnowcurrently ericsnowcurrently Awaiting requested review from ericsnowcurrently
@ZeroIntensity ZeroIntensity Awaiting requested review from ZeroIntensity

Uh oh!

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

Assignees

No one assigned

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.

6 participants

@ashm-dev @vstinner @serhiy-storchaka @sergey-miryanov @methane @StanFromIreland
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.