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..6d7c5fcb 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( @@ -106,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() @@ -116,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