Make container reuse safe across processes

Open
#1,553 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 4 days

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
docker, node.js, typescript
Domain
devtools, testing

Research direction

Read reuseOrStartContainer in packages/testcontainers/src/generic-container/generic-container.ts and the existing withFileLock implementation in packages/testcontainers/src/common/file-lock.ts. Check the tests for reusable container startup and run the relevant test suite with multiple processes to reproduce the race. Done means concurrent callers on the same host share one container; the issue notes that #1522 and #1552 also change this function, so coordinate around that overlap.

Written by the indexing model from the issue text.

Description

enhancement minor

What happens

withReuse() looks for an existing container and creates one if there is none. That lookup-then-create is guarded by an in-process lock (AsyncLock), so two processes can both find nothing and both create a container.

Test runners commonly run test files in separate processes or workers. Starting the same reusable configuration from three processes at once created two containers. The extra one is never cleaned up, because reusable containers carry no session label.

Expected

Lookup-or-create is atomic across processes on the same host, so concurrent callers end up sharing one container.

Where

reuseOrStartContainer in packages/testcontainers/src/generic-container/generic-container.ts.

Notes

  • withFileLock in packages/testcontainers/src/common/file-lock.ts already serialises Ryuk and SSHD startup across processes. The same mechanism fits here.
  • Related: #1172, where the answer was to use the test runner's global setup. That remains the better fit for one shared container, but withReuse() is also used to keep containers between runs, where the race still applies.
  • Reproduced on main (aa60a746, 12.2.0) with Docker 29.6.1.

Dependencies

  • Stack on #1522, #1552: all three rewrite reuseOrStartContainer.
Dominant language
TypeScript
Stars
2.7k
Forks
290
Avg merge
4d 1h
Merged PRs (30d)
22

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-node

All issues in testcontainers/testcontainers-node

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.