Skip to content

Call the log consumer again when a container is restarted - #1665

Open
yabdelhamed wants to merge 2 commits into
testcontainers:mainfrom
yabdelhamed:fix/log-consumer-restart
Open

yabdelhamed wants to merge 2 commits into
testcontainers:mainfrom
yabdelhamed:fix/log-consumer-restart

Conversation

@yabdelhamed

Copy link
Copy Markdown

Summary

restart() reattaches the internal container logger, but not the consumer from withLogConsumer. The consumer's stream ends when the container stops and nothing replaces it, so it gets no output after the restart.

StartedGenericContainer now gets the consumer from GenericContainer (on both the start and the reuse path) and calls it again in restart() with a new stream, using the same since as the internal logger, so lines from before the restart aren't replayed. Go keeps log consumers across stop/start too.

The log consumer docs now say what happens on restart.

Closes #1663

Verification

Red-green: new "should keep streaming logs to the log consumer after restart" in generic-container-restart.test.ts. Before the fix: expected [ 'Listening on port 8080\n' ] to have a length of 2 but got 1.

The scenario from the issue, an alpine container printing tick N every second, restarted after 3.5s:

  • before: after: []
  • after: after: ["boot","tick 1","tick 2","tick 3","tick 4","tick 5"], nothing from the first run repeated

Runs:

  • npx vitest run on the restart, logs and reuse tests and docker-compose-environment: 47 passed. should work with buildkit fails locally because I don't have the buildx plugin installed; it doesn't touch this code.
  • biome ci --error-on-warnings, npm run knip, npm run check-compiles: clean.

@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 the fix and the red-green evidence. I also checked that the new test fails when since is dropped, so it covers the no-replay part too.

  1. Share the log consumer and internal logger setup between start and restart instead of adding a second copy (inline).

Nits inline.

const mappedInspectResult = mapInspectResult(this.inspectResult);
const startTime = new Date(this.inspectResult.State.StartedAt);

if (this.logConsumer !== undefined) {

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 repeats GenericContainer.consumeContainerLogs, so start and restart each have their own copy of the consumer and internal logger setup. Move consumeContainerLogs into its own file, like inspectContainerUntilPortsExposed, with an optional since, and call it from startContainer and here.

expect((await container.exec(["cat", "config.txt"])).output).toEqual(expect.stringContaining("testconfig"));
});

it("should keep streaming logs to the log consumer after restart", async () => {

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: generic-container-logs.test.ts already has the log consumer test and the internal logger after restart test, so this belongs next to them.

await container.restart();

await vi.waitFor(() => expect(lines.filter((l) => l.includes("Listening on port 8080"))).toHaveLength(2), {
timeout: 10_000,

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: the consumer's stream is open before restart() waits for the container, so the default waitFor timeout is enough here, as in the first waitFor.

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

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.

A consumer passed to withLogConsumer receives no logs after restart()

2 participants