Skip to content

Fix wait strategy startup timeout being overridden by container timeout - #1669

Open
Vishv510 wants to merge 2 commits into
testcontainers:mainfrom
Vishv510:fix/wait-strategy-startup-timeout-precedence
Open

Vishv510 wants to merge 2 commits into
testcontainers:mainfrom
Vishv510:fix/wait-strategy-startup-timeout-precedence

Conversation

@Vishv510

@Vishv510 Vishv510 commented Nov 25, 2026 •

Copy link
Copy Markdown

Summary

GenericContainer.start() and DockerComposeEnvironment.up() unconditionally
called waitStrategy.withStartupTimeout(this.startupTimeoutMs) just before
waiting for the container, overwriting any timeout the caller had already set
explicitly on the wait strategy - and mutating the caller's wait strategy
instance in the process. CompositeWaitStrategy already gets this right for
its own children via isStartupTimeoutSet(); this brings the same precedence
to the container/environment level.

  • AbstractWaitStrategy gains withDefaultStartupTimeout(startupTimeoutMs):
    a no-op if the strategy already has its own timeout set, otherwise it
    applies the default to a clone and returns that, leaving the caller's
    original instance untouched.
  • GenericContainer.startContainer / reuseContainer and
    DockerComposeEnvironment's toStartedGenericContainer now call this
    instead of mutating the wait strategy directly.
  • docs/features/wait-strategies.md documents the precedence.

Not in scope: Kafka and Redpanda modules duplicate the old
unconditional-mutation pattern internally and still have this bug - a
candidate for a follow-up rather than expanding this PR's blast radius.

Not implemented: what happens when a wait strategy's own timeout is
longer than the container's timeout (ceiling vs. override) - per the
issue's own note, that's a separate design decision to coordinate with #1582.

Verification

  • npx tsc -b packages/testcontainers packages/modules/*, npm run knip,
    biome lint/biome format on changed files - all clean.

  • Red, on the unfixed code (temporarily reverted generic-container.ts,
    docker-compose-environment.ts, wait-strategy.ts to their pre-fix state,
    keeping the new tests):

    FAIL GenericContainer > should respect the wait strategy's own startup
    timeout over the container's startup timeout
    Expected: "Log message "this never appears" not received after 1000ms"
    Received: "Log message "this never appears" not received after 6000ms"

    FAIL GenericContainer > should not mutate a reused wait strategy's own
    startup timeout when applying the container's default
    Expected: false
    Received: true

    FAIL DockerComposeEnvironment > should respect the wait strategy's own
    startup timeout over the environment's startup timeout
    Expected: "Log message "unexpected" not received after 1000ms"
    Received: "Log message "unexpected" not received after 60000ms"

    Test Files 2 failed | Tests 3 failed | 81 skipped (84)

  • Green, with the fix restored:

    Test Files 2 passed (2)
    Tests 3 passed | 81 skipped (84)
    Duration 6.90s

  • Full regression run on both suites: Test Files 2 passed | Tests 83 passed | 1 skipped (84).

Not breaking

Existing callers who only set a timeout on one side (container or
strategy, not both) see no change. Callers who previously set both and were
relying on the container's timeout silently winning will now see the
strategy's explicit timeout take effect instead - this is the bug being
fixed.

Closes #1523

@Vishv510
Vishv510 marked this pull request as ready for review November 25, 2026 19:35

@cristianrgreco cristianrgreco left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for picking this up! A few things before this can go in:

  1. CouchDBContainer sets withStartupTimeout(120_000) on the container and withStartupTimeout(60_000) on its HTTP wait strategy. Today the wait gets 120s. With this change it gets 60s, and new CouchDBContainer(image).withStartupTimeout(...) no longer extends it. Drop the withStartupTimeout(60_000) from the module's wait strategy so it keeps the container's 120s.
  2. KafkaContainer and RedpandaContainer run the user's wait strategy in containerStarted, using the same lines this PR replaces (kafka-container.ts:197-200, redpanda-container.ts:83-86). Until they use withDefaultStartupTimeout too, a timeout set on a Kafka or Redpanda user's wait strategy is still overridden, so please include them here rather than in a follow-up.


getStartupTimeout(): number;

withDefaultStartupTimeout(startupTimeoutMs: number): WaitStrategy;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

WaitStrategy is exported and AbstractWaitStrategy isn't, so a custom strategy that isn't a StartupCheckStrategy implements this interface directly. Adding a required method breaks those: they no longer compile, and from JS start() throws withDefaultStartupTimeout is not a function whenever the container has a startup timeout, which most modules set. Keep the interface unchanged, handling strategies that don't extend AbstractWaitStrategy with the existing isStartupTimeoutSet()/withStartupTimeout(), or this needs major.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Fixed - reverted interface to its original shape and moved this to a standalone applyDefaultStartupTimeout() function instead. It checks instance of AbstractWaitStrategy first and falls back to the existing isStartupTimeoutSet()/withStartupTimeout() for anything else, so there's no interface break. See wait-strategy.ts in lestest pr commit

if (this.startupTimeoutSet) {
return this;
}
const clone: this = Object.assign(Object.create(Object.getPrototypeOf(this)), this);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is a shallow copy, so for Wait.forAll([...]) the clone shares the children, and CompositeWaitStrategy.withStartupTimeout sets the timeout on the caller's children. A composite reused across containers keeps the first container's timeout: start one with withStartupTimeout(1_000), then another with 2_000, and the second fails after 1000ms. Override this in CompositeWaitStrategy to build a new composite from each child's withDefaultStartupTimeout, keeping the deadline, and cover Wait.forAll in the reuse test.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Fixed- CompositeWaitStrategy now overrides withDefaultStartupTimeout to build new composite from each childs own applyDefaultStartupTimeout(...) result instead of cloning this so the original children are never mutated. Added a test covering the exact repro (reuse with withStartupTimeout(1_000) then 2_000) in composite-wait-strategy.test.ts

Comment thread docs/features/wait-strategies.md Outdated
.start();
```

`GenericContainer.withStartupTimeout` only applies to a wait strategy that has not set its own startup timeout. A timeout set explicitly on the wait strategy itself always takes precedence:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nit: this applies to DockerComposeEnvironment.withStartupTimeout too. Something like: "A startup timeout set on the wait strategy itself takes precedence over withStartupTimeout on the container or Compose environment:"

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Updated- now reads 'A startup timeout set on the wait strategy itself takes precedence over withStartupTimeout on the container or Compose environment'

@cristianrgreco cristianrgreco added bug Something isn't working patch Backward compatible bug fix labels Nov 29, 2026
@Vishv510

Copy link
Copy Markdown
Author

Thanks for the detailed review, @cristianrgreco
I have pushed follow-up commit addressing all five points:

  • Reverted WaitStrategy back to its original interface shape. applyDefaultStartupTimeout is now standalone function that only special-cases AbstractWaitStrategy; any other implementation falls back to existing isStartupTimeoutSet()/withStartupTimeout() methods, so there's no interface break.
  • CompositeWaitStrategy now overrides withDefaultStartupTimeout to build new composite from each child's own defaulted result instead of sharing child references, so a reused Wait.forAll(...) no longer leaks one container's timeout onto the next. Added a regression test covering that reuse case.
  • Applied the same fix in KafkaContainer and RedpandaContainer's wait-strategy re-selection
  • Removed CouchDBContainer's own withStartupTimeout(60_000), so it goes back to inheriting the container-level timeout like before
  • Updated the docs line to also mention DockerComposeEnvironment

Could you also approve the latest workflow run when you get a chance?

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

bug Something isn't working patch Backward compatible bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Stop the container startup timeout overriding a wait strategy's own timeout

2 participants