Skip to content

Segfault on re-entrant lazy import pandas #159408

Open

Description

@inoa-jboliveira

Crash report

With PYTHON_LAZY_IMPORTS=all, I get a segfault with pandas being initialized inside a freezegun, likely because GC frees it somehow.

Tested with freezegun==1.5.5 and pandas==3.0.6, require 2 files below

lib.py

import pandas

repro.py

import lib
from freezegun import freeze_time

for _ in range(2):          # the first call is fine, the second crashes
    with freeze_time("2024-01-01"):
        pass

Calling it

$ PYTHON_LAZY_IMPORTS=all python repro.py
Fatal Python error: Segmentation fault
Current thread ... (most recent call first):
  Garbage-collecting
  File "<frozen importlib._bootstrap_external>", line 500 in _compile_bytecode
  ...

freezegun runs dir() and getattr() over every module. That getattr resolves the lazy pandas reference, but if i try to reproduce without freezegun, it gives an error but no segfault

repro-python.py

import sys
import pandas

for _ in range(2):
    for m in list(sys.modules.values()):
        for n in dir(m):
            try: getattr(m, n)
            except ModuleNotFoundError: pass

Running

$ PYTHON_LAZY_IMPORTS=all python repro-python.py
  File "pandas/_libs/tslibs/np_datetime.pyx", line 33, in init pandas._libs.tslibs.np_datetime
  File ".../numpy/__init__.cython-30.pxd", line 1081, in numpy.import_array
ImportError: numpy._core.multiarray failed to import

Possibly related to #158402 or #159266 but very different crashes

CPython versions tested on:

3.15

Operating systems tested on:

Linux

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

Python 3.15.0 (main, Oct 9 2026, 15:24:40) [Clang 22.1.3 ]

Activity

  1. zolaski333 commented on Oct 14, 2026

    @zolaski333
    Contributor

    The GC crash also reproduces without lazy imports on Python 3.12, and without freezegun by replacing datetime classes before importing a small Cython extension. The native stack recurses between the extension traversal callback and subtype_traverse().

    Cython's native datetime declarations currently resolve mutable module attributes. The proposed fix in cython/cython#8059 now supports C type objects in extern declarations generically. datetime.pxd obtains the native types from PyDateTime_CAPI; the compiler has no datetime-specific handling. The interface remains under review. Downstream extensions would need regeneration and rebuilding.

    There is also a separate pandas initialization problem: its option-registration and datetime/parser capsule imports can be deferred in global lazy mode. pandas-dev/pandas#71173 tracks that, and pandas-dev/pandas#71178 proposes explicit initialization imports.

    With both proposed changes and rebuilt pandas, the reworked fix passes the original two-file scenario in five fresh Linux processes with empty stderr and no CPython source changes. The reduced C/C++ regressions pass on Windows and Linux, including free-threaded CPython. This does not establish general pandas compatibility with global lazy imports: a separate Timestamp import cycle remains.

    A verified workaround is to initialize pandas with normal imports before entering freeze_time:

    import sys
    
    mode = sys.get_lazy_imports()
    sys.set_lazy_imports('normal')
    try:
        import pandas
    finally:
        sys.set_lazy_imports(mode)
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

    interpreter-core(Objects, Python, Grammar, and Parser dirs)topic-lazy-importstype-crashA hard crash of the interpreter, possibly with a core dump

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions