From cec47208a65c0db56bf5afc09dd0b23cad437220 Mon Sep 17 00:00:00 2001 From: Sergey Popov <167248754+duckduckduck1@users.noreply.github.com> Date: Thu, 24 Sep 2026 12:40:13 +0400 Subject: [PATCH 1/2] Fix flaky topic reader reconnect test --- ydb/_topic_reader/topic_reader_asyncio_test.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/ydb/_topic_reader/topic_reader_asyncio_test.py b/ydb/_topic_reader/topic_reader_asyncio_test.py index c018604f..fede3c3c 100644 --- a/ydb/_topic_reader/topic_reader_asyncio_test.py +++ b/ydb/_topic_reader/topic_reader_asyncio_test.py @@ -11,6 +11,7 @@ import pytest from ydb import issues +from ydb.retries import BackoffSettings, RetrySettings from . import datatypes, topic_reader_asyncio from .datatypes import PublicBatch, PublicMessage from .topic_reader import PublicReaderSettings @@ -1592,8 +1593,10 @@ async def timeout_receive(timeout=None): class TestReaderReconnector: async def test_reconnect_on_repeatable_error(self, monkeypatch): test_error = issues.Overloaded("test error") + message_waited = asyncio.Event() async def wait_error(): + await message_waited.wait() raise test_error reader_stream_mock_with_error = mock.Mock(ReaderStream) @@ -1601,6 +1604,7 @@ async def wait_error(): reader_stream_mock_with_error.wait_error = mock.AsyncMock(side_effect=wait_error) async def wait_messages_with_error(): + message_waited.set() raise test_error reader_stream_mock_with_error.wait_messages = mock.AsyncMock(side_effect=wait_messages_with_error) @@ -1630,9 +1634,19 @@ async def stream_create( else: raise Exception("unexpected create stream") + settings = PublicReaderSettings("", "") + monkeypatch.setattr( + settings, + "_retry_settings", + lambda: RetrySettings(slow_backoff_settings=BackoffSettings(slot_duration=0)), + ) + with mock.patch.object(ReaderStream, "create", stream_create): - reconnector = ReaderReconnector(mock.Mock(), PublicReaderSettings("", "")) - await wait_for_fast(reconnector.wait_message()) + reconnector = ReaderReconnector(mock.Mock(), settings) + try: + await wait_for_fast(reconnector.wait_message()) + finally: + await reconnector.close(False) reader_stream_mock_with_error.wait_error.assert_any_await() reader_stream_mock_with_error.wait_messages.assert_any_await() From 93ba53f0ba2181c08c739c4da074869fa65dc3a6 Mon Sep 17 00:00:00 2001 From: Oleg Ovcharuk Date: Tue, 3 Nov 2026 16:03:42 +0300 Subject: [PATCH 2/2] Refactor wait_message to use asyncio task Refactor wait_message handling to use asyncio task cancellation. Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- ydb/_topic_reader/topic_reader_asyncio_test.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/ydb/_topic_reader/topic_reader_asyncio_test.py b/ydb/_topic_reader/topic_reader_asyncio_test.py index fede3c3c..b90a1b61 100644 --- a/ydb/_topic_reader/topic_reader_asyncio_test.py +++ b/ydb/_topic_reader/topic_reader_asyncio_test.py @@ -1643,9 +1643,15 @@ async def stream_create( with mock.patch.object(ReaderStream, "create", stream_create): reconnector = ReaderReconnector(mock.Mock(), settings) + wait_message_task = asyncio.create_task(reconnector.wait_message()) try: - await wait_for_fast(reconnector.wait_message()) + await wait_for_fast(wait_message_task) finally: + wait_message_task.cancel() + try: + await wait_message_task + except asyncio.CancelledError: + pass await reconnector.close(False) reader_stream_mock_with_error.wait_error.assert_any_await()