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
12 changes: 10 additions & 2 deletions ldclient/impl/datasystem/fdv2.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import time
from queue import Queue
from threading import Event, Thread
from threading import Event, Thread, current_thread
from typing import Any, Callable, Dict, List, Mapping, Optional

from ldclient.config import Config, DataSourceBuilder, DataSystemConfig
Expand Down Expand Up @@ -376,8 +376,11 @@ def stop(self):
except Exception as e:
log.error("Error stopping active data source: %s", e)

# Wait for all threads to complete
# Wait for all threads to complete. A listener can call stop on one of these threads,
# and a thread cannot join itself. That thread sees the stop event and exits on its own.
for thread in self._threads:
if thread is current_thread():
continue
if thread.is_alive():
thread.join(timeout=5.0) # 5 second timeout
if thread.is_alive():
Expand Down Expand Up @@ -688,6 +691,11 @@ def reader(self: 'FDv2'):
if update.state == DataSourceState.VALID and not set_on_ready.is_set():
set_on_ready.set()

# A listener can stop the data system while apply notifies it. Stop sets the
# status to OFF, so this update must not replace it.
if self._stop_event.is_set():
return ConditionDirective.FALLBACK

# Update status
self._data_source_status_provider.update_status(update.state, update.error)

Expand Down
8 changes: 7 additions & 1 deletion ldclient/impl/datasystem/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,10 @@ def __init__(
# store. Set on each successful apply from its persist flag.
self._persist = False

# True after close. A listener can close the store while apply notifies it. The
# change set that apply is processing is then not written to the closed persistent store.
self._closed = False

def with_persistence(
self,
persistent_store: FeatureStore,
Expand Down Expand Up @@ -522,7 +526,8 @@ def apply(self, change_set: ChangeSet, persist: bool) -> None:
def _should_persist(self) -> bool:
"""Returns whether data should be persisted to the persistent store."""
return (
self._persist
not self._closed
and self._persist
and self._persistent_store is not None
and self._persistent_store_writable
)
Expand Down Expand Up @@ -570,6 +575,7 @@ def __mapping(data: Dict[str, ModelEntity]) -> Dict[str, Dict[str, Any]]:
def close(self) -> None:
"""Close the store and any persistent store if configured."""
with self._lock:
self._closed = True
if self._persistent_store is not None:
try:
# Most FeatureStore implementations don't have close methods
Expand Down
6 changes: 5 additions & 1 deletion ldclient/impl/integrations/files/file_data_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import time
import traceback
from threading import current_thread
from typing import Optional

from ldclient.impl.repeating_task import RepeatingTask
Expand Down Expand Up @@ -174,7 +175,10 @@ def on_any_event(self, event):

def stop(self):
self._observer.stop()
self._observer.join()
# A listener can stop the client on the observer thread, and a thread cannot join
# itself. That thread sees the stop request and exits on its own.
if self._observer is not current_thread():
self._observer.join()

# Watch for changes to data files by polling their modification times. This is used if auto-update is
# on but the watchdog package is not installed.
Expand Down
36 changes: 23 additions & 13 deletions ldclient/impl/overrides/sink.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
class OverrideSinkImpl(OverrideSink):
"""
Applies override layer replacements supplied by an override source, then notifies flag
change listeners of every flag whose merged-view evaluation may have changed. Calls are
serialized, so overlapping updates from a source cannot interleave.
change listeners of every flag whose merged-view evaluation may have changed. Layer updates
are serialized, so overlapping updates from a source cannot interleave. Listeners are
notified after the lock is released, so a listener can close the client.
"""

def __init__(
Expand All @@ -48,28 +49,32 @@ def __init__(
self._notify = notify
self._has_listeners = has_listeners
self._lock = threading.Lock()
self._closed = False # guarded by _lock
# An event, not a flag that _lock guards, so close never waits for _lock.
self._closed = threading.Event()

def set_overrides(self, flags: Dict[str, Any], segments: Dict[str, Any]) -> None: # type: ignore[override]
with self._lock:
if self._closed:
if self._closed.is_set():
log.debug("Ignoring an override snapshot supplied after the override source was closed")
return
try:
self._apply(flags, segments)
affected = self._apply(flags, segments)
except DeletedItemError as e:
# The layer rejected the whole snapshot before changing anything, so nothing is
# notified. The error also reaches the source, as for an invalid definition.
log.error("Rejected an override snapshot; the overrides in effect stay: %s", e)
raise
self._notify_all(affected)

def close(self) -> None:
"""
Stops accepting snapshots. Used when the client is closed: a snapshot that the source
supplies while it shuts down is ignored. The layer keeps its contents.
supplies after close is ignored. A snapshot that is already in progress completes, and
it can notify listeners after close returns. The layer keeps its contents.
"""
with self._lock:
self._closed = True
# Do not take _lock here. A listener can close the client on a thread that holds the
# store lock. Another thread can hold _lock and wait for that store lock.
self._closed.set()

def close_and_clear(self) -> None:
"""
Expand All @@ -78,21 +83,26 @@ def close_and_clear(self) -> None:
overrides, and a late snapshot from the closed source is ignored.
"""
with self._lock:
self._closed = True
self._apply({}, {})
self._closed.set()
affected = self._apply({}, {})
self._notify_all(affected)

def _apply(self, flags: Dict[str, Any], segments: Dict[str, Any]) -> None:
# Called with _lock held.
def _apply(self, flags: Dict[str, Any], segments: Dict[str, Any]) -> Set[str]:
# Called with _lock held. Returns the keys of the affected flags.
if not self._has_listeners():
self._layer.set_all(flags, segments)
return
return set()

previous, current = self._layer.set_all(flags, segments)
old_merged = snapshot_merged_view(self._base, previous)
new_merged = snapshot_merged_view(self._base, current)
affected = compute_affected_flags(previous, current, old_merged, new_merged)
if len(affected) > 0:
log.debug("Override update affected %d flag(s)", len(affected))
return affected

def _notify_all(self, affected: Set[str]) -> None:
# Called without _lock held. A listener can then close the client, which closes this sink.
for key in sorted(affected):
self._notify(key)

Expand Down
36 changes: 36 additions & 0 deletions ldclient/testing/impl/datasystem/test_fdv2_datasystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,42 @@ def listener(flag_change: FlagChange):
assert len(changes) == 0


def test_fdv2_can_be_stopped_from_a_flag_change_listener():
td = TestDataV2.data_source()
td.update(td.flag("feature-flag").on(True))
data_system_config = DataSystemConfig(
initializers=None,
synchronizers=[td.builder],
)

set_on_ready = Event()
fdv2 = FDv2(Config(sdk_key="dummy"), data_system_config)

stopped = Event()
errors: List[Exception] = []

def listener(flag_change: FlagChange):
try:
fdv2.stop()
except Exception as e:
errors.append(e)
finally:
stopped.set()

fdv2.flag_change_listeners.add(listener)

fdv2.start(set_on_ready)
assert set_on_ready.wait(1), "Data system did not become ready in time"

td.update(td.flag("feature-flag").on(False))
assert stopped.wait(1), "Flag change listener was not called in time"
assert errors == []

for thread in fdv2._threads:
thread.join(timeout=1)
assert not thread.is_alive(), "Thread %s did not exit" % thread.name


@pytest.mark.parametrize("synchronizer_count", [1, 2])
def test_stop_reports_off_without_a_synchronizer_failure_warning(caplog, synchronizer_count):
td = TestDataV2.data_source()
Expand Down
62 changes: 62 additions & 0 deletions ldclient/testing/impl/test_overrides.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
snapshots and notifies flag change listeners.
"""
import logging
import threading
from typing import Any, Dict, List, Optional

import pytest
Expand Down Expand Up @@ -502,6 +503,67 @@ def test_sink_close_and_clear_clears_the_layer_and_ignores_later_snapshots():
assert f.take_notified() == []


def test_sink_listener_can_close_the_sink():
# A listener that closes the client closes the sink. The sink must not hold its lock while
# it notifies, or the close waits forever.
f = SinkFixture(FakeBaseStore())
f.sink.set_overrides({'flag1': flag('flag1')}, {})
f.take_notified()

def notify(key: str):
f.notified.append(key)
f.sink.close()

f.sink._notify = notify
worker = threading.Thread(target=lambda: f.sink.set_overrides({'flag1': flag('flag1', 2)}, {}), daemon=True)
worker.start()
worker.join(5)

assert not worker.is_alive(), "Closing the sink from a listener did not return"
assert f.take_notified() == ['flag1']
f.sink.set_overrides({'flag2': flag('flag2')}, {})
assert f.take_notified() == []


def test_sink_close_does_not_wait_for_a_snapshot_in_progress():
# A listener can close the client on a thread that holds the store lock. A snapshot in
# progress on another thread can wait for that lock while it reads the base store. Close
# must not wait for the snapshot, or the two threads wait for each other.
class BlockingBaseStore(FakeBaseStore):
def __init__(self):
super().__init__(flags={'flag1': flag('flag1')})
self.reading = threading.Event()
self.release = threading.Event()

def all(self, kind, callback=lambda x: x):
self.reading.set()
self.release.wait(5)
return super().all(kind, callback)

base = BlockingBaseStore()
f = SinkFixture(base)
snapshot = threading.Thread(target=lambda: f.sink.set_overrides({'flag1': flag('flag1', 2)}, {}), daemon=True)
snapshot.start()
assert base.reading.wait(5), "The snapshot did not read the base store"

closer = threading.Thread(target=f.sink.close, daemon=True)
closer.start()
closer.join(1)
closed_while_reading = not closer.is_alive()

base.release.set()
snapshot.join(5)
closer.join(5)
assert closed_while_reading, "Close waited for the snapshot in progress"

# The snapshot in progress completes. A later snapshot is ignored.
assert f.layer.get(FEATURES, 'flag1').version == 2
f.take_notified()
f.sink.set_overrides({'flag2': flag('flag2')}, {})
assert f.layer.get(FEATURES, 'flag2') is None
assert f.take_notified() == []


def test_sink_notifies_directly_changed_keys_when_base_read_fails():
base = FakeBaseStore(flags={'flag1': flag('flag1')})
base.fail_all = True
Expand Down
33 changes: 33 additions & 0 deletions ldclient/testing/test_file_data_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,39 @@ def test_reloads_modified_file_in_polling_mode():
do_auto_update_test({'auto_update': True, 'force_polling': True, 'poll_interval': 0.1})


@pytest.mark.parametrize('options', [
pytest.param({'auto_update': True}, id='watchdog'),
pytest.param({'auto_update': True, 'force_polling': True, 'poll_interval': 0.1}, id='polling'),
])
def test_client_can_be_closed_from_a_flag_change_listener_on_reload(options):
path = make_temp_file(flag_only_json)
factory = Files.new_data_source(paths=path, **options)
client = LDClient(config=Config('SDK_KEY', update_processor_class=factory, send_events=False))
try:
closed = threading.Event()
errors: List[Exception] = []

def listener(_):
if closed.is_set():
return
try:
client.close()
except Exception as e:
errors.append(e)
finally:
closed.set()

client.flag_tracker.add_listener(listener)
time.sleep(0.5)
replace_file(path, segment_only_json)

assert closed.wait(20), "Flag change listener was not called in time"
assert errors == []
finally:
client.close()
os.remove(path)


def test_evaluates_full_flag_with_client_as_expected():
path = make_temp_file(all_properties_json)
try:
Expand Down
Loading