[Bug] non determinism when using `asyncio.gather` with local activities
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 48/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Quiet
- Tech stack
- python
- Domain
- distributed-systems
Research direction
Start with the minimal reproduction in test_workflow_concurrent_local_activity_replay and run it through Replayer, focusing on execute_local_activity calls inside asyncio.gather. Compare local-activity completion ordering during initial execution and replay; done means the workflow completes and its history replays without NondeterminismError.
Written by the indexing model from the issue text.
Description
What are you really trying to do?
At a high level, our code is using some local activities at the beginning and end of some workflows parts. We got some reports of non determinism errors when those workflow parts are called using asyncio.gather.
Describe the bug
According to my understanding (might be wrong of course):
During first execution, local activity resolve_activity jobs arrive in completion order (wall-clock timing). During replay, they arrive in sequence number order. This ordering difference causes coroutines to resume in a different order, assigning different sequence numbers to subsequent commands, which no longer match the recorded markers.
Minimal Reproduction
Extracted from: https://github.com/temporalio/sdk-python/pull/1573
@activity.defn
async def local_activity_slow(index: int) -> None:
if index % 2 == 0:
await asyncio.sleep(0.05)
@activity.defn
async def local_activity_fast(index: int) -> None:
return None
@activity.defn
async def local_activity_gate() -> None:
await asyncio.sleep(0.05)
@workflow.defn
class ConcurrentLocalActivityReplayWorkflow:
"""Workflow that runs two concurrent coroutines with local activities.
This reproduces a replay nondeterminism bug: during first execution,
local activities take real time, creating a deterministic interleaving.
During replay, all local activities return instantly from markers, which
can reorder coroutine scheduling and produce a different command sequence.
"""
@workflow.run
async def run(self) -> list[int]:
async def lifecycle_a(index: int) -> int:
await workflow.execute_local_activity(
local_activity_slow,
args=[index * 2],
start_to_close_timeout=timedelta(seconds=5),
)
await workflow.execute_local_activity(
local_activity_fast,
args=[index * 2],
start_to_close_timeout=timedelta(seconds=5),
)
return index * 2
async def lifecycle_b(index: int) -> int:
await workflow.execute_local_activity(
local_activity_gate,
start_to_close_timeout=timedelta(seconds=5),
)
await workflow.execute_local_activity(
local_activity_slow,
args=[index * 2 + 1],
start_to_close_timeout=timedelta(seconds=5),
)
await workflow.execute_local_activity(
local_activity_fast,
args=[index * 2 + 1],
start_to_close_timeout=timedelta(seconds=5),
)
return index * 2 + 1
results: list[int] = []
for index in range(20):
results.extend(await asyncio.gather(lifecycle_a(index), lifecycle_b(index)))
return results
async def test_workflow_concurrent_local_activity_replay(client: Client):
"""Test that concurrent local activities replay deterministically.
Runs a workflow with two concurrent coroutines that each issue multiple
local activities, then replays the history. Without the fix, replay
fails with NondeterminismError because the local activity command order
diverges from the recorded marker order.
"""
async with new_worker(
client,
ConcurrentLocalActivityReplayWorkflow,
activities=[local_activity_slow, local_activity_fast, local_activity_gate],
) as worker:
handle = await client.start_workflow(
ConcurrentLocalActivityReplayWorkflow.run,
id=f"workflow-{uuid.uuid4()}",
task_queue=worker.task_queue,
)
expected = [v for i in range(20) for v in (i * 2, i * 2 + 1)]
assert await handle.result() == expected
history = await handle.fetch_history()
await Replayer(
workflows=[ConcurrentLocalActivityReplayWorkflow],
).replay_workflow(history)
Environment/Versions
- OS and processor: M1 Mac and Linux at least (I believe this is more general than that)
- Temporal Version: server=1.30.4, sdk=1.27.2, but also reproducing on main of
sdk-python - Building Temporal server from source
- Dominant language
- Python
- Stars
- 1.2k
- Forks
- 241
- Avg merge
- 2d 17h
- Merged PRs (30d)
- 38
Getting set up
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from temporalio/sdk-python
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
temporalio/sdk-python#1896 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
temporalio/sdk-python#1517 · 10 comments ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
temporalio/sdk-python#496 ·
Maintainers usually reply within 1 day
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
temporalio/sdk-python#1894 ·
Maintainers usually reply within 1 day
-
Difficulty 5/5 Over a week Newbie friendliness 25/100
temporalio/sdk-python#1890 ·
Maintainers usually reply within 1 day
All issues in temporalio/sdk-python
Similar issues
-
good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
vllm-project/vllm-metal#822 ·
Maintainers usually reply within 1 day
-
vector-store
Difficulty 1/5 1-3 hours Newbie friendliness 90/100
mem0ai/mem0#7461 · 1 comment ·
Maintainers usually reply within 1 day
-
[Bug]: chunk_span_bounds and _validated_chunk_spans reject Pydantic models ChunkSpan and AudioFileOpen
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
BasedHardware/omi#19047 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
Maintainers usually reply within 1 day