[Bug]: sbx proxy serialises container creates, including ones that will conflict

Open
#4,100 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
1-2 days
Newbie friendliness
48/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
docker, go
Domain
backend, testing

Research direction

Start with modules/sbx/leases.go, especially portManager.prepareCreate and abortCreate, then inspect the sbx Docker API proxy path that handles container creates. Trace how allocationMu is held around port allocation and cleanup, and compare the proxy's conflict response with Docker's. Done when conflicting concurrent creates fail fast without delaying other creates and TestReaper_ReuseRunning succeeds under sbx.

Written by the indexing model from the issue text.

Description

bug
Testcontainers version

v0.46.0 (main)

Using the latest Testcontainers version?

Yes

Host OS

Linux, GitHub Actions ubuntu-24.04 runners in the Test core using sbx jobs

Host arch

x86_64

Go version

1.26.10 and 1.27.3

Docker version

sbx v0.51.0 (docker-sbx 0.51.0-1~ubuntu.24.04~noble), sandbox Engine 29.9.2

Docker info

Operating System: Docker Hardened Images/Debian GNU/Linux 13 (trixie) (containerized)

What happened?

Under the sbx runtime, concurrent container creates are handled one at a time by the sbx module's Docker API proxy, including creates that Docker will reject on a name conflict. This makes TestReaper_ReuseRunning fail intermittently under sbx, and slows down any processes that create containers concurrently.

Every create first goes through portManager.prepareCreate (modules/sbx/leases.go), which holds allocationMu while it lists the engine's ports and runs sbx ports <sandbox> --publish ... for each port, before the request reaches Docker. When Docker then rejects the create, for example on a name conflict, abortCreate takes allocationMu again to release the ports. Each such create therefore costs a serialised round of sbx CLI calls, about 250 to 500ms in CI, and every other create waits behind it.

TestReaper_ReuseRunning starts 64 spawners at once, standing in for 64 test processes, so 63 lose the race to create the session's reaper. Each loser only gets to reuse the reaper once its own create has been rejected, so they get it at about two to four a second. The winning reaper's start waits on the same lock, taking 6 to 10 seconds, and the spawners give up after their 20 second retry budget. Across the sbx runs in #4095 and #4097, the test passed when that worked out to about 0.3s per spawner, with all 64 getting the reaper, and failed at about 0.5s per spawner, with only 36 getting it in time. Halving the Docker API requests each reuse makes (#3868) did not change the rate, so it isn't the request count.

The proxy could reject such creates before reserving ports, for example by checking that the container name isn't already in use and returning Docker's conflict straight away, or avoid holding allocationMu across the CLI call. Either would make the losing creates fail fast and stop them delaying the others.

Relevant log output
TestReaper_ReuseRunning (Go 1.27.x, sbx), 64 spawners:
22:07:59 64 x Creating container for image testcontainers/ryuk:0.14.0
22:07:59 Container created: <reaper>
22:08:09 Container started: <reaper>
22:08:09 .. 22:08:18 Reaper obtained, about 4 a second, 35 in total
new reaper: run container: container create: Error response from daemon: Conflict. The container name "/reaper_<session>" is already in use by container "<reaper>". You have to remove (or rename) that container to be able to reuse that name.
FAIL TestReaper_ReuseRunning (36.26s)
Additional information

@mdelapenya, as this is in the sbx proxy added in #3987, could you take a look? #4097 lowers TestReaper_ReuseRunning's concurrency to 32 under sbx as a workaround until the proxy is fixed. The other sbx CI failures are tracked in #4096.

Dominant language
Go
Stars
5k
Forks
660
Avg merge
3d 11h
Merged PRs (30d)
48

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from testcontainers/testcontainers-go

All issues in testcontainers/testcontainers-go

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.