:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
chore(proxy_service): make the loop-blocking checks deterministic (#95862)
posthog[bot]862f385d4155
diff --git a/posthog/temporal/proxy_service/test/dns_test.py b/posthog/temporal/proxy_service/test/dns_test.pyindex 7f9b334cf586..cd5db21d7b62 100644--- a/posthog/temporal/proxy_service/test/dns_test.py+++ b/posthog/temporal/proxy_service/test/dns_test.py@@ -1,6 +1,6 @@-import timeimport uuidimport asyncio+import threadingfrom unittest.mock import AsyncMock, MagicMock, patch@@ -42,18 +42,27 @@ def _record(target_cname="x.cf-prod-us-proxy.proxyhog.com."):return r-async def _time_a_co_tenant(during) -> tuple[float, float]:- async def ticker() -> float:- started = time.monotonic()- for _ in range(20):- await asyncio.sleep(0.05)- return time.monotonic() - started+# Only reached when the loop is blocked, so it bounds a deadlock instead of timing the loop.+BLOCKED_LOOP_TIMEOUT_S = 5.0- baseline = await ticker()- task = asyncio.create_task(ticker())- await asyncio.sleep(0.1)- await during()- return baseline, await task++class _BlockingCall:+ """A blocking stub that reports whether the event loop ran while the stub held its thread."""++ def __init__(self, exception: Exception):+ self._exception = exception+ self._entered = threading.Event()+ self._released = threading.Event()+ self.loop_ran_while_blocked = False++ def __call__(self, *args, **kwargs):+ self._entered.set()+ self.loop_ran_while_blocked = self._released.wait(BLOCKED_LOOP_TIMEOUT_S)+ raise self._exception++ async def release_when_entered(self) -> None:+ await asyncio.to_thread(self._entered.wait, BLOCKED_LOOP_TIMEOUT_S)+ self._released.set()class TestProxyChecksDoNotFailOnHandledConditions(SimpleTestCase):@@ -110,33 +119,27 @@ class TestProxyChecksKeepTheEventLoopFree(SimpleTestCase):@patch("posthog.temporal.proxy_service.monitor.get_record")async def test_check_dns_does_not_block(self, mock_get_record):mock_get_record.return_value = _record()-- def slow(domain, rdtype, **kwargs):- time.sleep(1.0)- raise dns.resolver.NXDOMAIN()+ lookup = _BlockingCall(dns.resolver.NXDOMAIN())with patch("posthog.temporal.proxy_service.monitor.dnssec_resolver") as resolver:- resolver.return_value.resolve.side_effect = slow- baseline, contended = await _time_a_co_tenant(- lambda: check_dns(CheckActivityInput(proxy_record_id=RECORD_ID))- )+ resolver.return_value.resolve.side_effect = lookup+ check = asyncio.create_task(check_dns(CheckActivityInput(proxy_record_id=RECORD_ID)))+ await lookup.release_when_entered()+ await check- assert contended < baseline + 0.5, f"loop blocked: baseline={baseline:.2f}s contended={contended:.2f}s"+ assert lookup.loop_ran_while_blocked, "loop blocked: no coroutine ran while the DNS lookup blocked"@patch("posthog.temporal.proxy_service.monitor.get_record")async def test_check_proxy_is_live_does_not_block(self, mock_get_record):mock_get_record.return_value = _record()+ probe = _BlockingCall(requests.exceptions.Timeout())- def slow(*args, **kwargs):- time.sleep(1.0)- raise requests.exceptions.Timeout()-- with patch("posthog.temporal.proxy_service.monitor.requests.post", side_effect=slow):- baseline, contended = await _time_a_co_tenant(- lambda: check_proxy_is_live(CheckActivityInput(proxy_record_id=RECORD_ID))- )+ with patch("posthog.temporal.proxy_service.monitor.requests.post", side_effect=probe):+ check = asyncio.create_task(check_proxy_is_live(CheckActivityInput(proxy_record_id=RECORD_ID)))+ await probe.release_when_entered()+ await check- assert contended < baseline + 0.5, f"loop blocked: baseline={baseline:.2f}s contended={contended:.2f}s"+ assert probe.loop_ran_while_blocked, "loop blocked: no coroutine ran while the proxy probe blocked"class TestLegacyCertificateStatus(SimpleTestCase):