Skip to content

Await the initial mongoose connection in MongoDB tests - #1500

Merged
cristianrgreco merged 3 commits into
mainfrom
claude/fix-atlas-local-ready-race
Oct 11, 2026
Merged

cristianrgreco merged 3 commits into
mainfrom
claude/fix-atlas-local-ready-race

Conversation

@cristianrgreco

@cristianrgreco cristianrgreco commented Oct 11, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

The MongoDB tests now await the mongoose initial connection: mongoose.createConnection(...) became await mongoose.createConnection(...).asPromise() in both test files (five calls).

createConnection() returns immediately and connects in the background, and mongoose queues operations until the connection is up, so the happy path is unchanged. The difference is when the first connect fails: the background promise used to reject with nothing handling it, so Vitest reported an unhandled rejection and exited 1 even though the retried test passed. Now the error is thrown inside the test, where the CI retry can absorb it.

This is what failed Tests (mongodb, 24.x, docker) in 2 of 92 mongodb jobs across the last 45 Checks runs (jobs 114290620317 and 113776788163): one retried test plus one unhandled rejection.

Also adds a rule to AGENTS.md: a change that only makes a failure less likely (arbitrary delays, repeated checks, more retries) is not a fix.

Not changed

The first connect fails in those jobs because the Atlas Local image can report healthy just before it restarts mongod at the end of its first-run initialisation. An earlier revision of this PR ran runner healthcheck twice to narrow that window; it was dropped because it only reduced the odds. The image exposes no deterministic signal for its final mongod that holds across image builds, so the wait strategy is unchanged.

Verification

  • npm run format, npm run lint
  • npx vitest run packages/modules/mongodb/src twice without retries: 10/10 passed, no unhandled errors
  • Throwaway check: an unawaited createConnection() against a server that drops every connection makes Vitest report an unhandled MongoServerSelectionError; with asPromise() awaited the rejection is handled.

Not breaking

Tests and AGENTS.md only; no change to published code. The docs snippets included from these tests now show await ...asPromise().

The Atlas Local image restarts mongod at the end of its first-run
initialisation, and `runner healthcheck` can pass on the mongod that is
about to be stopped. A client connecting straight after `start()` then
reached a server that was shutting down.

Run the healthcheck twice in the wait strategy: the second check fails
or blocks until the final mongod is up.

Also await the mongoose initial connection in the MongoDB tests, so a
failed first connect fails the test instead of surfacing as an
unhandled rejection that Vitest retries cannot absorb.
@cristianrgreco cristianrgreco added bug Something isn't working patch Backward compatible bug fix labels Oct 11, 2026
Running `runner healthcheck` twice only made the early-ready race in the
Atlas Local image less likely, so revert the wait strategy change and
its simulated regression test. The image exposes no deterministic signal
for its final mongod.

What remains is the test change: awaiting the mongoose initial
connection, so a failed first connect fails the test instead of
surfacing as an unhandled rejection.

Record in AGENTS.md that odds-reducing changes are not fixes.
@cristianrgreco cristianrgreco changed the title Fix MongoDB Atlas Local container reporting ready before mongod restarts Await the initial mongoose connection in MongoDB tests Oct 11, 2026
@cristianrgreco cristianrgreco added maintenance Improvements that do not change functionality and removed bug Something isn't working labels Oct 11, 2026
@cristianrgreco
cristianrgreco merged commit b844a73 into main Oct 11, 2026
18 checks passed
@cristianrgreco
cristianrgreco deleted the claude/fix-atlas-local-ready-race branch October 11, 2026 18:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintenance Improvements that do not change functionality patch Backward compatible bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant