Skip to content

fix: clean up worker threads in the event of an error #2783

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 07-18-fix_make_query_as_macros_retry
Choose a base branch
from
Open
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
28 changes: 20 additions & 8 deletions packages/common/chirp-workflow/core/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@ impl Worker {
loop {
tokio::select! {
_ = tick_interval.tick() => {},
res = wake_sub.next() => {
if res.is_none() {
// Cancel background tasks
gc_handle.abort();
metrics_handle.abort();

return Err(WorkflowError::SubscriptionUnsubscribed.into());
}

tick_interval.reset();
},

res = &mut gc_handle => {
tracing::error!(?res, "metrics task unexpectedly stopped");
break;
Expand All @@ -91,18 +103,17 @@ impl Worker {
tracing::error!(?res, "metrics task unexpectedly stopped");
break;
},
res = wake_sub.next() => {
if res.is_none() {
return Err(WorkflowError::SubscriptionUnsubscribed.into());
}

tick_interval.reset();
},
_ = ctrl_c() => break,
_ = sigterm.recv() => break,
}

self.tick(&shared_client, &config, &pools, &cache).await?;
if let Err(err) = self.tick(&shared_client, &config, &pools, &cache).await {
// Cancel background tasks
gc_handle.abort();
metrics_handle.abort();

return Err(err);
}
}

self.shutdown(sigterm).await;
Expand Down Expand Up @@ -181,6 +192,7 @@ impl Worker {

tracing::info!("shutdown complete");

// This will stop all background tasks because the tokio runtime will stop
rivet_runtime::shutdown().await;
}

Expand Down
Loading