python / python/cpython

asyncio eager task inherits the creators exception state

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

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

stdlib topic-asyncio type-bug
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

Bug report

Bug description:

Eager tasks created from an except block inherits the creator's exception state, a bare raise re-raises it, and any other exception gets it as __context__.

Repro:

import asyncio


async def bare():
    raise


async def explicit():
    raise ValueError("child's own")


async def main(coro):
    try:
        raise ConnectionError("primary failed")
    except ConnectionError:
        t = asyncio.create_task(coro)
    try:
        await t
    except BaseException as e:
        print(f"  {type(e).__name__}: {e} | __context__: {e.__context__!r}")


for eager in (False, True):
    print("eager_task_factory:", eager)
    for coro_fn in (bare, explicit):
        loop = asyncio.new_event_loop()
        if eager:
            loop.set_task_factory(asyncio.eager_task_factory)
        try:
            loop.run_until_complete(main(coro_fn()))
        finally:
            loop.close()

expected:

eager_task_factory: False
  RuntimeError: No active exception to reraise | __context__: None
  ValueError: child's own | __context__: None
eager_task_factory: True
  RuntimeError: No active exception to reraise | __context__: None
  ValueError: child's own | __context__: None

actually:

eager_task_factory: False
  RuntimeError: No active exception to reraise | __context__: None
  ValueError: child's own | __context__: None
eager_task_factory: True
  ConnectionError: primary failed | __context__: None
  ValueError: child's own | __context__: ConnectionError('primary failed')

proposed fix - run the eager first step under an empty exception state in task_eager_start

    _PyErr_StackItem exc_state = { .exc_value = NULL, .previous_item = NULL };
    _PyErr_StackItem *prev_exc_info = ts->base.exc_info;
    ts->base.exc_info = &exc_state;
    Py_BEGIN_CRITICAL_SECTION(task);
    stepres = task_step_impl(state, task, NULL);
    Py_END_CRITICAL_SECTION();
    ts->base.exc_info = prev_exc_info;

This error also affects python implementation of task, but i think this needs a discussion in a separate issue

Have a fix ready

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs
  • gh-155578

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 bằng cách tái hiện hành vi của eager task trong ví dụ asyncio của issue và kiểm tra entry point task_eager_start. So sánh cách xử lý ngoại lệ eager và non-eager, sau đó xác minh rằng đầu ra mong đợi được báo cáo được tạo ra mà không thay đổi phần triển khai task Python riêng biệt.

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
backend
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 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
30/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.