Skip to content

gh-142349: Fix refcount corruption in lazy import specialization - #144733

Merged
Yhg1s merged 1 commit into
python:mainfrom
pablogsal:lazy-cleanup-2
Feb 12, 2026
Merged

Yhg1s merged 1 commit into
python:mainfrom
pablogsal:lazy-cleanup-2

Conversation

@pablogsal

@pablogsal pablogsal commented Feb 12, 2026 •

Copy link
Copy Markdown
Member

_PyDict_LookupIndexAndValue() returns a borrowed reference via _Py_dict_lookup(), but specialize_load_global_lock_held() called Py_DECREF(value) on it when bailing out for lazy imports. Each time the adaptive counter fired while a lazy import was still in globals, this stole one reference from the dict's object. With 8+ threads racing through LOAD_GLOBAL during concurrent lazy import resolution, enough triggers accumulated to drive the refcount to zero while the dict and other threads still referenced the object, causing use-after-free.

@pablogsal
pablogsal requested a review from DinoV February 12, 2026 08:26
@pablogsal
pablogsal requested a review from Yhg1s February 12, 2026 08:26
@pablogsal pablogsal changed the title gh-142351: Fix refcount corruption in lazy import specialization Feb 12, 2026
Comment thread Python/import.c Resolved

@Yhg1s Yhg1s left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is good enough for a free-threaded build. The error case happens when _PyImport_LoadLazyImportTstate is called with a borrowed reference. The GIL won't be protecting that borrowed reference in a free-threaded build, so the INCREF already happens too late. We need the caller to acquire an owned reference instead, in a safe way. But... where does this happen? As far as I can tell the callers all own the reference they pass.

Comment thread Python/import.c Resolved
Comment thread Python/import.c Resolved
@pablogsal
pablogsal marked this pull request as draft February 12, 2026 10:49
@pablogsal

Copy link
Copy Markdown
Member Author

But... where does this happen? As far as I can tell the callers all own the reference they pass.

If that were true then there would not be crashes no?

@Yhg1s

Yhg1s commented Feb 12, 2026

Copy link
Copy Markdown
Member

If that were true then there would not be crashes no?

Unless the problem is actually something different, yes :) I would like to know which it is.

_PyDict_LookupIndexAndValue() returns a borrowed reference via
_Py_dict_lookup(), but specialize_load_global_lock_held() called
Py_DECREF(value) on it when bailing out for lazy imports. Each time
the adaptive counter fired while a lazy import was still in globals,
this stole one reference from the dict's object. With 8+ threads
racing through LOAD_GLOBAL during concurrent lazy import resolution,
enough triggers accumulated to drive the refcount to zero while the
dict and other threads still referenced the object, causing
use-after-free.
@pablogsal
pablogsal marked this pull request as ready for review February 12, 2026 11:21
@pablogsal

Copy link
Copy Markdown
Member Author

If that were true then there would not be crashes no?

Unless the problem is actually something different, yes :) I would like to know which it is.

Narrator: and the problem was indeed something different. Kind of

@Yhg1s
Yhg1s enabled auto-merge (squash) February 12, 2026 11:22
@Yhg1s
Yhg1s merged commit 072cd7c into python:main Feb 12, 2026
47 checks passed
thunder-coding pushed a commit to thunder-coding/cpython that referenced this pull request Feb 15, 2026
python#144733)

Remove spurious Py_DECREF on borrowed ref in LOAD_GLOBAL specialization

_PyDict_LookupIndexAndValue() returns a borrowed reference via
_Py_dict_lookup(), but specialize_load_global_lock_held() called
Py_DECREF(value) on it when bailing out for lazy imports. Each time
the adaptive counter fired while a lazy import was still in globals,
this stole one reference from the dict's object. With 8+ threads
racing through LOAD_GLOBAL during concurrent lazy import resolution,
enough triggers accumulated to drive the refcount to zero while the
dict and other threads still referenced the object, causing
use-after-free.
pablogsal added a commit to pablogsal/cpython that referenced this pull request Feb 15, 2026
Add a regression test ensuring that circular lazy imports raise a proper
error (ImportCycleError) instead of crashing with a segfault. The crash
was fixed in pythongh-144733 but no test was added at the time.
pablogsal added a commit to pablogsal/cpython that referenced this pull request Feb 15, 2026
Add a regression test ensuring that circular lazy imports raise a proper
error (ImportCycleError) instead of crashing with a segfault. The crash
was fixed in pythongh-144733 but no test was added at the time.
pablogsal added a commit that referenced this pull request Feb 15, 2026
Add a regression test ensuring that circular lazy imports raise a proper
error (ImportCycleError) instead of crashing with a segfault. The crash
was fixed in gh-144733 but no test was added at the time.
brijkapadia pushed a commit to brijkapadia/cpython that referenced this pull request Feb 28, 2026
python#144733)

Remove spurious Py_DECREF on borrowed ref in LOAD_GLOBAL specialization

_PyDict_LookupIndexAndValue() returns a borrowed reference via
_Py_dict_lookup(), but specialize_load_global_lock_held() called
Py_DECREF(value) on it when bailing out for lazy imports. Each time
the adaptive counter fired while a lazy import was still in globals,
this stole one reference from the dict's object. With 8+ threads
racing through LOAD_GLOBAL during concurrent lazy import resolution,
enough triggers accumulated to drive the refcount to zero while the
dict and other threads still referenced the object, causing
use-after-free.
brijkapadia pushed a commit to brijkapadia/cpython that referenced this pull request Feb 28, 2026
…) (python#144838)

Add a regression test ensuring that circular lazy imports raise a proper
error (ImportCycleError) instead of crashing with a segfault. The crash
was fixed in pythongh-144733 but no test was added at the time.
ljfp pushed a commit to ljfp/cpython that referenced this pull request Apr 25, 2026
python#144733)

Remove spurious Py_DECREF on borrowed ref in LOAD_GLOBAL specialization

_PyDict_LookupIndexAndValue() returns a borrowed reference via
_Py_dict_lookup(), but specialize_load_global_lock_held() called
Py_DECREF(value) on it when bailing out for lazy imports. Each time
the adaptive counter fired while a lazy import was still in globals,
this stole one reference from the dict's object. With 8+ threads
racing through LOAD_GLOBAL during concurrent lazy import resolution,
enough triggers accumulated to drive the refcount to zero while the
dict and other threads still referenced the object, causing
use-after-free.
ljfp pushed a commit to ljfp/cpython that referenced this pull request Apr 25, 2026
…) (python#144838)

Add a regression test ensuring that circular lazy imports raise a proper
error (ImportCycleError) instead of crashing with a segfault. The crash
was fixed in pythongh-144733 but no test was added at the time.
@pablogsal
pablogsal deleted the lazy-cleanup-2 branch September 24, 2026 17:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

3 participants