Skip to content

Expose ShutdownManager poll interval to speed up TestWorkflowEnvironment teardown#2876

Open
brucearctor wants to merge 1 commit into
temporalio:masterfrom
brucearctor:expose-shutdown-check-interval
Open

Expose ShutdownManager poll interval to speed up TestWorkflowEnvironment teardown#2876
brucearctor wants to merge 1 commit into
temporalio:masterfrom
brucearctor:expose-shutdown-check-interval

Conversation

@brucearctor
Copy link
Copy Markdown
Contributor

Summary

Makes the hard-coded 250ms CHECK_PERIOD_MS in ShutdownManager configurable through a new WorkerFactoryOptions.Builder#setShutdownCheckInterval(Duration) option.

Motivation

Resolves #2869. In env-per-test setups the 250ms shutdown poll interval dominates teardown wall-time:

  • p50 = 506ms (stock) → 7ms (with a lower interval)
  • ~257ms/test overhead reproduced internally

Changes

File Change
ShutdownManager Convert CHECK_PERIOD_MS from a static constant to a configurable instance field. No-arg constructor preserves the 250ms default. New ShutdownManager(int checkPeriodMs) constructor with validation.
WorkerFactoryOptions Add shutdownCheckInterval field + Builder#setShutdownCheckInterval(Duration) with positive-duration validation.
WorkerFactory Wire the configured interval through to ShutdownManager in doShutdown().

Usage

// Production: no change needed, defaults to 250ms

// Test environment: set a fast interval
WorkerFactoryOptions factoryOptions = WorkerFactoryOptions.newBuilder()
    .setShutdownCheckInterval(Duration.ofMillis(1))
    .build();
TestEnvironmentOptions testOptions = TestEnvironmentOptions.newBuilder()
    .setWorkerFactoryOptions(factoryOptions)
    .build();
TestWorkflowEnvironment env = TestWorkflowEnvironment.newInstance(testOptions);

Backward Compatibility

Fully backward compatible — the no-arg ShutdownManager() constructor and default WorkerFactoryOptions preserve the existing 250ms behavior.

@brucearctor brucearctor requested a review from a team as a code owner May 14, 2026 03:17
@brucearctor brucearctor force-pushed the expose-shutdown-check-interval branch from 4edebfc to 5d644cf Compare May 14, 2026 03:44
Copy link
Copy Markdown
Contributor

@maciejdudko maciejdudko left a comment

Choose a reason for hiding this comment

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

Hi @brucearctor, thank you for your contribution. There are some minor issues that need to be addressed, but overall it's a great PR and hopefully we can get this merged soon.

Comment thread temporal-sdk/src/test/java/io/temporal/internal/worker/ShutdownManagerTest.java Outdated
Comment thread temporal-sdk/src/test/java/io/temporal/internal/worker/ShutdownManagerTest.java Outdated
Comment thread temporal-sdk/src/main/java/io/temporal/worker/WorkerFactory.java Outdated
Comment thread temporal-sdk/src/main/java/io/temporal/worker/WorkerFactoryOptions.java Outdated
Make the hard-coded 250ms CHECK_PERIOD_MS in ShutdownManager configurable
through a new WorkerFactoryOptions.Builder#setShutdownCheckInterval(Duration)
option. This allows test environments to use a much lower value (e.g. 1ms)
to eliminate the shutdown polling overhead that dominates env-per-test
teardown time (reported p50 = 506ms stock vs 7ms with a patched constant).

The no-arg ShutdownManager constructor preserves the existing 250ms default
for production use. When the option is set on WorkerFactoryOptions,
WorkerFactory passes it through to the ShutdownManager constructor.

Changes from review feedback:
- Add DEFAULT_SHUTDOWN_CHECK_INTERVAL constant to WorkerFactoryOptions and
  apply it as default in validateAndBuildWithDefaults, similar to
  workflowCacheSize
- Add @experimental annotation to setter and getter
- Remove @nullable from field/getter since validated options always have
  the interval filled
- Simplify WorkerFactory.doShutdown() by removing null check
- Remove flaky/trivial ShutdownManagerTests, keep only validation tests
- Update WorkerFactoryOptionsTest to verify default is 250ms

Fixes temporalio#2869
@brucearctor brucearctor force-pushed the expose-shutdown-check-interval branch from 5d644cf to fb10321 Compare May 15, 2026 00:23
@brucearctor
Copy link
Copy Markdown
Contributor Author

@maciejdudko -- thanks for the thorough review! addressed

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.

Expose ShutdownManager poll interval to speed up TestWorkflowEnvironment teardown

2 participants