Skip to content

fix(actor): hold per-actor turn until a cancelled call finishes (#353) - #356

Open
NaniToka wants to merge 1 commit into
dapr:mainfrom
NaniToka:fix/actor-turn-lock
Open

NaniToka wants to merge 1 commit into
dapr:mainfrom
NaniToka:fix/actor-turn-lock

Conversation

@NaniToka

Copy link
Copy Markdown

Description

Port the per-actor turn locking from dotnet-sdk#1937 and go-sdk#865 to resolve turn overlaps when requests are canceled in flight.

Problem

In the Dapr actor model, actor invocations are expected to be single-threaded (sequential per actor ID). Previously in rust-sdk, incoming Axum actor requests (/actors/{actor_type}/{actor_id}/method/..., timers, reminders, deactivation) executed without an application-side turn lock.

When a caller disconnected or cancelled a request, daprd freed the turn while the app-side method was still executing. The next scheduled call would then run concurrently on the same actor ID, violating single-threaded turn guarantees and risking state inconsistency.

Changes

  1. ActorTurnLock & ActorTurnLease (dapr/src/server/actor/runtime/turn_lock.rs):
    • Maintains an in-memory lock map keyed by (actor_type, actor_id) with FIFO queue waiters.
    • Automatically reclaims map entries when active calls and waiters reach 0.
    • Handles reentrancy matching on Dapr-Reentrancy-Id once actor activation has succeeded.
  2. Actor Turn Middleware (dapr/src/server/http.rs):
    • Wraps all /actors/{actor_type}/{actor_id}/... endpoints in actor_turn_middleware.
    • Acquires the turn before activation and method dispatch.
    • Runs handler invocation in a background task holding the ActorTurnLease. Even if the client connection drops or the outer request future is cancelled, local turn ownership is preserved until the invocation runs to completion.
  3. Reentrancy Configuration:
    • Added ReentrancyConfig to actor models and runtime registration so actors can enable reentrancy and declare reentrancy limits.
    • Reentrancy is marked allowed via lease.allow_reentrancy() only after get_or_create_actor activation has finished.
  4. Tests:
    • Unit tests covering turn FIFO serialization, parallel calls to different actor IDs, reentrant entry, and canceled waiter cleanup.
    • End-to-end HTTP integration tests verifying that concurrent calls serialize, cancelled requests retain the turn until completion, and reentrant calls sharing Dapr-Reentrancy-Id execute without deadlock.

Issue reference

This PR will close #353

Checklist

  • Code compiles correctly
  • Created/updated tests
  • Extended the documentation

@NaniToka
NaniToka requested review from a team as code owners November 16, 2026 15:02
Port the turn-based concurrency and cancellation-safe locking from dotnet-sdk#1937 and go-sdk#865 to the Rust SDK.

- Add ActorTurnLock to manage per-(actor_type, actor_id) FIFO turn acquisition and reentrancy.
- Wrap actor execution in actor_turn_middleware so that caller disconnects or cancellations do not release the turn lock prematurely before method execution finishes.
- Support reentrancy matching via Dapr-Reentrancy-Id once activation has completed.
- Add unit and integration regression tests verifying FIFO serial execution, cancellation turn holding, and HTTP reentrancy.

Closes dapr#353

Signed-off-by: NaniToka <tokananiy@gmail.com>
@NaniToka
NaniToka force-pushed the fix/actor-turn-lock branch from 935cfd1 to 5c688e4 Compare November 16, 2026 15:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant