From 1ae54cbbb53d297bfc54370eeff370f8d49ba349 Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 30 Nov 2026 14:54:49 -0400 Subject: [PATCH 1/2] fix: preserve async feature flags readiness errors --- src/workos/feature_flags/runtime/_async.py | 12 +++++++----- tests/feature_flags_runtime/test_async_client.py | 9 ++++++++- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/workos/feature_flags/runtime/_async.py b/src/workos/feature_flags/runtime/_async.py index c82e1bac..b1e636aa 100644 --- a/src/workos/feature_flags/runtime/_async.py +++ b/src/workos/feature_flags/runtime/_async.py @@ -166,11 +166,13 @@ async def wait_until_ready(self, timeout: Optional[float] = None) -> None: return # Nothing can make the client ready in a forked child that cannot poll. self._raise_if_fork_blocked() - try: - await asyncio.wait_for(self._ready.wait(), timeout) - except asyncio.TimeoutError: - # The readiness timeout names the last refresh error as its cause. - raise self._readiness_timeout(timeout) + # A zero timeout cancels Event.wait() before it can observe a set event. + if not self._ready.is_set(): + try: + await asyncio.wait_for(self._ready.wait(), timeout) + except asyncio.TimeoutError: + # The readiness timeout names the last refresh error as its cause. + raise self._readiness_timeout(timeout) if not self._status.ready: raise self._readiness_failure() diff --git a/tests/feature_flags_runtime/test_async_client.py b/tests/feature_flags_runtime/test_async_client.py index 290e2935..303a87c7 100644 --- a/tests/feature_flags_runtime/test_async_client.py +++ b/tests/feature_flags_runtime/test_async_client.py @@ -89,7 +89,14 @@ async def test_bootstrap_is_ready_without_a_request( async def test_wait_times_out_without_a_configuration(make_async_runtime: Make) -> None: with pytest.raises(TimeoutError): - await make_async_runtime().wait_until_ready(timeout=0.01) + await make_async_runtime().wait_until_ready(timeout=0) + + +async def test_wait_with_zero_timeout_after_close(make_async_runtime: Make) -> None: + runtime = make_async_runtime() + await runtime.close() + with pytest.raises(RuntimeError, match="closed before it became ready"): + await runtime.wait_until_ready(timeout=0) async def test_a_wait_timeout_names_the_last_refresh_error( From d06a3388b631ce7cabbc389388312edcf6bf7f44 Mon Sep 17 00:00:00 2001 From: "Garen J. Torikian" Date: Mon, 30 Nov 2026 17:22:56 -0500 Subject: [PATCH 2/2] test: add the zero timeout case to the 401 readiness test The test for the AuthenticationError used only a timeout of 1 second. That case passed before the correction, so it could not find the defect again. Run the test with timeouts of 0 and 1. --- tests/feature_flags_runtime/test_async_client.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/feature_flags_runtime/test_async_client.py b/tests/feature_flags_runtime/test_async_client.py index 303a87c7..6d7c5fcb 100644 --- a/tests/feature_flags_runtime/test_async_client.py +++ b/tests/feature_flags_runtime/test_async_client.py @@ -113,8 +113,9 @@ async def test_a_wait_timeout_names_the_last_refresh_error( assert raised.value.__cause__ is runtime.status.last_error +@pytest.mark.parametrize("timeout", [0, 1]) async def test_each_wait_after_a_401_raises_a_fresh_error( - server: FlagServer, make_async_runtime: Make + server: FlagServer, make_async_runtime: Make, timeout: float ) -> None: server.respond(401, {"message": "Unauthorized"}) runtime = make_async_runtime() @@ -123,7 +124,7 @@ async def test_each_wait_after_a_401_raises_a_fresh_error( errors: List[BaseException] = [] for _ in range(4): try: - await runtime.wait_until_ready(timeout=1) + await runtime.wait_until_ready(timeout=timeout) except AuthenticationError as error: errors.append(error) assert len({id(error) for error in errors}) == 4