Skip to content

Fix concurrent local activity replay nondeterminism#1573

Closed
paulcacheux wants to merge 2 commits into
temporalio:mainfrom
paulcacheux:paulcacheux/fix-concurrent-local-activity-replay-nondeterminism
Closed

Fix concurrent local activity replay nondeterminism#1573
paulcacheux wants to merge 2 commits into
temporalio:mainfrom
paulcacheux:paulcacheux/fix-concurrent-local-activity-replay-nondeterminism

Conversation

@paulcacheux
Copy link
Copy Markdown

Problem

When multiple coroutines issue local activities concurrently (e.g. via asyncio.gather), replay can fail with NondeterminismError.

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.

Fix

Sort local activity resolve_activity jobs by sequence number before processing them in _WorkflowInstanceImpl.activate(). This ensures coroutines always resume in scheduling order regardless of execution mode (first-execution vs replay).

The fix is safe for existing workflows:

  • Without concurrent local activities: sorting a single item is a no-op.
  • With concurrent local activities that replayed correctly (completion order happened to match seq order): the new code produces the same interleaving.
  • With concurrent local activities that already failed on replay: they were hitting NondeterminismError anyway.

Test

Added test_workflow_concurrent_local_activity_replay: runs a workflow with two concurrent coroutines each issuing multiple sequential local activities, then replays the recorded history. Without the fix, replay fails with NondeterminismError.

Add a regression test that runs two concurrent coroutines (via
asyncio.gather) each issuing multiple sequential local activities,
then replays the recorded history. Without a fix, replay fails with
NondeterminismError because local activity markers are consumed in a
different order than the original execution.
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Jun 2, 2026

CLA assistant check
All committers have signed the CLA.

@CLAassistant
Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

During first execution, local activity resolve_activity jobs arrive in
completion order (wall-clock). During replay, they arrive in sequence
number order. When multiple coroutines issue local activities
concurrently, this ordering difference causes coroutines to resume in a
different order, assigning different sequence numbers to subsequent
commands and triggering NondeterminismError.

Fix by sorting local activity resolutions by sequence number before
processing. This ensures coroutines always resume in scheduling order
regardless of execution mode.
@paulcacheux paulcacheux force-pushed the paulcacheux/fix-concurrent-local-activity-replay-nondeterminism branch from 78a4a1b to 02e2c5e Compare June 3, 2026 12:40
@paulcacheux paulcacheux marked this pull request as ready for review June 3, 2026 12:48
@paulcacheux paulcacheux requested a review from a team as a code owner June 3, 2026 12:48
@Sushisource
Copy link
Copy Markdown
Member

Sushisource commented Jun 3, 2026

Thanks for the contribution!

This is very likely not the right thing to do - resolutions need to come in the same order they were written in, which means whatever order they are in history, not sequence number order.

Additionally, it's the responsibility of Core to do this all properly.

Does the test you've written consistently reproduce an NDE without your fix? If so, something's definitely wrong, but I don't think this is the right solution.

@tconley1428
Copy link
Copy Markdown
Contributor

Your test reproduced locally for me, but given that the solution is not the one we would want to proceed with, could you open an issue instead, which we can use to track the problem?

@paulcacheux
Copy link
Copy Markdown
Author

Yes I'm going to open an issue with the reproducer (that at least for me is reproducing every time)

@paulcacheux
Copy link
Copy Markdown
Author

here is the issue #1578, thanks for taking a look !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants