diff --git a/CHANGELOG.md b/CHANGELOG.md index 508c4afc8f..02585d635b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -62,6 +62,8 @@ relevant information. and both spellings are now accepted. * `Worker::run()` now waits for spawned activity tasks to finish before returning, including when running fails. +* Workers no longer leak memory for every activity task they run. Each task's cancellation + handle was previously kept until the worker exited. * Workers using a custom metrics backend now report task-slot and poller counts correctly in worker heartbeats, rather than reporting zero when the backend does not expose its labels. * Local activities still running when their workflow is evicted or the worker shuts down now diff --git a/crates/sdk/src/lib.rs b/crates/sdk/src/lib.rs index 34b9de620e..2e7bc67c05 100644 --- a/crates/sdk/src/lib.rs +++ b/crates/sdk/src/lib.rs @@ -132,6 +132,7 @@ use crate::{ }; use anyhow::{anyhow, bail}; use futures_util::{FutureExt, StreamExt, TryStreamExt, future::LocalBoxFuture}; +use parking_lot::Mutex; use std::{ any::{Any, TypeId}, cell::RefCell, @@ -817,7 +818,7 @@ struct WorkflowFutureHandle { struct ActivityHalf { /// Maps activity type to the function for executing activities of that type activities: ActivityDefinitions, - task_tokens_to_cancels: HashMap, + task_tokens_to_cancels: Arc>>, tasks: TaskTracker, } @@ -1564,7 +1565,9 @@ impl ActivityHalf { ); let ct = CancellationToken::new(); let task_token = activity.task_token; - self.task_tokens_to_cancels + let task_tokens_to_cancels = self.task_tokens_to_cancels.clone(); + task_tokens_to_cancels + .lock() .insert(task_token.clone().into(), ct.clone()); let (ctx, args) = ActivityContext::new( @@ -1590,6 +1593,7 @@ impl ActivityHalf { } .instrument(span); let result = act_fut.await; + task_tokens_to_cancels.lock().remove(task_token.as_slice()); let result = match result { Ok(output) => { // Codec application happens at the SDK/Core boundary, so activity @@ -1620,6 +1624,7 @@ impl ActivityHalf { Some(activity_task::Variant::Cancel(_)) => { if let Some(ct) = self .task_tokens_to_cancels + .lock() .get(activity.task_token.as_slice()) { ct.cancel(); @@ -1685,6 +1690,7 @@ mod tests { }, protos::coresdk::{ activity_result::activity_execution_result, + activity_task::{Cancel, Start}, workflow_commands::{CompleteWorkflowExecution, workflow_command}, workflow_completion::workflow_activation_completion, }, @@ -1893,6 +1899,53 @@ mod tests { assert_eq!(Arc::strong_count(&worker.common.worker), 1); } + #[tokio::test] + async fn activity_cancel_tokens_removed_after_completion() { + let worker = run_until_test_worker(stream::empty(), Default::default()); + let mut act_half = ActivityHalf { + activities: WorkerOptions::new("task_q") + .register_activities(MyActivities {}) + .build() + .activities(), + ..Default::default() + }; + let task_token = b"task-token".to_vec(); + let handle_task = |act_half: &mut ActivityHalf, variant| { + act_half + .activity_task_handler( + worker.common.worker.clone(), + worker.common.client_options.clone(), + worker.common.task_queue.clone(), + worker.common.data_converter.clone(), + vec![], + ActivityTask { + task_token: task_token.clone(), + variant: Some(variant), + }, + ) + .unwrap(); + }; + + handle_task( + &mut act_half, + activity_task::Variant::Start(Start { + activity_type: "MyActivities::my_activity".to_owned(), + ..Default::default() + }), + ); + assert_eq!(act_half.task_tokens_to_cancels.lock().len(), 1); + act_half.tasks.close(); + act_half.tasks.wait().await; + assert!(act_half.task_tokens_to_cancels.lock().is_empty()); + + // Cancels can still arrive for tasks that already finished and must be ignored + handle_task( + &mut act_half, + activity_task::Variant::Cancel(Cancel::default()), + ); + assert!(act_half.task_tokens_to_cancels.lock().is_empty()); + } + #[derive(Default)] struct FailingEncodeCodec { calls: AtomicUsize,