diff --git a/CHANGELOG.md b/CHANGELOG.md index c6d5588b..af891a77 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ 1. [#732](https://github.com/influxdata/influxdb-client-python/pull/732): Apply the `#default` annotation in `FluxCsvParser` when it comes before `#datatype`. 1. [#733](https://github.com/influxdata/influxdb-client-python/pull/733): Write the last batch when the batching `WriteApi` is closed in an `atexit` handler instead of dropping it with "cannot schedule new futures after interpreter shutdown". 1. [#734](https://github.com/influxdata/influxdb-client-python/pull/734): Match `data_frame_timestamp_column` by exact column name, so a field whose name is part of the timestamp column name (e.g. `Open` with `OpenTime`) is no longer dropped. +1. [#739](https://github.com/influxdata/influxdb-client-python/pull/739): Async CSV query cancellation no longer raises `AttributeError` with pre 3.11 release Python platforms. ## 1.50.0 [2026-01-23] diff --git a/influxdb_client/client/flux_csv_parser.py b/influxdb_client/client/flux_csv_parser.py index cdfa5187..16cb5ebb 100644 --- a/influxdb_client/client/flux_csv_parser.py +++ b/influxdb_client/client/flux_csv_parser.py @@ -149,7 +149,7 @@ async def _parse_flux_response_async(self): yield df except BaseException as e: e_type = type(e).__name__ - if "CancelledError" in e_type or "TimeoutError" in e_type: + if ("CancelledError" in e_type or "TimeoutError" in e_type) and hasattr(e, "add_note"): e.add_note("Stream cancelled during read. Recommended: Check Influxdb client `timeout` setting.") raise finally: diff --git a/tests/test_FluxCSVParserAsyncExceptions.py b/tests/test_FluxCSVParserAsyncExceptions.py new file mode 100644 index 00000000..32eded0f --- /dev/null +++ b/tests/test_FluxCSVParserAsyncExceptions.py @@ -0,0 +1,32 @@ +import asyncio +import unittest +from unittest.mock import Mock + +from influxdb_client.client.flux_csv_parser import FluxCsvParser, FluxSerializationMode + + +class FluxCsvParserAsyncExceptionTest(unittest.TestCase): + def test_preserves_exception_and_closes_response(self): + async def check(error): + response = Mock() + parser = FluxCsvParser(response, FluxSerializationMode.stream) + + async def rows(): + raise error + yield # Make this an async iterator, like the CSV reader. + + parser._reader = rows() + with self.assertRaises(type(error)) as raised: + await parser.generator_async().__anext__() + self.assertIs(raised.exception, error) + response.close.assert_called_once() + if hasattr(error, "add_note") and isinstance(error, (asyncio.CancelledError, asyncio.TimeoutError)): + self.assertTrue(any("timeout" in note for note in error.__notes__)) + + loop = asyncio.new_event_loop() + try: + for error in (asyncio.CancelledError(), asyncio.TimeoutError(), ValueError("invalid row")): + with self.subTest(error=type(error).__name__): + loop.run_until_complete(check(error)) + finally: + loop.close()