python / python/cpython

concurrent.interpreters: Interpreter.call() crashes when the result fails to unpickle in the caller

Đang mở
#151,892 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

3.14 3.15 3.16 extension-modules topic-subinterpreters type-bug type-crash
Ngôn ngữ chính
Python
Star
77.2k
Fork
36k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

When Interpreter.call() returns a value, that value is pickled in the callee interpreter and reconstructed (unpickled) in the calling interpreter on the way out of the cross-interpreter session, inside _PyXI_Exit() (Python/crossinterp.c). If the reconstruction step raises — e.g. the returned object's __reduce__ produces a callable that raises during unpickling — the raised exception is left set while _PyXI_Exit() applies its "preserve failure" handling. On a debug build this trips assert(!_PyErr_Occurred(tstate)) and aborts the process (SIGABRT); on a release build the stale exception is clobbered and the resulting error state is wrong.

Reproduction (debug build)
import sys, os, tempfile, importlib
from textwrap import dedent
from concurrent import interpreters

d = tempfile.mkdtemp(); sys.path.insert(0, d)
with open(os.path.join(d, "b.py"), "w") as f:
    f.write(dedent('''
        def _boom():
            raise ValueError("unpickling failed")
        class Bomb:
            def __reduce__(self):
                return (_boom, ())
        def mk():
            return Bomb()
    '''))
m = importlib.import_module("b")
ip = interpreters.create()
ip.prepare_main(_d=d)
ip.exec("import sys; sys.path.insert(0, _d); import b")
ip.call(m.mk)   # debug build: Assertion failed in _PyXI_Exit -> SIGABRT

This affects only the call() path (and any other _PyXI_Enter/_PyXI_Exit-bracketed result-preserve path). Queue/channel get()/recv() and exec() do not go through this path and are unaffected. Since call() runs the caller's own code in the subinterpreter, this is a robustness / crash bug, not a security issue.

Expected: call() should raise a normal Python exception instead of aborting.

Fix

Have the reconstruction-failure branch consume the live exception and propagate it as the failure, mirroring how the active-session _pop_preserved path already routes errors through capture_session_error. With the fix, call() raises NotShareableError with the underlying error reachable as the cause. A self-contained regression test is added in Lib/test/test_interpreters/test_api.py.

Affected versions

main only. The result-preserve machinery (_PyXI_Preserve / _finish_preserved, gh-133484) and the xi_error_* error-handling rewrite (gh-135369) that this touches both landed after the 3.14.0b1 freeze, so the affected code path does not exist on 3.14 or earlier.

Related

A separate, distinct crossinterp NULL dereference on the queue/channel receive path is tracked in #151862.

Linked PR

A PR follows.

Linked PRs
  • gh-151893

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu trong Python/crossinterp.c tại _PyXI_Exit() và so sánh cách xử lý lỗi tái dựng với đường dẫn _pop_preserved của phiên hoạt động và capture_session_error. Sau đó đọc phần kiểm thử hồi quy trong Lib/test/test_interpreters/test_api.py và chạy kiểm thử đó; hoàn tất khi call() raise NotShareableError với lỗi bên dưới làm nguyên nhân thay vì bị abort.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
c, python
Lĩnh vực
compilers
Loại issue
Lỗi
Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
25/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.