Skip to content
Open
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
33 changes: 26 additions & 7 deletions sdk_compliance_adapter/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,22 @@ def __init__(self):

def reset(self):
"""Reset all state"""
client_to_shutdown = None
with self.lock:
client_to_shutdown = self.client
self.client = None

if client_to_shutdown:
# Flush and shutdown the existing client outside state.lock.
# The patched transport records successful flush requests through
# SDKState.record_request(), which also needs state.lock. Holding the
# lock while shutdown() waits for the queue to drain can deadlock when
# a pending background event is being flushed during test reset.
try:
client_to_shutdown.shutdown()
except Exception as e:
logger.warning(f"Error shutting down client: {e}")

with self.lock:
self.pending_events = 0
self.total_events_captured = 0
Expand All @@ -77,13 +93,6 @@ def reset(self):
self.last_error = None
self.requests_made = []
self.retry_attempts = {}
if self.client:
# Flush and shutdown existing client
try:
self.client.shutdown()
except Exception as e:
logger.warning(f"Error shutting down client: {e}")
self.client = None

def increment_captured(self):
"""Increment total events captured"""
Expand Down Expand Up @@ -235,6 +244,10 @@ def init():
gzip=enable_compression,
max_retries=max_retries,
debug=True,
# Compliance tests assert the request-level default when callers omit
# disable_geoip. Configure the adapter to exercise geoip-enabled
# /flags requests by default while still allowing per-call overrides.
disable_geoip=False,
)

state.client = client
Expand Down Expand Up @@ -393,6 +406,12 @@ def get_feature_flag():
only_evaluate_locally=not force_remote,
)

# Ensure the SDK's side-effect $feature_flag_called event is sent before
# the adapter action returns. Otherwise the harness may reset mock-server
# state for the next test while the background consumer is still flushing,
# leaking the previous test's event into the next test.
state.client.flush()

logger.info(f"Feature flag {key} for {distinct_id}: {value}")

return jsonify({"success": True, "value": value})
Expand Down
Loading