Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions ldclient/impl/datasourcev2/polling.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
Expand All @@ -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)

Expand Down
28 changes: 27 additions & 1 deletion ldclient/impl/datasystem/fdv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
DataStoreStatus,
FeatureStore,
FlagChange,
Initializer,
OverrideSource,
ReadOnlyStore,
Synchronizer
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand All @@ -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).
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions ldclient/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
79 changes: 78 additions & 1 deletion ldclient/testing/impl/datasystem/test_fdv2_datasystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Loading