Skip to content

Add Worker::run_until to the Rust SDK - #1637

Open
eamsden wants to merge 6 commits into
mainfrom
eamsden/worker-run-until
Open

eamsden wants to merge 6 commits into
mainfrom
eamsden/worker-run-until

Conversation

@eamsden

@eamsden eamsden commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

What was changed

Add Worker::run_until(future) to trigger shutdown and await Core's finalize_shutdown(). run() now waits for activity tasks before returning.

Why?

Let applications tie worker lifetime to a future and release its registration when shutdown finishes.

Checklist

  1. How was this tested: 39 SDK unit tests and the local integration regression passed, along with formatting and both lint checks. Covers direct run() cleanup on success and error; reuses the existing activity workflow.
  2. Any docs updates needed? API documentation and changelog entry included.

@eamsden
eamsden force-pushed the eamsden/worker-run-until branch from 5ba4786 to 4ff1a51 Compare October 12, 2026 20:26
@eamsden
eamsden marked this pull request as ready for review October 12, 2026 20:26
@eamsden
eamsden requested a review from a team as a code owner October 12, 2026 20:26

#[workflow]
#[derive(Default)]
struct RunUntilWorkflow;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sure there's some other existing workflow we can re-use rather than adding this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, fixed

Comment thread crates/sdk/src/lib.rs Outdated
Comment on lines +1085 to +1087
// Validation failures skip the SDK polling loops, but Core's activity manager still needs
// a poll to observe shutdown before its finalizer can complete.
if matches!(result, Err(WorkerRunError::Validation(_))) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. This sorta feels like a separate bug that should be addressed... but doesn't need to be in this PR.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#1646 draft PR for fixing the bug at the source

Comment thread crates/sdk/src/lib.rs
/// Maps activity type to the function for executing activities of that type
activities: ActivityDefinitions,
task_tokens_to_cancels: HashMap<TaskToken, CancellationToken>,
tasks: TaskTracker,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh I did not know about this thing

Comment thread crates/sdk/src/lib.rs Outdated
Comment on lines +1090 to +1092
for cancellation in self.activity_half.task_tokens_to_cancels.values() {
cancellation.cancel();
}

@Sushisource Sushisource Oct 12, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't need to be added here. This should've happened as part of initiate_shutdown or otherwise part of the existing shutdown logic. There shouldn't be special activity cancel logic that only runs if you use run_until.

What motivated needing to add this here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was Codex being over-defensive and I missed it.

@eamsden
eamsden force-pushed the eamsden/worker-run-until branch from 4ff1a51 to 2d1309f Compare October 12, 2026 22:27
@eamsden
eamsden changed the base branch from main to eamsden/shutdown-after-validation-failure October 12, 2026 22:27
@eamsden
eamsden added this pull request to stack #1648 October 12, 2026 22:28
Comment thread crates/sdk/src/lib.rs Outdated
Comment on lines +1084 to +1088
self.common.worker.initiate_shutdown();
// Activity completions can release Core's slots before their tasks drop their Core
// references, so wait for the SDK tasks before taking ownership for finalization.
self.activity_half.tasks.close();
self.activity_half.tasks.wait().await;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still think this maybe isn't quite right... initiate_shutdown I think should be unnecessary here. It's called by shutdown_handle.

Then, waiting for this new tasks thing to close, that should probably just be done in run itself because, same thing, there should be no special waiting logic in run_until... it should really only just be calling the shutdown and waiting for run to resolve.

@eamsden
eamsden force-pushed the eamsden/worker-run-until branch from 2d1309f to cab0ae2 Compare October 12, 2026 22:39
@eamsden
eamsden removed this pull request from stack #1648 October 12, 2026 22:45
@eamsden
eamsden changed the base branch from eamsden/shutdown-after-validation-failure to main October 12, 2026 22:45
@eamsden
eamsden force-pushed the eamsden/worker-run-until branch from cab0ae2 to e80a834 Compare October 12, 2026 23:17
Comment thread crates/sdk/src/lib.rs Outdated
.await
.await;

self.common.worker.initiate_shutdown();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still not clear on why this line is needed. run_inner ends up always calling this indirectly via shutdown... so it should've already happened.

The only case seemingly is worker validation errors, in which case we should be able to just short-circuit I think and return that early?

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.

2 participants