diff --git a/ldclient/impl/datasourcev2/async_polling.py b/ldclient/impl/datasourcev2/async_polling.py index d59631e8..ee48493a 100644 --- a/ldclient/impl/datasourcev2/async_polling.py +++ b/ldclient/impl/datasourcev2/async_polling.py @@ -116,8 +116,7 @@ async def sync(self, ss: SelectorStore) -> AsyncGenerator[Update, None]: occurs. """ log.info("Starting PollingDataSourceV2 synchronizer") - self._interrupt_event.clear() - self._stop.clear() + # The flags are not cleared here. Stop can occur before sync starts, and sync then ends at once. try: while self._stop.is_set() is False: result = await self._fetch(ss) diff --git a/ldclient/impl/datasourcev2/polling.py b/ldclient/impl/datasourcev2/polling.py index 8a913b37..038815bd 100644 --- a/ldclient/impl/datasourcev2/polling.py +++ b/ldclient/impl/datasourcev2/polling.py @@ -107,8 +107,7 @@ def sync(self, ss: SelectorStore) -> Generator[Update, None, None]: occurs. """ log.info("Starting PollingDataSourceV2 synchronizer") - self._interrupt_event.clear() - self._stop.clear() + # The flags are not cleared here. Stop can occur before sync starts, and sync then ends at once. while self._stop.is_set() is False: result = self._fetch(ss) decision = map_polling_result(result) diff --git a/ldclient/impl/datasystem/async_fdv2.py b/ldclient/impl/datasystem/async_fdv2.py index f190a67e..f14eaa87 100644 --- a/ldclient/impl/datasystem/async_fdv2.py +++ b/ldclient/impl/datasystem/async_fdv2.py @@ -393,6 +393,11 @@ async def stop(self): # Wait for the coordinator's background work to complete await self._runner.stop_all(timeout=5.0) + # The data source stops here, whichever synchronizer was active. + self._data_source_status_provider.update_status( + DataSourceState.OFF, self._data_source_status_provider.status.error + ) + # Close the store await self._store.close() @@ -549,6 +554,10 @@ async def _synchronizer_loop(self, set_on_ready: AsyncEvent): synchronizer, set_on_ready, current_index != 0 ) + # Stop ends the synchronizer. That is not a failure, so the directive is ignored. + if self._stop_event.is_set(): + break + if directive == ConditionDirective.FDV1: # Abandon all synchronizers and use only fdv1 fallback log.warning("Falling back to FDv1 protocol") diff --git a/ldclient/impl/datasystem/fdv2.py b/ldclient/impl/datasystem/fdv2.py index ae8281f2..cb979830 100644 --- a/ldclient/impl/datasystem/fdv2.py +++ b/ldclient/impl/datasystem/fdv2.py @@ -374,6 +374,11 @@ def stop(self): if thread.is_alive(): log.warning("Thread %s did not terminate in time", thread.name) + # The data source stops here, whichever synchronizer was active. + self._data_source_status_provider.update_status( + DataSourceState.OFF, self._data_source_status_provider.status.error + ) + # Close the store self._store.close() @@ -515,6 +520,10 @@ def synchronizer_loop(self: 'FDv2'): synchronizer, set_on_ready, current_index != 0 ) + # Stop ends the synchronizer. That is not a failure, so the directive is ignored. + if self._stop_event.is_set(): + break + if directive == ConditionDirective.FDV1: # Abandon all synchronizers and use only fdv1 fallback log.warning("Falling back to FDv1 protocol") diff --git a/ldclient/testing/impl/datasourcev2/test_async_polling.py b/ldclient/testing/impl/datasourcev2/test_async_polling.py index 622a1a9e..4281eb31 100644 --- a/ldclient/testing/impl/datasourcev2/test_async_polling.py +++ b/ldclient/testing/impl/datasourcev2/test_async_polling.py @@ -346,3 +346,17 @@ async def close(self) -> None: assert "connection refused" in interrupted.error.message assert interrupted.fallback_to_fdv1 is False assert valid.state == DataSourceState.VALID + + +@pytest.mark.asyncio +async def test_sync_does_not_poll_when_stopped_before_it_starts(): + requester = MockPollingRequester([_Success(value=(_valid_change_set(), {}))]) + src = AsyncPollingDataSource(poll_interval=0.01, requester=requester) + + await src.stop() + + async def collect(): + return [update async for update in src.sync(_ss())] + + assert await asyncio.wait_for(collect(), timeout=1) == [] + assert requester.call_times == [] diff --git a/ldclient/testing/impl/datasourcev2/test_polling_synchronizer.py b/ldclient/testing/impl/datasourcev2/test_polling_synchronizer.py index d1ff4f48..d6198f49 100644 --- a/ldclient/testing/impl/datasourcev2/test_polling_synchronizer.py +++ b/ldclient/testing/impl/datasourcev2/test_polling_synchronizer.py @@ -536,3 +536,13 @@ def test_requester_exception_interrupts_and_recovers(): assert "connection refused" in interrupted.error.message assert interrupted.fallback_to_fdv1 is False assert valid.state == DataSourceState.VALID + + +def test_sync_does_not_poll_when_stopped_before_it_starts(): + requester = ListBasedRequester(results=iter([])) + synchronizer = PollingDataSource(poll_interval=0.01, requester=requester) + + synchronizer.stop() + + # The requester has no results, so a poll would yield an INTERRUPTED update. + assert next(synchronizer.sync(MockSelectorStore(Selector.no_selector())), None) is None diff --git a/ldclient/testing/impl/datasystem/test_async_fdv2.py b/ldclient/testing/impl/datasystem/test_async_fdv2.py index 1a0a2d88..a0e3ba9b 100644 --- a/ldclient/testing/impl/datasystem/test_async_fdv2.py +++ b/ldclient/testing/impl/datasystem/test_async_fdv2.py @@ -133,6 +133,25 @@ async def test_async_fdv2_basic_start_stop(): await fdv2.stop() +@pytest.mark.asyncio +@pytest.mark.parametrize("synchronizer_count", [1, 2]) +async def test_async_fdv2_stop_reports_off_without_a_synchronizer_failure_warning(caplog, synchronizer_count): + td = TestDataV2.data_source() + fdv2 = AsyncFDv2( + AsyncConfig(sdk_key="dummy"), + DataSystemConfig(initializers=None, synchronizers=[td.async_builder] * synchronizer_count), + ) + ready_event = asyncio.Event() + fdv2.start(ready_event) + await asyncio.wait_for(ready_event.wait(), timeout=2) + + await fdv2.stop() + + assert fdv2.data_source_status_provider.status.state == DataSourceState.OFF + # Stop ends the synchronizer. That is not a failure, so it is not reported. + assert not any(record.levelno >= logging.WARNING for record in caplog.records) + + @pytest.mark.asyncio async def test_async_fdv2_synchronizer_receives_updates(): td = TestDataV2.data_source() diff --git a/ldclient/testing/impl/datasystem/test_fdv2_datasystem.py b/ldclient/testing/impl/datasystem/test_fdv2_datasystem.py index 6cc98a85..f9305641 100644 --- a/ldclient/testing/impl/datasystem/test_fdv2_datasystem.py +++ b/ldclient/testing/impl/datasystem/test_fdv2_datasystem.py @@ -6,6 +6,7 @@ from threading import Event from typing import List +import pytest from mock import Mock from ldclient.config import ( @@ -153,6 +154,24 @@ def listener(flag_change: FlagChange): assert len(changes) == 0 +@pytest.mark.parametrize("synchronizer_count", [1, 2]) +def test_stop_reports_off_without_a_synchronizer_failure_warning(caplog, synchronizer_count): + td = TestDataV2.data_source() + fdv2 = FDv2( + Config(sdk_key="dummy"), + DataSystemConfig(initializers=None, synchronizers=[td.builder] * synchronizer_count), + ) + set_on_ready = Event() + fdv2.start(set_on_ready) + assert set_on_ready.wait(1), "Data system did not become ready in time" + + fdv2.stop() + + assert fdv2.data_source_status_provider.status.state == DataSourceState.OFF + # Stop ends the synchronizer. That is not a failure, so it is not reported. + assert not any(record.levelno >= logging.WARNING for record in caplog.records) + + def test_fdv2_data_availability_is_refreshed_with_data(): td = TestDataV2.data_source() data_system_config = DataSystemConfig(