Skip to content

providers/mongo: ping mongod before yielding from mongodb_container fixture#67066

Merged
potiuk merged 1 commit into
apache:mainfrom
potiuk:fix-mongo-testcontainer-ping-wait
May 17, 2026
Merged

providers/mongo: ping mongod before yielding from mongodb_container fixture#67066
potiuk merged 1 commit into
apache:mainfrom
potiuk:fix-mongo-testcontainer-ping-wait

Conversation

@potiuk
Copy link
Copy Markdown
Member

@potiuk potiuk commented May 17, 2026

The Low dep tests: providers mongo job is occasionally flaky on ARM CI —
every TestMongoHook test errors at setup_method with:

pymongo.errors.ServerSelectionTimeoutError: 172.17.0.1:32769:
[Errno 111] Connection refused

while the same SHA passes on the other parallel runs (e.g. mongo-3.11
failed in run 25978857524
while every other mongo slot on that SHA was green).

Root cause: under uv --resolution lowest-direct, testcontainers is
pinned at the floor (4.12.0). MongoDbContainer.start() only waits for
the "waiting for connections" log line, and mongod can emit that line a
few milliseconds before the TCP listener is actually accepting on the
published port. The fixture yielded get_connection_url() immediately
after start() returned, so the first hook setup hit a closed port.

No testcontainers version on PyPI closes that gap — testcontainers 4.13.2
only made the log regex case-insensitive for legacy mongo 3.6 images
(see testcontainers-python#783),
which doesn't help mongo:8.0. The reliable fix is in our own conftest.

Fix: after container.start() succeeds, actively ping mongod with
pymongo.MongoClient.admin.command("ping") retried for up to 60 s, then
yield. All dependent fixtures now see a mongod that genuinely accepts
connections.

Observed failing CI job: https://github.com/apache/airflow/actions/runs/25978857524/job/76374215829


Was generative AI tooling used to co-author this PR?
  • Yes — Claude Opus 4.7 (1M context)

Generated-by: Claude Opus 4.7 (1M context) following the guidelines

…ixture

Under uv --resolution lowest-direct, testcontainers==4.12.0 is pinned and
MongoDbContainer.start() waits only for the "waiting for connections" log
line. mongod can emit that log line a few milliseconds before the TCP
listener is actually accepting connections on the published port. We
observed a low-frequency ARM CI flake where the entire TestMongoHook
suite errored at setup_method with:

    pymongo.errors.ServerSelectionTimeoutError: 172.17.0.1:32769:
    [Errno 111] Connection refused

while the same SHA passed on the other parallel runs.

No testcontainers version on PyPI closes the log-to-listener gap for
modern mongo images (4.13.2 only made the log regex case-insensitive for
mongo 3.6); the only reliable fix is to actively probe mongod from the
fixture. Add a short pymongo ping loop after get_connection_url() and
before yielding so all dependent fixtures see a mongod that actually
accepts connections.
@potiuk
Copy link
Copy Markdown
Member Author

potiuk commented May 17, 2026

I'd love to get this one merged.


Drafted-by: Claude Code (Opus 4.7); reviewed by @potiuk before posting

@potiuk potiuk merged commit 8ee29ce into apache:main May 17, 2026
89 checks passed
@potiuk potiuk deleted the fix-mongo-testcontainer-ping-wait branch May 17, 2026 19:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants