Skip to content

fix(core): prevent stale local activity dispatch after eviction - #1839

Draft
hikaru-212 wants to merge 1 commit into
temporalio:mainfrom
hikaru-212:fix/local-activity-dispatch-identity
Draft

hikaru-212 wants to merge 1 commit into
temporalio:mainfrom
hikaru-212:fix/local-activity-dispatch-identity

Conversation

@hikaru-212

Copy link
Copy Markdown
Contributor

What was changed

  • Queued local activity Start and Retry requests now carry the task token issued when they were queued. A Start carries its registration's token; a local retry carries the new token issued for that retry.
  • LocalActivityManager::next_pending dispatches a request only if that token matches the current registration for its (run_id, seq). Otherwise the request is dropped, the same way as when no registration exists: the registration's state is left alone, and the slot permit the request was paired with is released.
  • The manager's task-token counter is now a u64 advanced with checked_add, so a manager never issues the same token twice. If the counter is ever exhausted, allocation panics instead of wrapping. Local activity task tokens grow from 14 to 18 bytes; Core still identifies them only by their local_act_ prefix.
  • Added LocalActivityManager tests, a Core test for eviction and rebuild, and entries in both changelogs.

Why?

A queued request carried only (run_id, seq). Eviction removes the run's local activity registrations (#1304) but leaves requests that haven't been dispatched yet in the queue. When the rebuilt run scheduled the same seq, next_pending found the new registration by that key and dispatched the old request under the new token. The old request could then run for the rebuilt run's local activity and supply its result (#1838). Queued local retries went through the same path.

The token check only holds if a manager never reissues a token. The previous u32 counter wrapped in release builds and panicked on overflow in debug builds. After 2^32 allocations, a retry queued before the wrap could therefore match a newer registration's token.

The Core test triggers eviction with request_workflow_eviction; it does not depend on #1835 or legacy queries.

Checklist

  1. Closes [Bug] Stale queued local activity can use a rebuilt run's task token and deliver the wrong result #1838

  2. How was this tested?

    The tests were run locally on 6eaaeeb with the same changes. The relevant upstream files were unchanged between 6eaaeeb and b233e9f. The tested patch matches this commit except for the allocator's doc comment. The PR head itself was not built locally.

    • Without the fix, three regression tests fail because stale queued requests can be dispatched under a newer registration:

      • queued_start_of_evicted_run_is_not_dispatched_for_rebuilt_run
      • queued_retry_of_evicted_run_is_not_dispatched_for_rebuilt_run
      • The evicted_before_polled case of core_tests::local_activity_eviction::local_activity_queued_before_eviction_runs_only_for_rebuilt_run

      The kept_in_cache case passes with and without the fix. With the fix, the Core test also checks the recorded marker and a cold replay of the resulting history.

    • With the original u32 allocator, three tests fail:

      • local_activity_tokens_stay_unique_past_u32_max
      • local_activity_token_allocation_stops_at_exhaustion_instead_of_wrapping
      • queued_retry_of_evicted_run_is_not_dispatched_after_tokens_pass_u32_max

      The last test fast-forwards the counter instead of allocating 2^32 tokens.

    • Each of three mutations fails at least one new or existing test: skipping the check for retries, binding a retry to the failed attempt's token, or deleting the new registration on a mismatch.

    • cargo test -p temporalio-sdk-core --lib --release, with debug assertions and overflow checks enabled for temporalio-sdk-core: 693 passed, 1 ignored.

    • cargo +nightly fmt --all -- --check passes, also on the PR head.

    • cargo clippy -p temporalio-sdk-core --lib --tests --release -- -D warnings passes, with clippy::infallible_destructuring_match allowed for an existing warning in temporalio-client.

    • Not run: cargo lint, cargo test-lint, cargo integ-test, the unchunked build, a real server, or a language SDK worker. The Core test uses mocked server responses.

  3. Any docs updates needed?

    No additional documentation changes required. Both changelog entries have been added; no public API changes.

This branch has not been deployed

No deployments
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.

[Bug] Stale queued local activity can use a rebuilt run's task token and deliver the wrong result

1 participant