Skip to content

Fail fast on an unreachable MongoDB - #1174

Merged
grandnode-dev merged 1 commit into
developfrom
fix/mongo-server-selection-timeout
Dec 3, 2026
Merged

grandnode-dev merged 1 commit into
developfrom
fix/mongo-server-selection-timeout

Conversation

@grandnode-dev

Copy link
Copy Markdown
Member

Type: bugfix

Issue

The MongoDB client is built from the connection string with the driver defaults, so server selection waits 30 s. Every storefront request reads the customer from MongoDB without a cache, so while MongoDB is unreachable every non-static request hangs for 30 s before it fails. Waiting requests pile up, and the single-flight cache lock makes later waiters of the same key wait for each earlier attempt in turn.

Solution

StartupApplication.CreateMongoClientSettings builds the client settings as before, and sets ServerSelectionTimeout to 5 s when the connection string does not give serverSelectionTimeoutMS. A value in the connection string always wins, including an explicit 30000. The check reads the parsed ConnectionString.ServerSelectionTimeout (nullable), not the value on MongoClientSettings, because that one already holds the 30 s default when nothing was given.

The option is described next to ConnectionStrings in the four appsettings.json files (storefront, Admin, Store, Vendor).

Not changed:

  • The client-side operation timeout (timeoutMS, CSOT) is not public in driver 3.12 (MongoClientSettings.Timeout is internal), so it is neither set nor documented.
  • Clients built outside the DI registration still use the driver default: MongoPluginStateStore, the installer's DatabaseFactoryContext, and the connection-string constructors of MongoRepository and MongoStoreFilesContext.
  • No retries or circuit breaker around repositories.

Breaking changes

Installations whose connection string does not set serverSelectionTimeoutMS now give up after 5 s instead of 30 s. A replica set that needs longer to elect a new primary should set the value explicitly, e.g. ...?serverSelectionTimeoutMS=15000.

Testing

  1. dotnet test src/Tests/Grand.Infrastructure.Tests --filter "FullyQualifiedName~Startup": the new MongoClientSettingsTests cover the default (5 s), an explicit value (15 s), an explicit driver default (30 s), and other options left as given.
  2. A ping through a client from CreateMongoClientSettings to a closed port (mongodb://127.0.0.1:27999) throws TimeoutException after 5.0 s (A timeout occurred after 5014ms selecting a server); with the driver default it is 30 s.
  3. Full suite: dotnet test GrandNode.slnx: everything passes except 2 tests in Grand.Business.Catalog.Tests and 5 in Grand.Business.Checkout.Tests that failed with collection dropped (shared test database in the parallel run); both projects pass on their own (1209/1210 with 1 skipped, 1195/1195). They do not use the changed code.

Use a 5 s server selection timeout instead of the driver's 30 s when the connection string does not set serverSelectionTimeoutMS; a value in the connection string always wins.
Copilot AI balanced review requested due to automatic review settings December 2, 2026 20:13

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@grandnode-dev
grandnode-dev merged commit 0712646 into develop Dec 3, 2026
9 checks passed
@grandnode-dev
grandnode-dev deleted the fix/mongo-server-selection-timeout branch December 3, 2026 15:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants