Skip to content

time.strptime() with lambda/function call triggers GC finalization error in Python 3.12 (struct_time n_fields missing in tp_clear) #148551

Description

@SimGo123

Bug report

Bug description:

Summary

When calling time.strptime() inside a function (including a lambda), CPython 3.12 can emit a GC finalization error during object teardown:

Exception ignored in tp_clear of: <class 'dict'>
TypeError: Missed attribute 'n_fields' of type time.struct_time

The issue is not reproducible when calling time.strptime() directly in assignment context, and appears to depend on temporary object lifetime and function call boundaries.


Environment

  • Python: 3.12.3 (also reproducible in locally built 3.12 from source)
  • OS: Linux (x86_64)
  • Compiler: GCC 13.3.0
  • Repro isolation: works under env -i (no environment variables required)

Minimal Reproducer

python3 -m venv venv && env -i ./venv/bin/python3 -c "
import time

func = lambda: time.strptime(
    'Fri, 27 Mar 2026 10:00:00',
    '%a, %d %b %Y %H:%M:%S'
)

x = func()
"

Observed output

Exception ignored in tp_clear of: <class 'dict'>
TypeError: Missed attribute 'n_fields' of type time.struct_time

Non-reproducible variant

The issue does NOT occur when the same call is executed directly:

import time

x = time.strptime(
    'Fri, 27 Mar 2026 10:00:00',
    '%a, %d %b %Y %H:%M:%S'
)

This version runs cleanly without any GC or finalization errors.


Additional observations

  • Reproducible in isolated environment (env -i)
  • Reproducible in fresh virtual environments
  • Not dependent on external packages or site customization
  • Appears sensitive to function/lambda call boundaries and temporary object lifetime
  • Error originates during garbage collection finalization (tp_clear)
  • Involves time.struct_time internal state (n_fields attribute missing during teardown)

Expected behavior

time.strptime() should behave identically regardless of whether it is:

  • called directly in assignment context
  • returned from a function
  • executed inside a lambda

No GC-related exceptions should be emitted during object finalization.


Actual behavior

Only function/lambda invocation triggers:

Exception ignored in tp_clear of: <class 'dict'>
TypeError: Missed attribute 'n_fields' of type time.struct_time

Notes / Hypothesis

This appears to be a CPython 3.12 GC finalization edge case involving:

  • time.struct_time (C-level object)
  • temporary object lifetime in function frames
  • garbage collector / tp_clear execution path
  • possible refcount vs GC interaction during frame teardown

The issue may be triggered by subtle differences in:

  • stack frame lifetime
  • temporary object escaping rules
  • lambda/function call cleanup ordering

Impact

  • Unexpected exceptions during interpreter shutdown / GC
  • Potentially indicates memory safety issue in CPython core for struct_time
  • Difficult-to-diagnose nondeterministic behavior depending on call context

Notes

  • I generated this report with ChatGPT because I'm not familar with CPython / garbage collection
  • I came across this issue when using feedparser inside a function and getting:
Exception ignored in tp_clear of: <class 'dict'>
SystemError: ../Objects/longobject.c:577: bad argument to internal function

While attempting to reduce this to a minimal reproducible example, I encountered the mentioned issue and believe they could be related (both only occur when called in a function)

CPython versions tested on:

3.12

Operating systems tested on:

Linux

Activity

  1. sergey-miryanov commented on Apr 14, 2026

    @sergey-miryanov
    Contributor

    I can't reproduce on current 3.12 branch.

    [X:\Work\cpython\3.12] [ 3.15.0] [ gh-145247-tuple-from-pair] [3.12 ≡ +0 ~11 -0 !] [2026-04-14 18:45:49] [󰅒 49.668]
    ➜ .\python.bat -X faulthandler ..\issues\b148551.py
    Running Debug|x64 interpreter...
    
    [X:\Work\cpython\3.12] [ 3.15.0] [ gh-145247-tuple-from-pair] [3.12 ≡ +0 ~11 -0 !] [2026-04-14 18:45:54] [󰅒 0.231]
    ➜ .\python.bat -VV
    Running Debug|x64 interpreter...
    Python 3.12.13+ (heads/3.12-dirty:82b53a619ed, Apr 14 2026, 18:45:34) [MSC v.1944 64 bit (AMD64)]
    
    [X:\Work\cpython\3.12] [ 3.15.0] [ gh-145247-tuple-from-pair] [3.12 ≡ +0 ~11 -0 !] [2026-04-14 18:46:25] [󰅒 0.042]
    ➜ cat ..\issues\b148551.py
    import time
    
    func = lambda: time.strptime(
        'Fri, 27 Mar 2026 10:00:00',
        '%a, %d %b %Y %H:%M:%S'
    )
    
    x = func()
    
  2. added
    pendingThe issue will be closed if no feedback is provided
    3.12only security fixes
    on Apr 14, 2026
  3. StanFromIreland commented on Apr 14, 2026

    @StanFromIreland
    Member

    I think it was fixed by 4b63cd1, which made it into 3.12.6. Can your reproduce this on that or a newer version?

  4. SimGo123 commented on Apr 14, 2026

    @SimGo123
    Author

    @StanFromIreland Yes, doesn't occur in 3.12.6. Closing it.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.12only security fixesextension-modulesC modules in the Modules dirpendingThe issue will be closed if no feedback is providedtype-bugAn unexpected behavior, bug, or error

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions