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-137110: Untrack immortal objects from expand_region_transitivity_reachable by sergey-miryanov · Pull Request #137111 · python/cpython

Conversation

@sergey-miryanov

@sergey-miryanov sergey-miryanov commented Jul 25, 2025 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

There is some inconsistency in handling of immortal objects in default-build's GC.

In update_refs immortal objects just untracked (all immortal objects that we face in GC is an accidentally immortalized).

cpython/Python/gc.c

Lines 495 to 500 in 1e69cd1

if (_Py_IsImmortal(op)) {
assert(!_Py_IsStaticImmortal(op));
_PyObject_GC_UNTRACK(op);
gc = next;
continue;
}

update_refs called for all collections (young, increment and full). But for incremental collection if increment_size < gcstate->work_to_do we steal some objects from neighbor gen and call expand_region_transitively_reachable where immortal objects moved to permanent generation:

cpython/Python/gc.c

Lines 1397 to 1402 in 1e69cd1

if (_Py_IsImmortal(op)) {
PyGC_Head *next = GC_NEXT(gc);
gc_list_move(gc, &gcstate->permanent_generation.head);
gc = next;
continue;
}

So, one part of immortal objects can be untracked, and other part can be moved to perm gen.

As I understand from #127519 it is preferring to untrack immortal objects in all cases.

CPython versions tested on:

CPython main branch

  • Issue: Inconsistency handling of immortal objects in gc #137110

Sorry, something went wrong.

Uh oh!

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

All reactions
@sergey-miryanov
Untrack immortal objects from expand_region_transitivity_reachable
c9391eb
@bedevere-app bedevere-app Bot added the awaiting review label Jul 25, 2025
@bedevere-app bedevere-app Bot mentioned this pull request Jul 25, 2025
Inconsistency handling of immortal objects in gc #137110
Closed
@sergey-miryanov

sergey-miryanov commented Jul 25, 2025

Copy link
Copy Markdown
Contributor Author

I'm not sure, but this probably should skip news.

All reactions

Sorry, something went wrong.

Uh oh!

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

@sobolevn sobolevn added the skip news label Jul 26, 2025
@sobolevn
sobolevn requested review from markshannon and nascheme July 26, 2025 10:40
@sergey-miryanov

sergey-miryanov commented Jul 29, 2025

Copy link
Copy Markdown
Contributor Author

AFAIU moving immortal objects to the permanent_generation is against the completed_scavenge logic, and the visited_space will stick in this case.

All reactions

Sorry, something went wrong.

Uh oh!

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

@sergey-miryanov

sergey-miryanov commented Oct 27, 2025

Copy link
Copy Markdown
Contributor Author

@nascheme Could you please take a look?

All reactions

Sorry, something went wrong.

Uh oh!

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

efimov-mikhail
efimov-mikhail approved these changes Oct 27, 2025
View reviewed changes

@efimov-mikhail efimov-mikhail 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

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 core review and removed awaiting review labels Oct 27, 2025
@nascheme

nascheme commented Oct 28, 2025

Copy link
Copy Markdown
Member

@nascheme Could you please take a look?

I can (although not just now). Probably @markshannon would be the most useful reviewer since he's most familiar with that code.

sergey-miryanov reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

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

@nascheme

nascheme commented Oct 29, 2025

Copy link
Copy Markdown
Member

moving immortal objects to the permanent_generation is against the completed_scavenge logic, and the visited_space will stick in this case.

I'd need to study the GC code more to familiarize myself with how this works. However, would it be possible to change the other case and always move immortal objects to the permanent generation, rather than untracking? I think it's safer to do that, for a couple of reasons. First, the 3.13 GC did not untrack, only move. Second, if an object's dealloc assumes that it is tracked, untracking it could cause a crash (if _PyObject_GC_UNTRACK is used). I don't know if that can actually happens but it seems like a risk to me. Finally, gc.get_objects() will no longer return this immortal objects. Again, I don't know if that's actually a problem (people probably shouldn't be making strong assumptions about what that function returns) but it seems safer to not untrack.

I would guess that immortal container objects (tracked by the GC) are quite rare. So the performance benefit from untracking should be tiny.

All reactions

Sorry, something went wrong.

Uh oh!

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

@sergey-miryanov

sergey-miryanov commented Oct 29, 2025

Copy link
Copy Markdown
Contributor Author

Thanks!

We always untrack newly created immortal objects. In the PR that I linked (which unfortunately was reverted), immortal objects were untracked in an analogous function - https://github.com/python/cpython/pull/127519/files#diff-a848a0ef178aa113a092e72403da0e344f37bd141bb90a7aa65015c77bfe7385R1451.

Also, IIUC, moving immortal objects to the permanent generation would prevent us from flipping visited and pending spaces unless we remove those objects from the permanent generation.

OTOH, the case that handled by this condition is very rare - it is for accidentally immortalized objects.

All reactions

Sorry, something went wrong.

Uh oh!

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

@kumaraditya303

kumaraditya303 commented Nov 8, 2025

Copy link
Copy Markdown
Contributor

However, would it be possible to change the other case and always move immortal objects to the permanent generation, rather than untracking? I think it's safer to do that, for a couple of reasons. First, the 3.13 GC did not untrack, only move. Second, if an object's dealloc assumes that it is tracked, untracking it could cause a crash (if _PyObject_GC_UNTRACK is used).

The free-threading gc untracks any immortal objects encountered during gc so I think it makes sense to do the same for normal builds as well.

All reactions

Sorry, something went wrong.

Uh oh!

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

@sergey-miryanov

sergey-miryanov commented Nov 20, 2025

Copy link
Copy Markdown
Contributor Author

@markshannon Could you please take a look?

All reactions

Sorry, something went wrong.

Uh oh!

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

@github-actions

github-actions Bot commented Apr 27, 2026

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

All reactions

Sorry, something went wrong.

Uh oh!

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

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Apr 27, 2026
@sergey-miryanov

sergey-miryanov commented May 3, 2026

Copy link
Copy Markdown
Contributor Author

Now that the generational GC has landed, I'll close this as obsolete.

All reactions

Sorry, something went wrong.

Uh oh!

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

@sergey-miryanov sergey-miryanov closed this May 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@efimov-mikhail efimov-mikhail efimov-mikhail approved these changes
@nascheme nascheme Awaiting requested review from nascheme
@markshannon markshannon Awaiting requested review from markshannon

Uh oh!

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

Assignees

No one assigned

Labels

awaiting core review skip news stale Stale PR or inactive for long period of time.

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.

5 participants

@sergey-miryanov @nascheme @kumaraditya303 @efimov-mikhail @sobolevn
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.