Bug: LogMessageWaitStrategy: a container that exits before its message raises without its logs, and __enter__ then removes the container

Đang mở Phù hợp với người mới
#1,138 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@granzer69 đang làm issue này rồi.

Từ ngày 22/10/2026.

  • #1139 của @granzer69 — đang mở

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
76/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
docker, python
Lĩnh vực
devtools, testing

Hướng nghiên cứu

Đọc core/wait_strategies.py, đặc biệt là LogMessageWaitStrategy.wait_until_ready và nhánh timeout của hàm này. Sau đó, kiểm tra core/container.py để xem DockerContainer.start xử lý ngoại lệ như thế nào. Tái hiện lỗi bằng lệnh MongoDB được cung cấp và so sánh lỗi khi tiến trình đã thoát với lỗi timeout. Hoàn tất khi thông báo RuntimeError ở nhánh tiến trình đã thoát có chứa phần cuối stdout và stderr hiện có.

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

Mô tả

Version: testcontainers 4.15.0 (Python). The same code is on main as read on 2026-10-20.

What happens. A container that exits before it logs the message a LogMessageWaitStrategy waits for raises:

RuntimeError: Container exited (status: exited, exit code: 1) before emitting logs containing 'waiting for connections'. Container error: . Hint: Check container logs and ensure the application is configured to start correctly. The application may be crashing or exiting early.
The hint says to check the container's logs, but there is no way to read them afterwards:

LogMessageWaitStrategy.wait_until_ready (core/wait_strategies.py) has the logs in hand when it notices the exit. It has just called container.get_logs() to search them. But the RuntimeError it raises carries only the status, the exit code and State.Error, which is the daemon's own error field and is empty when the process exits on its own. Its timeout path does include "Recent stdout" and "Recent stderr", while its exited path does not.
DockerContainer.start (core/container.py) attaches the logs as a note only on except TimeoutError, so the RuntimeError above passes through without them.
DockerContainer.enter catches any exception from start(), calls exit, which stops and removes the container with remove(force=True, v=True), and re-raises. By the time the error reaches the caller, the container and its logs are gone.
So a container that dies at start under with DockerContainer(...) as c: leaves nothing to diagnose it with. This is the case the hint is written for.

How we met it. mongod 8.3.13 refuses some host kernels at startup (SERVER-121912). It logs one fatal line and exits 1, and GitHub's ubuntu-24.04 runner image 20261018 carries such a kernel. Our CI showed only the message above, about 3,000 times, once per test. The cause became visible only after we started our containers without the context manager, read get_logs() in our own except and removed the container ourselves.

To reproduce:

from testcontainers.core.container import DockerContainer
from testcontainers.core.wait_strategies import LogMessageWaitStrategy

container = DockerContainer("mongo:8.3.13").with_command("--refusedAtStart")
container.waiting_for(LogMessageWaitStrategy("waiting for connections"))
with container:
pass
mongod refuses the unknown option, logs why and exits. The error names neither the option nor mongod's line.

Suggested fix. On the exited path, include the tail of the stdout and stderr the strategy already read, as the timeout path does. Because the message carries the log, a caller that only has str(error) gets it too: pytest-xdist workers, for example, receive only the string, so a note would be lost on the way.

Ngôn ngữ chính
Python
Star
2.3k
Fork
395
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Mở trong Codespaces

Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.

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

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

Issue tương tự

Thêm issue về Python

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.