Skip to content

fix: Run the module startup script after it has been copied completely - #1810

Merged
HofmeisterAn merged 2 commits into
developfrom
bugfix/startup-script-text-file-busy
Nov 3, 2026
Merged

HofmeisterAn merged 2 commits into
developfrom
bugfix/startup-script-text-file-busy

Conversation

@HofmeisterAn

@HofmeisterAn HofmeisterAn commented Nov 2, 2026 •

Copy link
Copy Markdown
Collaborator

What does this PR do?

The PR adds a protected WithStartupScript method to ContainerBuilder. The Kafka, Pulsar, Redpanda and FakeGcsServer modules now use it. The container's main process waits until a ready file is copied, which happens after the startup script has been copied. It then empties the ready file and execs the script. The script runs only after it has been copied completely, and it's copied again on every start. A restarted container uses its new mapped ports. Because of exec, the script runs as the container's main process, so it receives the stop signal and shuts down cleanly.

For Kafka in ZooKeeper mode, it sets KAFKA_CONTROLLED_SHUTDOWN_RETRY_BACKOFF_MS=0. When the container is stopped right after it starts, Kafka may not know its controller yet and waits 5 s per retry. Docker kills it before it unregisters from ZooKeeper, and the next start fails.

Why is it important?

Containers could run the startup script before it was fully copied, which failed with Text file busy. Stopping and starting a container ran the old script with stale ports, or the container failed to start again.

Related issues

Summary by CodeRabbit

  • Bug Fixes
    • Improved startup handling for Fake GCS Server, Kafka, Pulsar, and Redpanda containers.
    • Updated Kafka’s controlled-shutdown retry behavior.
  • Tests
    • Added coverage for stopping and restarting a Kafka container, including verification that messages can still be delivered afterward.
    • Updated Kafka test coverage to use Apache Kafka Native 4.4.0.

@HofmeisterAn HofmeisterAn added the bug Something isn't working label Nov 2, 2026
@netlify

netlify Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

βœ… Deploy Preview for testcontainers-dotnet ready!

Name Link
πŸ”¨ Latest commit ca05332
πŸ” Latest deploy log https://app.netlify.com/projects/testcontainers-dotnet/deploys/6ae8ee802ba93601ea26996e
😎 Deploy Preview https://deploy-preview-1810--testcontainers-dotnet.netlify.app
πŸ“± Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@coderabbitai

coderabbitai Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

Review in Change Stack β†’

No actionable comments were generated in the recent review.

Walkthrough

The change adds a shared WithStartupScript method that writes generated scripts and a ready marker before the container shell runs the script. Fake GCS, Kafka, Pulsar, and Redpanda builders use it. Kafka also changes ZooKeeper shutdown configuration and native-image test coverage.

Changes

Container startup scripts

Layer / File(s) Summary
Shared startup-script method
src/Testcontainers/Builders/ContainerBuilder3.cs`
WithStartupScript generates the shell entrypoint, writes the startup script with mode 755, and writes the ready marker with mode 666. The entrypoint waits for the marker, clears it, and executes the script.
Builder migration
src/Testcontainers.FakeGcsServer/FakeGcsServerBuilder.cs, src/Testcontainers.Kafka/KafkaBuilder.cs, src/Testcontainers.Pulsar/PulsarBuilder.cs, src/Testcontainers.Pulsar/PulsarContainer.cs, src/Testcontainers.Redpanda/RedpandaBuilder.cs
The four builders use WithStartupScript instead of shell polling and script-copy callbacks. Pulsar and Redpanda return generated script text. Kafka sets the ZooKeeper shutdown retry backoff to "0".
Kafka restart validation
tests/Testcontainers.Kafka.Tests/Dockerfile, tests/Testcontainers.Kafka.Tests/KafkaContainerTest.cs
The Kafka native test image changes from version 4.1.1 to 4.4.0. A test stops and restarts the container, then checks producer-to-consumer message delivery.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant ContainerBuilder
  participant StartupCallback
  participant ContainerFilesystem
  participant ShellEntrypoint
  ContainerBuilder->>StartupCallback: generate startup script text
  StartupCallback-->>ContainerBuilder: return script text
  ContainerBuilder->>ContainerFilesystem: write /testcontainers.sh and /testcontainers.ready
  ShellEntrypoint->>ContainerFilesystem: wait for ready marker
  ContainerFilesystem-->>ShellEntrypoint: return ready marker
  ShellEntrypoint->>ContainerFilesystem: clear marker and execute /testcontainers.sh
Loading

Suggested labels: enhancement

Suggested reviewers: entvex, sukharevandrey

Merge Risk: Low Β· up to ca053

Custom Kafka startup commands are silently ignored; default startup is unaffected.

Pre-merge checks: Passed 5
Check name Status Explanation
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed The description explains the what and the why, and it covers the main technical changes. It states that the new protected WithStartupScript method is added to ContainerBuilder, used by the Kafka, Puls…
Title check Passed The title clearly describes the main change: run the module startup script only after the complete script has been copied. It is specific, concise, and related to the pull request objectives.
Full details: Docstring Coverage

Explanation

Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 3 functions across 7 files. (1 skipped: 1 unsupported.)

Full details: Description check

Explanation

The description explains the what and the why, and it covers the main technical changes. It states that the new protected WithStartupScript method is added to ContainerBuilder, used by the Kafka, Pulsar, Redpanda, and FakeGcsServer modules, and that the startup script runs via exec after a ready file is created. It also explains the Kafka ZooKeeper KAFKA_CONTROLLED_SHUTDOWN_RETRY_BACKOFF_MS=0 change and the motivation (Text file busy errors and stale ports on restart). It links Closes #1809. The Why section is complete. The main gaps are the empty 'How to test this PR' section, which still contains the template placeholder comment, and the unfilled Follow-ups section, which is optional. The Related issues section also still contains a leftover blank template bullet structure that is fine, but the test section is missing content.

Finishing Touches
Generate docstrings
  • Commit to this branch
  • Create a new PR
Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix Β· Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Tip

🎯 Customize your risk assessments

Guide how CodeRabbit assesses merge risk and security architecture risk. Highlight sensitive changes and explain your security boundaries using Merge Risk Instructions and Security Risk Instructions in your review settings. Security architecture risk assessments require the Advanced plan or above.

βœ… Automatic approvals, with your rules

Let CodeRabbit approve pull requests once required threads are resolved, pre-merge checks pass, and your risk limits are met. Enable Automatic approvals in review settings, with overrides for teams or file paths.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

I’m a rabbit with a script to run,
I wait for the ready mark to come.
The shell takes off, the tests restart,
Kafka sends its message smart.
I twitch my nose and hop away,
With startup scripts in place today.

CodeRabbit Verify

  • Run CodeRabbit Verify to test the changes in this pull request.
Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: f8560387-903c-40c4-a34d-55729a83781e
Commits

Reviewing files that changed from the base of the PR and between a6ce741 and ca05332.

Files selected for processing: 8
  • src/Testcontainers.FakeGcsServer/FakeGcsServerBuilder.cs
  • src/Testcontainers.Kafka/KafkaBuilder.cs
  • src/Testcontainers.Pulsar/PulsarBuilder.cs
  • src/Testcontainers.Pulsar/PulsarContainer.cs
  • src/Testcontainers.Redpanda/RedpandaBuilder.cs
  • src/Testcontainers/Builders/ContainerBuilder3.cs`
  • tests/Testcontainers.Kafka.Tests/Dockerfile
  • tests/Testcontainers.Kafka.Tests/KafkaContainerTest.cs

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

@HofmeisterAn HofmeisterAn changed the title fix: Run the module startup script only after it has been copied completely fix: Run the module startup script after it has been copied completely Nov 3, 2026
@HofmeisterAn
HofmeisterAn merged commit 448635a into develop Nov 3, 2026
164 checks passed
@HofmeisterAn
HofmeisterAn deleted the bugfix/startup-script-text-file-busy branch November 3, 2026 05:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Kafka startup executes /testcontainers.sh before its writer closes (ETXTBSY)

1 participant