python / python/cpython

Crash in `DistributionFinder.context` because it is using `vars(self).update(kwargs)` under memory preasure

オープン
#152,083 コメント 4 件 リアクション 0 件 担当者 1 名 GitHub で見る

@deadlovelll がすでに取り組んでいます。

2026年6月29日 から。

stdlib topic-importlib type-crash
主要言語
Python
スター
77.2k
フォーク
35.9k
PR マージ指標
PR 指標を取得中

説明

Crash report

Original report: https://gist.github.com/devdanzin/bf9fec4554c58c2a279b05b7ff8e6d9b (it produces the correct repro, but everything else is not correct, since it is AI generated and can be fuzzy).

Here's the real picture.

This class raises an error:

https://github.com/python/cpython/blob/1785f4b35f899704df0be54cba3776906186b2b1/Lib/importlib/metadata/__init__.py#L742-L750

Repro:

import faulthandler, importlib.metadata
faulthandler.enable()
from _testcapi import set_nomemory
for start in range(40):
    set_nomemory(start)
    try:
        importlib.metadata.metadata("f")
    except BaseException:
        pass
print("done, no crash")

Error:

Fatal Python error: _Py_Dealloc: Deallocator of type 'Context' cleared the current exception
Python runtime state: initialized

Stack (most recent call first):
  File "/Users/sobolev/Desktop/cpython/Lib/importlib/metadata/__init__.py", line 465 in from_name
  File "/Users/sobolev/Desktop/cpython/Lib/importlib/metadata/__init__.py", line 1091 in metadata
  File "/Users/sobolev/Desktop/cpython/ex.py", line 7 in <module>

Extension modules: _testcapi (total: 1)
[1]    14108 abort      ./python.exe ex.py

I tested this hack, no crash:

diff --git Lib/importlib/metadata/__init__.py Lib/importlib/metadata/__init__.py
index 32f4b7d2d6e..43ec93db749 100644
--- Lib/importlib/metadata/__init__.py
+++ Lib/importlib/metadata/__init__.py
@@ -777,7 +777,8 @@ class Context:
         """
 
         def __init__(self, **kwargs):
-            vars(self).update(kwargs)
+            for k, v in kwargs.items():
+                setattr(self, k, v)
 
         @property
         def path(self) -> list[str]:

But, this is clearly not a proper fix. Because for some reason the exception is cleared somewhere.

After:

» ./python.exe ex.py
object address  : 0x20002128190
object refcount : 3
object type     : 0x101267e70
object type name: MemoryError
object repr     : 
lost sys.stderr
object address  : 0x20002128110
object refcount : 3
object type     : 0x101267e70
object type name: MemoryError
object repr     : 
lost sys.stderr
Linked PRs
  • gh-152981

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。