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

オープン 初心者向け
#1,138 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@granzer69 がすでに取り組んでいます。

2026年10月22日 から。

  • #1139 @granzer69 による — オープン

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
76/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
docker, python
領域
devtools, testing

調査の方向性

core/wait_strategies.pyを読み、特にLogMessageWaitStrategy.wait_until_readyとそのタイムアウト経路を確認してください。次にcore/container.pyで、DockerContainer.startが例外をどのように処理するか確認してください。提示されたMongoDBコマンドで再現し、プロセス終了時のエラーとタイムアウトエラーを比較してください。プロセス終了経路のRuntimeErrorメッセージに、利用可能なstdoutとstderrの末尾が含まれる状態になれば完了です。

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
Python
スター
2.3k
フォーク
395
PR マージ指標
30日以内にマージされた PR はありません

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

testcontainers/testcontainers-python のほかの issue

testcontainers/testcontainers-python の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。