Skip to content

[Bug] Native worker panics when activity completes after finalization #856

Description

@THardy98

What are you really trying to do?

Ensure worker shutdown is safe when activity completions are still in flight.

Describe the bug

If an activity completes while TemporalWorker is shutting down, the native worker may already have been finalized. Finalization removes the underlying Core worker, then the late completion reaches temporal_core_worker_complete_activity_task and unwraps the missing worker at worker.rs:852, aborting the .NET process:

called `Option::unwrap()` on a `None` value
fatal runtime error: failed to initiate panic, error 5, aborting

This timing-sensitive race was observed while running:

  • ExecuteWorkflowAsync_Cancel_ProperlyCanceled
  • ExecuteWorkflowAsync_StdlibSemaphore_NonAsyncDeadlocks

The runner named the cancellation test as active at the time of the crash, but both tests can leave activity completions in flight as worker shutdown begins.

Minimal Reproduction

Configure the test harness to use Temporal Cloud, then run either test:

dotnet test tests/Temporalio.Tests/Temporalio.Tests.csproj \
  --filter "FullyQualifiedName~ExecuteWorkflowAsync_Cancel_ProperlyCanceled"

dotnet test tests/Temporalio.Tests/Temporalio.Tests.csproj \
  --filter "FullyQualifiedName~ExecuteWorkflowAsync_StdlibSemaphore_NonAsyncDeadlocks"

The race may require repeated runs.

Environment/Versions

  • OS and processor: GitHub Actions ubuntu-latest, x64
  • Temporal environment: ephemeral Temporal Cloud namespace using mTLS
  • Temporal .NET SDK 1.18.0.0
  • sdk-core 78ac17e4bdaffdb54eb1c925a07d1fd40ce7b7f6
  • Failing CI job

Additional context

PR #823 marks both tests as NeedsCloudAdaptation. A durable fix should either wait for in-flight activity completions before native worker finalization or safely reject completions submitted after finalization.

Metadata

Metadata

Assignees

No one assigned

    Labels

    .NETPull requests that update .NET codebugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions