Skip to content

Commit d6bc3f6

Browse files
committed
fix: preserve cancellation during async flag evaluation
Prepared with AI assistance. Personal review and DCO sign-off pending.
1 parent efd29b1 commit d6bc3f6

2 files changed

Lines changed: 115 additions & 0 deletions

File tree

‎openfeature/client.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import asyncio
12
import logging
23
import threading
34
import typing
@@ -638,6 +639,16 @@ async def evaluate_flag_details_async(
638639

639640
return flag_evaluation
640641

642+
except asyncio.CancelledError:
643+
# Cancellation must propagate, but finally hooks still need details.
644+
flag_evaluation = FlagEvaluationDetails(
645+
flag_key=flag_key,
646+
value=default_value,
647+
reason=Reason.ERROR,
648+
error_code=ErrorCode.GENERAL,
649+
error_message="Flag evaluation was cancelled",
650+
)
651+
raise
641652
except OpenFeatureError as err:
642653
error_hooks(flag_type, err, reversed_merged_hooks_and_context, hook_hints)
643654
flag_evaluation = FlagEvaluationDetails(

‎tests/test_async_cancellation.py‎

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import asyncio
2+
from unittest.mock import AsyncMock, Mock
3+
4+
import pytest
5+
6+
from openfeature.exception import ErrorCode
7+
from openfeature.flag_evaluation import Reason
8+
from openfeature.hook import Hook
9+
10+
11+
@pytest.mark.asyncio
12+
@pytest.mark.parametrize(
13+
"flag_type, default_value",
14+
[
15+
("boolean", False),
16+
("string", "fallback"),
17+
("integer", 42),
18+
("float", 1.5),
19+
("object", {"fallback": True}),
20+
("object", ["fallback"]),
21+
],
22+
)
23+
async def test_cancellation_preserves_task_state_and_runs_finally_hooks(
24+
flag_type, default_value, no_op_provider_client, monkeypatch
25+
):
26+
started = asyncio.Event()
27+
28+
async def resolve(**kwargs):
29+
started.set()
30+
await asyncio.Future()
31+
32+
monkeypatch.setattr(
33+
no_op_provider_client.provider, f"resolve_{flag_type}_details_async", resolve
34+
)
35+
hook = Mock(spec=Hook)
36+
no_op_provider_client.add_hooks([hook])
37+
evaluate = getattr(no_op_provider_client, f"get_{flag_type}_details_async")
38+
task = asyncio.create_task(evaluate("cancelled-flag", default_value))
39+
try:
40+
await asyncio.wait_for(started.wait(), timeout=5)
41+
finally:
42+
task.cancel("caller cancelled evaluation")
43+
44+
with pytest.raises(asyncio.CancelledError):
45+
await task
46+
47+
assert task.cancelled()
48+
hook.before.assert_called_once()
49+
hook.after.assert_not_called()
50+
hook.error.assert_not_called()
51+
hook.finally_after.assert_called_once()
52+
details = hook.finally_after.call_args.kwargs["details"]
53+
assert details.flag_key == "cancelled-flag"
54+
assert details.value == default_value
55+
assert details.reason == Reason.ERROR
56+
assert details.error_code == ErrorCode.GENERAL
57+
58+
59+
@pytest.mark.asyncio
60+
async def test_provider_cancellation_is_preserved_without_hooks(
61+
no_op_provider_client, monkeypatch
62+
):
63+
cancellation = asyncio.CancelledError("provider cancelled evaluation")
64+
monkeypatch.setattr(
65+
no_op_provider_client.provider,
66+
"resolve_boolean_details_async",
67+
AsyncMock(side_effect=cancellation),
68+
)
69+
70+
with pytest.raises(asyncio.CancelledError) as caught:
71+
await no_op_provider_client.get_boolean_value_async("cancelled-flag", False)
72+
73+
assert caught.value is cancellation
74+
75+
76+
@pytest.mark.asyncio
77+
async def test_wait_for_preserves_timeout_and_runs_finally_hooks(
78+
no_op_provider_client, monkeypatch
79+
):
80+
started = asyncio.Event()
81+
82+
async def resolve(**kwargs):
83+
started.set()
84+
await asyncio.Future()
85+
86+
monkeypatch.setattr(
87+
no_op_provider_client.provider, "resolve_boolean_details_async", resolve
88+
)
89+
hook = Mock(spec=Hook)
90+
no_op_provider_client.add_hooks([hook])
91+
task = asyncio.create_task(
92+
no_op_provider_client.get_boolean_value_async("timed-out-flag", False)
93+
)
94+
try:
95+
await asyncio.wait_for(started.wait(), timeout=5)
96+
with pytest.raises(asyncio.TimeoutError):
97+
await asyncio.wait_for(task, timeout=0)
98+
finally:
99+
task.cancel()
100+
101+
assert task.cancelled()
102+
hook.after.assert_not_called()
103+
hook.finally_after.assert_called_once()
104+
assert hook.finally_after.call_args.kwargs["details"].value is False

0 commit comments

Comments
 (0)