Skip to content

Remove activity cancel tokens once the activity finishes - #1677

Merged
chris-olszewski merged 1 commit into
temporalio:mainfrom
Anshuman-Prakash:fix-activity-cancel-token-leak
Oct 19, 2026
Merged

chris-olszewski merged 1 commit into
temporalio:mainfrom
Anshuman-Prakash:fix-activity-cancel-token-leak

Conversation

@Anshuman-Prakash

Copy link
Copy Markdown
Contributor

Fixes #1676

What changed

ActivityHalf::task_tokens_to_cancels got an entry for every activity task start and nothing ever removed it, so worker memory grew with every activity attempt.

  • The map is now shared with the spawned activity task (Arc<parking_lot::Mutex<..>>), and the task removes its own entry as soon as the activity future resolves. Success, failure and cancellation all go through that point. Panics are already caught inside the activity wrapper.
  • The entry is removed before the result is serialized and reported to Core, so a failed completion call can't leave it behind.
  • A Cancel that arrives after removal finds no entry and is ignored. That matches how unknown tokens were handled before.
  • Added a unit test that starts an activity through activity_task_handler, waits for it to finish, checks that the map is empty, then sends a late cancel for the same token.
  • Added a Fixed entry to CHANGELOG.md.

Checks run (Docker, rust:1.94 = 1.94.1, protoc 23.4)

  • cargo fmt --all --check: passes
  • cargo test -p temporalio-sdk --lib: 40 passed, including the new activity_cancel_tokens_removed_after_completion
  • cargo clippy -p temporalio-sdk --all-features --tests: no warnings in temporalio-sdk. The one warning in the output is an existing unnecessary_lazy_evaluations in crates/client/src/lib.rs:720, which this PR doesn't touch.

Not run locally: the workspace-wide cargo lint / cargo test-lint aliases and the integration tests (cargo integ-test).

🤖 Generated with Claude Code

ActivityHalf inserted a CancellationToken into task_tokens_to_cancels for
every activity task start and never removed it, so a long-running worker
leaked memory per activity attempt. The spawned activity task now removes
its entry once the activity future completes, covering success, failure
and cancellation. A cancel that arrives afterwards finds no entry and is
ignored, as before for unknown tokens.

Fixes temporalio#1676

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Anshuman-Prakash
Anshuman-Prakash requested a review from a team as a code owner October 19, 2026 10:39
@CLAassistant

CLAassistant commented Oct 19, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@chris-olszewski chris-olszewski self-assigned this Oct 19, 2026
@chris-olszewski
chris-olszewski merged commit 489337e into temporalio:main Oct 19, 2026
23 of 24 checks passed
chris-olszewski added a commit that referenced this pull request Nov 3, 2026
* Remove activity cancel tokens once the activity finishes (#1677)

ActivityHalf inserted a CancellationToken into task_tokens_to_cancels for
every activity task start and never removed it, so a long-running worker
leaked memory per activity attempt. The spawned activity task now removes
its entry once the activity future completes, covering success, failure
and cancellation. A cancel that arrives afterwards finds no entry and is
ignored, as before for unknown tokens.

Fixes #1676

Co-authored-by: Anshuman Prakash <199287261+Anshuman-Prakash@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 489337e)

* Prepare 1.1.2 release

---------

Co-authored-by: Anshuman Prakash <nakulsrivastava2004@gmail.com>
Co-authored-by: Anshuman Prakash <199287261+Anshuman-Prakash@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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] Memory Leak: task_tokens_to_cancels gets an entry per activity task that is never removed

3 participants