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 — dict.fromkeys() GC-tracks a non-GC object returned by a subclass constructor (3.15 regression) · Issue #161690 · python/cpython

dict.fromkeys() GC-tracks a non-GC object returned by a subclass constructor (3.15 regression) #161690

New issue
Copy link
New issue
Copy link
Open
Open
dict.fromkeys() GC-tracks a non-GC object returned by a subclass constructor (3.15 regression)#161690
Copy link
Assignees
sergey-miryanov
Labels
3.15bugs and security fixesbugs and security fixes3.16new features, bugs and security fixesnew features, bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@Broyojo
Broyojo
opened on Nov 9, 2026
Issue body actions

Crash report

When a dict subclass's __new__ returns a non-GC object, fromkeys([]) returns it but also attempts to link it into the garbage collector. The call appears to succeed; the next collection segfaults.

import gc
xs = [bytes(40) for _ in range(2000)]
class D(dict):
    __new__ = lambda cls, *a: xs[1000]
r = D.fromkeys([])
assert r is xs[1000]
print("fromkeys returned", type(r).__name__, flush=True)
gc.collect()
print("survived gc.collect()", flush=True)

Recorded on 3.15.2 and main: prints fromkeys returned bytes, then SIGSEGV in gc.collect(). The final print is not reached. The record reports that 3.14.7 returns the object and completes the collection normally.

Cause: Objects/dictobject.c:3430 accepts any object returned by cls(). With an empty iterable there is no insertion to reject a non-mapping. At :3596-3597, _PyObject_GC_IS_TRACKED(d) and _PyObject_GC_TRACK(d) assume that this arbitrary object has a GC header. For bytes it does not, so the macros read and write memory preceding the object. This exit-time tracking was added for gh-151722 in 3.15. Tracking should be restricted to suitable objects created untracked by this function; at minimum it needs a PyObject_IS_GC check.

CPython versions tested on:

3.15, CPython main branch

Operating systems tested on:

Linux

Output from running 'python -VV' on the command line:

Python 3.15.2 (tags/v3.15.2:5e58eaa, Nov 5 2026, 20:20:36) [GCC 11.4.0]


Found while building a Lean 4 model of CPython (https://broyojo.com/python-formalization/). AI tools (Claude and Codex) helped investigate the findings and prepare this report. The examples were rerun on CPython 3.15.2 and main, and the report received technical review. Record: dictset-fromkeys-gc-track-non-gc

Activity

  1. sergey-miryanov commented on Nov 9, 2026

    @sergey-miryanov
    sergey-miryanov
    on Nov 9, 2026
    Contributor
    More actions

    Would you like to open a PR for this? If not, I can take care of it.

    Reacted by David Andrews
  2. Broyojo commented on Nov 9, 2026

    @Broyojo
    Broyojo
    on Nov 9, 2026
    Author
    More actions

    Thanks! Please go ahead, I'd be glad to test the fix against the reproducer.

    Reacted by Sergey Miryanov
  3. sergey-miryanov
    self-assigned this
    on Nov 9, 2026
  4. sergey-miryanov
    added
    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
    3.15bugs and security fixesbugs and security fixes
    3.16new features, bugs and security fixesnew features, bugs and security fixes
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @sergey-miryanov
    sergey-miryanov

Labels

3.15bugs and security fixesbugs and security fixes3.16new features, bugs and security fixesnew features, bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    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.