[Bug]: GenericContainer.start() and stop() are not thread-safe

Đang mở
#11,719 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
35/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
docker, java
Lĩnh vực
testing-qa

Hướng nghiên cứu

Bắt đầu với GenericContainer.start() và stop(), đặc biệt là guard containerId được mô tả trong issue. Xem lại PR #11702 và ví dụ đồng thời với @Testcontainers và @Container; được xem là hoàn tất khi các lệnh gọi đồng thời tới start và stop an toàn, không tạo các container trùng lặp và không xử lý sai dependency dùng chung.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

type/bug
Module

Core

Testcontainers version

2.0.4

Using the latest Testcontainers version?

Yes

What happened?

GenericContainer.start() guards against double-start with if (containerId != null) return, but start() is not synchronized. When two threads call start() on the same container concurrently, both can pass the guard before either sets containerId, creating two Docker containers for one logical dependency.

This can happen when @Testcontainers is used and the container is also started from another context. For example, custom test infrastructure that handles @Container annotations alongside the JUnit extension:

@Testcontainers
class MyTest {

    // Custom infrastructure starts this container during context setup.
    // The @Testcontainers extension also starts it via Startables.deepStart().
    // Both run concurrently - the second start() should be a no-op, but without
    // synchronization both threads pass the containerId == null check.
    @Container
    static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:16");
}

The workaround is to not annotate dependencies with @Container when custom infrastructure already handles them, but this is not obvious to developers and error-prone.

I can't provide the exact scenario because we faced this in a closed-source project. The example above illustrates the general pattern.

Additional Information

I submitted a PR with a fix: #11702

Ngôn ngữ chính
Java
Star
8.7k
Fork
1.9k
Merge trung bình
2 ngày 17 giờ
Pull request đã merge (30 ngày)
9

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của testcontainers/testcontainers-java

Tất cả issue của testcontainers/testcontainers-java

Issue tương tự

Thêm issue về Java

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.