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
4 changes: 2 additions & 2 deletions .github/workflows/sdk-compliance.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ on:
jobs:
compliance:
name: PostHog SDK compliance tests
uses: PostHog/posthog-sdk-test-harness/.github/workflows/test-sdk-action.yml@39d05346c4638d24f94e591ab89c9c6fcdb52d6b
uses: PostHog/posthog-sdk-test-harness/.github/workflows/test-sdk-action.yml@85e2901ea3260a28e07a83086d59b4fb4dfc814f
with:
adapter-dockerfile: "sdk_compliance_adapter/Dockerfile"
adapter-context: "."
test-harness-version: "latest"
test-harness-version: "main-85e2901@sha256:4c8eac34e7ff66554a2c6947788c0a42b82456bc949c03bd8f6b9a10bef23ef5"
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