diff --git a/ldclient/impl/datasourcev2/polling.py b/ldclient/impl/datasourcev2/polling.py index 8a913b37..f302de51 100644 --- a/ldclient/impl/datasourcev2/polling.py +++ b/ldclient/impl/datasourcev2/polling.py @@ -111,6 +111,9 @@ def sync(self, ss: SelectorStore) -> Generator[Update, None, None]: self._stop.clear() while self._stop.is_set() is False: result = self._fetch(ss) + if self._stop.is_set(): + # Stop can occur while the request is in progress. The result is not reported. + break decision = map_polling_result(result) yield decision.update @@ -135,7 +138,10 @@ def _fetch(self, ss: SelectorStore) -> PollingResult: return self._requester.fetch(ss.selector()) except Exception as e: # pylint: disable=broad-except msg = f"Error: Exception encountered when polling for flags. {e}" - log.warning(msg) + if self._stop.is_set(): + log.debug(msg) + else: + log.warning(msg) return _Fail(error=msg, exception=e) def _poll(self, ss: SelectorStore) -> BasisResult: @@ -144,7 +150,11 @@ def _poll(self, ss: SelectorStore) -> BasisResult: return polling_result_to_basis(result) except Exception as e: # pylint: disable=broad-except msg = f"Error: Exception encountered when updating flags. {e}" - log.exception(msg) + if self._stop.is_set(): + # The SDK is shutting down, so the error is not reported. + log.debug(msg) + else: + log.exception(msg) return _Fail(error=msg, exception=e) diff --git a/ldclient/impl/datasystem/fdv2.py b/ldclient/impl/datasystem/fdv2.py index ae8281f2..b056a0d6 100644 --- a/ldclient/impl/datasystem/fdv2.py +++ b/ldclient/impl/datasystem/fdv2.py @@ -33,6 +33,7 @@ DataStoreStatus, FeatureStore, FlagChange, + Initializer, OverrideSource, ReadOnlyStore, Synchronizer @@ -260,6 +261,7 @@ def __init__( self._stop_event = Event() self._lock = ReadWriteLock() self._active_synchronizer: Optional[Synchronizer] = None + self._active_initializer: Optional[Initializer] = None self._threads: List[Thread] = [] self._environment_id: Optional[str] = None @@ -361,6 +363,13 @@ def stop(self): self._stop_event.set() with self._lock.write(): + # An initializer can match the protocol without the stop method. + stop_initializer = getattr(self._active_initializer, "stop", None) + if stop_initializer is not None: + try: + stop_initializer() + except Exception as e: + log.error("Error stopping active initializer: %s", e) if self._active_synchronizer is not None: try: self._active_synchronizer.stop() @@ -387,6 +396,10 @@ def _run_main_loop(self, set_on_ready: Event): # Run initializers first fallback_requested = self._run_initializers(set_on_ready) + # Stop can interrupt an initializer. The synchronizers then do not start. + if self._stop_event.is_set(): + return + # If an initializer asked the SDK to fall back to FDv1, halt the # configured FDv2 chain and switch terminally to the FDv1 Fallback # Synchronizer (or transition to OFF if none is configured). @@ -436,7 +449,20 @@ def _run_initializers(self, set_on_ready: Event) -> bool: initializer = initializer_builder.build(self._config) log.info("Attempting to initialize via %s", initializer.name) - basis_result = initializer.fetch(self._store) + # Stop sets the stop event before it takes the lock. So stop either sees this + # initializer and stops it, or this check sees the stop event. + with self._lock.write(): + if self._stop_event.is_set(): + return False + self._active_initializer = initializer + try: + basis_result = initializer.fetch(self._store) + finally: + with self._lock.write(): + self._active_initializer = None + + if self._stop_event.is_set(): + return False if isinstance(basis_result, _Fail): log.warning("Initializer %s failed: %s", initializer.name, basis_result.error) diff --git a/ldclient/interfaces.py b/ldclient/interfaces.py index f1b3314c..40bac1ce 100644 --- a/ldclient/interfaces.py +++ b/ldclient/interfaces.py @@ -2073,6 +2073,12 @@ def fetch(self, ss: SelectorStore) -> BasisResult: """ raise NotImplementedError + def stop(self) -> None: + """ + stop is called from another thread when the SDK shuts down while fetch is in progress. + It should make fetch return as soon as possible. The default implementation does nothing. + """ + @dataclass(frozen=True) class Update: diff --git a/ldclient/testing/impl/datasystem/test_fdv2_datasystem.py b/ldclient/testing/impl/datasystem/test_fdv2_datasystem.py index 6cc98a85..b604c449 100644 --- a/ldclient/testing/impl/datasystem/test_fdv2_datasystem.py +++ b/ldclient/testing/impl/datasystem/test_fdv2_datasystem.py @@ -3,7 +3,7 @@ import logging import os import tempfile -from threading import Event +from threading import Event, Timer from typing import List from mock import Mock @@ -861,3 +861,80 @@ def test_does_not_warn_when_payload_filter_key_is_not_configured(caplog): FDv2(Config(sdk_key="dummy"), DataSystemConfig(initializers=None, synchronizers=None)) assert not any("Payload filtering" in record.message for record in caplog.records) + + +class _BlockingInitializer(Initializer): + """An initializer whose fetch waits until stop is called, then returns data.""" + + def __init__(self): + self.fetching = Event() + self.stopped = Event() + + @property + def name(self) -> str: + return "blocking" + + def fetch(self, ss: SelectorStore) -> BasisResult: + self.fetching.set() + self.stopped.wait(5) + builder = ChangeSetBuilder() + builder.start(IntentCode.TRANSFER_FULL) + change_set = builder.finish(Selector(state="blocking-state", version=1)) + return _Success(value=Basis(change_set=change_set, persist=False, environment_id=None)) + + def stop(self): + self.stopped.set() + + +def test_stop_stops_the_initializer_in_progress_and_ignores_its_result(): + initializer = _BlockingInitializer() + td = TestDataV2.data_source() + fdv2 = FDv2( + Config(sdk_key="dummy"), + DataSystemConfig(initializers=[_InitializerBuilder(initializer)], synchronizers=[td.builder]), + ) + set_on_ready = Event() + fdv2.start(set_on_ready) + assert initializer.fetching.wait(5), "The initializer did not start" + + fdv2.stop() + + assert initializer.stopped.is_set() + # The initializer returned data after stop. The data system does not apply it. + assert not set_on_ready.is_set() + assert not fdv2.store.initialized + + +class _InitializerWithoutStop: + """An initializer that matches the protocol but does not inherit from it, so it has no stop.""" + + def __init__(self): + self.fetching = Event() + self.release = Event() + + @property + def name(self) -> str: + return "without-stop" + + def fetch(self, ss: SelectorStore) -> BasisResult: + self.fetching.set() + self.release.wait(5) + return _Fail(error="released") + + +def test_stop_accepts_an_initializer_in_progress_without_a_stop_method(caplog): + initializer = _InitializerWithoutStop() + fdv2 = FDv2( + Config(sdk_key="dummy"), + DataSystemConfig(initializers=[_InitializerBuilder(initializer)], synchronizers=None), + ) + fdv2.start(Event()) + assert initializer.fetching.wait(5), "The initializer did not start" + + # The initializer cannot be stopped, so release it shortly after stop begins. + release = Timer(0.2, initializer.release.set) + release.start() + fdv2.stop() + release.join() + + assert not any(record.levelno >= logging.ERROR for record in caplog.records)