Skip to content

Add idle shutdown to the Lambda worker - #2795

Draft
johanngan wants to merge 1 commit into
johanngan/worker-slot-usagefrom
johanngan/lambda-worker-shutdown-tuner
Draft

johanngan wants to merge 1 commit into
johanngan/worker-slot-usagefrom
johanngan/lambda-worker-shutdown-tuner

Conversation

@johanngan

@johanngan johanngan commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

What was changed

Add an experimental Options.IdleTimeout (and TEMPORAL_LAMBDA_IDLE_TIMEOUT, in whole seconds) that gracefully stops the worker before the invocation deadline once it has had no task in hand for at least the given duration. Tasks are counted using the new experimental SlotUsage API added in #2858. The timeout is a lower bound, with a 20% random jitter added so that workers that went idle together don't all shut down at once.

Why?

Letting Lambda workers shut themselves down on an idle condition can help save money for spiky or infrequent workloads.

Checklist

  1. Closes COM-478

  2. How was this tested:

  • New unit tests added.
  • TODO: manual testing with a serverless Lambda WDV with Temporal Cloud, with the following cases:
    • idle shutdown with no work
    • delayed idle shutdown after some work
    • deadline shutdown after continuous workload with no idling
    • deadline shutdown with no activity and idle shutdown disabled
  1. Any docs updates needed?
    In-repo docs updated.

@johanngan
johanngan requested review from 02strich and eamsden October 19, 2026 17:42
@johanngan

Copy link
Copy Markdown
Contributor Author

Draft status until we're happy that this client-side tuner approach is the right one

Comment thread contrib/aws/lambdaworker/defaults.go Resolved
Comment thread contrib/aws/lambdaworker/runworker.go Resolved
@johanngan
johanngan requested a review from Sushisource November 3, 2026 17:20

@Sushisource Sushisource left a comment

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.

Overall my take is we could save a lot of code here by exposing the in flight task count you need from the underlying SDK.

I think it's worth it and had AI sketch what needs to be done and it's pretty small:

  1. In trackingSlotSupplier, add a usedSlotCount() that reads len(usedSlots) under the existing mutex. It could also record the last time a slot was marked used or released, under that same lock, which would let the contrib code compute idle time exactly (my other comment)
  2. On AggregatedWorker, sum that count across these sub-workers:
    • workflowWorker.worker
    • workflowWorker.localActivityWorker
    • activityWorker.worker
    • sessionWorker.creationWorker and sessionWorker.activityWorker
    • nexusWorker.worker

To expose it, I think we should just do:

  • A function in the worker package, such as worker.InFlightTaskCount(w Worker) (int, bool), marked experimental. It would type-assert to *internal.AggregatedWorker. This adds no interface methods, so it can't break anyone.

The only other downside is we need to then cut a release so this package can depend on the updated version w/ the new function.

Comment thread contrib/aws/lambdaworker/idle.go Resolved
Comment thread contrib/aws/lambdaworker/idle.go Resolved
Comment thread contrib/aws/lambdaworker/idle.go Resolved
Comment thread contrib/aws/lambdaworker/idle.go Resolved
Comment thread contrib/aws/lambdaworker/runworker.go Resolved
Add Options.IdleTimeout, also settable via the
TEMPORAL_SERVERLESS_WORKER_IDLE_TIMEOUT environment variable. When set,
each invocation shuts down early once the worker has had no tasks of
any kind in progress for the timeout, using the same shutdown sequence
as the invocation deadline. Idle time is measured from when the last
task finished, read from worker.GetSlotUsage, so the timeout is exact.
Each invocation adds up to 20% random jitter so that workers that go
idle together don't all shut down at once.

Idle shutdown is disabled by default. If slot usage can't be read, the
invocation logs a warning and runs until the deadline as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@johanngan
johanngan changed the base branch from main to johanngan/worker-slot-usage November 6, 2026 21:56
@johanngan
johanngan force-pushed the johanngan/lambda-worker-shutdown-tuner branch from d62b99f to 5d1fdc8 Compare November 6, 2026 21:57
@johanngan
johanngan added this pull request to stack #2859 November 6, 2026 21:59
@johanngan

Copy link
Copy Markdown
Contributor Author

Changed the approach of this PR to rely on a base SDK worker.GetSlotUsage API which reads from the worker's internal trackers instead of reimplementing them as a tuner, see #2858.

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.

3 participants