Skip to content
Draft
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
11 changes: 11 additions & 0 deletions openfeature/client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import logging
import threading
import typing
Expand Down Expand Up @@ -638,6 +639,16 @@ async def evaluate_flag_details_async(

return flag_evaluation

except asyncio.CancelledError:
# Cancellation must propagate, but finally hooks still need details.
flag_evaluation = FlagEvaluationDetails(
flag_key=flag_key,
value=default_value,
reason=Reason.ERROR,
error_code=ErrorCode.GENERAL,
error_message="Flag evaluation was cancelled",
)
raise
except OpenFeatureError as err:
error_hooks(flag_type, err, reversed_merged_hooks_and_context, hook_hints)
flag_evaluation = FlagEvaluationDetails(
Expand Down
104 changes: 104 additions & 0 deletions tests/test_async_cancellation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import asyncio
from unittest.mock import AsyncMock, Mock

import pytest

from openfeature.exception import ErrorCode
from openfeature.flag_evaluation import Reason
from openfeature.hook import Hook


@pytest.mark.asyncio
@pytest.mark.parametrize(
"flag_type, default_value",
[
("boolean", False),
("string", "fallback"),
("integer", 42),
("float", 1.5),
("object", {"fallback": True}),
("object", ["fallback"]),
],
)
async def test_cancellation_preserves_task_state_and_runs_finally_hooks(
flag_type, default_value, no_op_provider_client, monkeypatch
):
started = asyncio.Event()

async def resolve(**kwargs):
started.set()
await asyncio.Future()

monkeypatch.setattr(
no_op_provider_client.provider, f"resolve_{flag_type}_details_async", resolve
)
hook = Mock(spec=Hook)
no_op_provider_client.add_hooks([hook])
evaluate = getattr(no_op_provider_client, f"get_{flag_type}_details_async")
task = asyncio.create_task(evaluate("cancelled-flag", default_value))
try:
await asyncio.wait_for(started.wait(), timeout=5)
finally:
task.cancel("caller cancelled evaluation")

with pytest.raises(asyncio.CancelledError):
await task

assert task.cancelled()
hook.before.assert_called_once()
hook.after.assert_not_called()
hook.error.assert_not_called()
hook.finally_after.assert_called_once()
details = hook.finally_after.call_args.kwargs["details"]
assert details.flag_key == "cancelled-flag"
assert details.value == default_value
assert details.reason == Reason.ERROR
assert details.error_code == ErrorCode.GENERAL


@pytest.mark.asyncio
async def test_provider_cancellation_is_preserved_without_hooks(
no_op_provider_client, monkeypatch
):
cancellation = asyncio.CancelledError("provider cancelled evaluation")
monkeypatch.setattr(
no_op_provider_client.provider,
"resolve_boolean_details_async",
AsyncMock(side_effect=cancellation),
)

with pytest.raises(asyncio.CancelledError) as caught:
await no_op_provider_client.get_boolean_value_async("cancelled-flag", False)

assert caught.value is cancellation


@pytest.mark.asyncio
async def test_wait_for_preserves_timeout_and_runs_finally_hooks(
no_op_provider_client, monkeypatch
):
started = asyncio.Event()

async def resolve(**kwargs):
started.set()
await asyncio.Future()

monkeypatch.setattr(
no_op_provider_client.provider, "resolve_boolean_details_async", resolve
)
hook = Mock(spec=Hook)
no_op_provider_client.add_hooks([hook])
task = asyncio.create_task(
no_op_provider_client.get_boolean_value_async("timed-out-flag", False)
)
try:
await asyncio.wait_for(started.wait(), timeout=5)
with pytest.raises(asyncio.TimeoutError):
await asyncio.wait_for(task, timeout=0)
finally:
task.cancel()

assert task.cancelled()
hook.after.assert_not_called()
hook.finally_after.assert_called_once()
assert hook.finally_after.call_args.kwargs["details"].value is False