Skip to content
Open
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ class GreetingWorkflow:

# Wait for salutation update or complete signal (this can be
# cancelled)
await asyncio.wait(
await workflow.wait(
[
asyncio.create_task(self._greeting_info_update.wait()),
asyncio.create_task(self._complete.wait()),
Expand Down Expand Up @@ -885,6 +885,8 @@ deterministic:
* `asyncio.as_completed()` - use `workflow.as_completed()`
* `asyncio.wait()` - use `workflow.wait()`

The sandbox raises `RestrictedWorkflowAccessError` if the `asyncio` versions are called from a workflow.

`asyncio.TaskGroup` tracks tasks in a `set()`, so cancellation order can differ between execution and replay.
This can cause non-determinism errors, even if the workflow catches the task group's exception and completes
successfully. The sandbox warns when a task group is created.
Expand Down
1 change: 1 addition & 0 deletions changelog/breaking-changes/impatient-hourglass-heron.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The workflow sandbox now raises `RestrictedWorkflowAccessError` when workflow code calls `asyncio.as_completed()` or `asyncio.wait()`, instead of only warning. Use the deterministic `workflow.as_completed()` and `workflow.wait()` instead.
2 changes: 0 additions & 2 deletions temporalio/worker/workflow_sandbox/_restrictions.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,6 @@ def _public_callables(parent: Any, *, exclude: set[str] = set()) -> set[str]:
children={
"__call__": SandboxMatcher(
match_self=True,
leaf_warning=UserWarning,
leaf_message="asyncio.as_completed() is non-deterministic, use workflow.as_completed() instead",
)
},
Expand All @@ -598,7 +597,6 @@ def _public_callables(parent: Any, *, exclude: set[str] = set()) -> set[str]:
children={
"__call__": SandboxMatcher(
match_self=True,
leaf_warning=UserWarning,
leaf_message="asyncio.wait() is non-deterministic, use workflow.wait() instead",
)
},
Expand Down
52 changes: 28 additions & 24 deletions tests/worker/workflow_sandbox/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,41 +452,45 @@ async def test_workflow_sandbox_known_issues(client: Client):
@workflow.defn
class BadAsyncioWorkflow:
@workflow.run
async def run(self) -> list[str]:
async def run(self, call: str) -> None:
# Two known bad asyncio task calls, as_completed and wait
async def return_value(value: str) -> str:
return value

ret = []
for coro in asyncio.as_completed([return_value("val1"), return_value("val2")]):
ret.append(await coro)
done, _ = await asyncio.wait(
[
asyncio.create_task(return_value("val3")),
asyncio.create_task(return_value("val4")),
]
)
for task in done:
ret.append(await task)
return ret
task = asyncio.create_task(return_value("val1"))
try:
if call == "as_completed":
for coro in asyncio.as_completed([task]):
await coro
else:
await asyncio.wait([task])
except RestrictedWorkflowAccessError as err:
raise ApplicationError(
str(err), type="RestrictedWorkflowAccessError"
) from err


async def test_workflow_sandbox_bad_asyncio(client: Client):
@pytest.mark.parametrize(
"call,alternative",
[("as_completed", "workflow.as_completed()"), ("wait", "workflow.wait()")],
)
async def test_workflow_sandbox_bad_asyncio(
client: Client, call: str, alternative: str
):
async with new_worker(client, BadAsyncioWorkflow) as worker:
# We currently warn on this
with pytest.warns(UserWarning) as warnings:
result = await client.execute_workflow(
with pytest.raises(WorkflowFailureError) as err:
await client.execute_workflow(
BadAsyncioWorkflow.run,
call,
id=f"workflow-{uuid.uuid4()}",
task_queue=worker.task_queue,
)
assert "val1" in result[:2]
assert "val2" in result[:2]
assert "val3" in result[2:]
assert "val4" in result[2:]
assert len(warnings) == 2
assert "asyncio.as_completed()" in str(warnings[0].message)
assert "asyncio.wait()" in str(warnings[1].message)
assert isinstance(err.value.cause, ApplicationError)
assert err.value.cause.type == "RestrictedWorkflowAccessError"
assert (
f"asyncio.{call}() is non-deterministic, use {alternative} instead"
in err.value.cause.message
)


@workflow.defn
Expand Down