Skip to content

Avoid 'Text file busy' race when starting KafkaContainer#11769

Open
singhvishalkr wants to merge 1 commit intotestcontainers:mainfrom
singhvishalkr:fix-kafka-startup-script-race
Open

Avoid 'Text file busy' race when starting KafkaContainer#11769
singhvishalkr wants to merge 1 commit intotestcontainers:mainfrom
singhvishalkr:fix-kafka-startup-script-race

Conversation

@singhvishalkr
Copy link
Copy Markdown

The container's startup command waits for /tmp/testcontainers_start.sh to appear, then execs it. containerIsStarting writes the script via copyFileToContainer, which is a single Container Archive PUT. On busy hosts the directory entry can become visible before the docker engine has closed the writer, and the immediate exec then fails with ETXTBSY (exit code 126), which is what #11682 reports.

This PR switches to a two-file handshake: write the starter script first, then a separate /tmp/testcontainers_start.ready marker. The wait loop in KafkaHelper.COMMAND now polls the marker instead of the script. The marker copy only completes after the script copy has fully closed, so by the time the wait loop unblocks the subsequent exec against the script is safe. The marker itself is never executed, so its own copy can race with the directory listing harmlessly.

Fixes #11682.

Fixes testcontainers#11682.

The container's command waits for /tmp/testcontainers_start.sh to appear,
then exec's it. containerIsStarting writes the script via a single
Container Archive PUT. On busy hosts the directory entry becomes visible
before the docker engine has released the writer, and the immediate exec
fails with ETXTBSY (exit 126).

Switch to a two-file handshake: write the starter script first, then a
separate /tmp/testcontainers_start.ready marker. The wait loop polls the
marker instead of the script. By the time the second copyFileToContainer
returns, the script's writer is fully closed, so the subsequent exec is
safe. The marker file is never executed, so its own copy can race with
the directory listing harmlessly.
@singhvishalkr singhvishalkr requested a review from a team as a code owner May 7, 2026 17:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Kafka container fails with 'Container exited with code 126' due to a race between command wait loop and script installation

1 participant