Async query cancellation raises AttributeError on Python 3.10 when adding an exception note

Đang mở Phù hợp với người mới
#736 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.

Đánh giá

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

Hướng nghiên cứu

Đọc FluxCsvParser._parse_flux_response_async và phần xử lý ngoại lệ quanh e.add_note(...); issue xác định đây là đường dẫn gây lỗi. Chạy tests/test_FluxCSVParser.py trên Python 3.10, sau đó kiểm tra rằng các ngoại lệ hủy và hết thời gian chờ vẫn được giữ nguyên khi phản hồi đóng lại. Công việc hoàn tất khi các bài kiểm thử parser đều đạt và các phương thức truy vấn async công khai bị hủy truyền tiếp CancelledError.

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

Mô tả

Specifications
  • Client: latest PyPI release 1.50.0, also current master (be9aa4bf142be1a990bda061b56d581d753b23c8, 1.51.0dev0)
  • Python: CPython 3.10.12 on Linux. CPython 3.13.18 is a working control.
  • InfluxDB: not required. The reproduction uses a local aiohttp HTTP endpoint to send valid annotated CSV and then hold the response open.
Problem

Cancelling a task while an async query consumes CSV raises AttributeError instead of preserving asyncio.CancelledError on Python 3.10. The exception handler in FluxCsvParser._parse_flux_response_async calls e.add_note(...) unconditionally, but that method is not available on Python 3.10. The package still declares Python >=3.7 support.

This affects cancellation through query, query_stream, query_data_frame, and query_data_frame_stream. Code expecting cancellation to propagate receives an ordinary exception instead, which changes cancellation/shutdown control flow.

Reproduction

Install influxdb-client[async]==1.50.0 and pandas in a Python 3.10 environment, then run this standalone script. It uses synthetic credentials and loopback HTTP only.

import asyncio
import json
import sys
from aiohttp import web
from influxdb_client.client.influxdb_client_async import InfluxDBClientAsync

CSV = b'#datatype,string,long,double\n#group,false,false,false\n#default,_result,,\n,result,table,_value\n,,0,1.5\n'

async def case(method, mode):
    started = asyncio.Event()
    finish = asyncio.Event()
    async def handler(request):
        await request.read()
        response = web.StreamResponse(headers={'Content-Type': 'text/csv'})
        await response.prepare(request)
        await response.write(CSV)
        started.set()
        await finish.wait()
        return response
    app = web.Application()
    app.router.add_post('/api/v2/query', handler)
    runner = web.AppRunner(app)
    await runner.setup()
    site = web.TCPSite(runner, '127.0.0.1', 0)
    await site.start()
    port = site._server.sockets[0].getsockname()[1]
    try:
        async with InfluxDBClientAsync(url=f'http://127.0.0.1:{port}', token='synthetic', org='test', timeout=100 if mode == 'timeout' else 5000) as client:
            async def consume():
                result = await getattr(client.query_api(), method)('synthetic query')
                if hasattr(result, '__aiter__'):
                    async for item in result:
                        pass
            task = asyncio.create_task(consume())
            await asyncio.wait_for(started.wait(), 2)
            await asyncio.sleep(0.02)
            if mode == 'cancel':
                task.cancel()
            elif mode == 'complete':
                finish.set()
            try:
                await task
                outcome = 'completed'
            except BaseException as error:
                outcome = type(error).__name__
            expected = {'cancel': 'CancelledError', 'timeout': 'TimeoutError', 'complete': 'completed'}[mode]
            return {'method': method, 'mode': mode, 'outcome': outcome, 'expected': expected, 'pass': outcome == expected}
    finally:
        finish.set()
        await runner.cleanup()

async def main():
    results = []
    for method in ('query', 'query_stream', 'query_data_frame', 'query_data_frame_stream'):
        for mode in ('cancel', 'complete'):
            result = await case(method, mode)
            results.append(result)
            print(json.dumps(result), flush=True)
    print(json.dumps({'python': sys.version, 'passed': sum(r['pass'] for r in results), 'total': len(results)}))

asyncio.run(main())
Expected behavior

All four cancelled calls propagate CancelledError; all four completed-response controls succeed.

Actual behavior

On Python 3.10, each cancellation produces AttributeError; completed responses succeed. The same result occurs on release 1.50.0 and current master. On Python 3.13, all four cancellations preserve CancelledError and all completion controls succeed.

Additional checks
  • A direct parser test injecting a CancelledError or asyncio.TimeoutError reproduces exception replacement on Python 3.10. An injected ValueError is propagated unchanged, and the response is closed in all three cases. All three exceptions are preserved on Python 3.13.
  • The existing tests/test_FluxCSVParser.py suite passes unchanged: 26 tests on each Python version.
  • A private guard around the optional note call preserves both injected exception identities on Python 3.10 and restores all four public-API cancellation cases without changing the completed-response controls; the same 26 native parser tests still pass. No PR is included here.

This report is specifically about the Python-version compatibility regression in exception annotation introduced by #679, not about whether large queries should time out or about the earlier #671 query-size report. Timer expiry in the loopback setup still surfaces the existing cancellation behavior on newer Python; this report does not claim to fix or redefine that separate behavior.

AI assistance was used to investigate and run these synthetic reproductions. No production application or production InfluxDB instance is involved.

Ngôn ngữ chính
Python
Star
796
Fork
191
Merge trung bình
12 ngày 13 giờ
Pull request đã merge (30 ngày)
5

Chuẩn bị môi trường

  • Không có Dockerfile hay tệp Docker Compose
  • Có mẫu pull request
  • Không có 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.

Issue khác của influxdata/influxdb-client-python

Tất cả issue của influxdata/influxdb-client-python

Issue tương tự

Thêm issue về Python

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.