Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
57 changes: 55 additions & 2 deletions crates/sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -817,7 +818,7 @@ struct WorkflowFutureHandle<F: Future> {
struct ActivityHalf {
/// Maps activity type to the function for executing activities of that type
activities: ActivityDefinitions,
task_tokens_to_cancels: HashMap<TaskToken, CancellationToken>,
task_tokens_to_cancels: Arc<Mutex<HashMap<TaskToken, CancellationToken>>>,
tasks: TaskTracker,
}

Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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,
},
Expand Down Expand Up @@ -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,
Expand Down
Loading