Skip to content

Set the MongoDB server version of the MongoDB sample, and start its web apps without the database - #152

Merged
paolosalvatori merged 2 commits into
mainfrom
cosmosdb-mongodb-server-version
Nov 11, 2026
Merged

paolosalvatori merged 2 commits into
mainfrom
cosmosdb-mongodb-server-version

Conversation

@paolosalvatori

@paolosalvatori paolosalvatori commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

samples/web-app-cosmosdb-mongodb-api/python/scripts/deploy.sh and samples/web-app-cosmosdb-mongodb-api/dotnet/scripts/deploy.sh create the Cosmos DB account with az cosmosdb create --kind MongoDB --server-version 7.0. Since azure-cli 2.90.0, the Azure CLI sends that value as a ServerVersion property (api_properties['ServerVersion'] in cli_cosmosdb_create and cli_cosmosdb_update of src/azure-cli/azure/cli/command_modules/cosmosdb/custom.py), and the service ignores it, so the account runs MongoDB server version 3.6. It is still so in azure-cli 2.92.0.

The MongoDB drivers of both web apps refuse a 3.6 server. On Azure, the Python app failed with ConfigurationError: Server at <account>-francecentral.mongo.cosmos.azure.com:10255 reports wire version 6, but this version of PyMongo requires at least 9 (MongoDB 4.4) and answered HTTP 503; the .NET app's MongoDB.Driver 3.x needs MongoDB 4.0 or later. The Bicep and Terraform deployments are not affected: they send serverVersion.

Fixes SMF-1140

Changes

  • Both deploy.sh read the account's apiProperties.serverVersion. When it differs from MONGODB_API_VERSION, they set serverVersion with az rest (PATCH, with API version 2026-03-15, the one the Azure CLI uses) and wait until the account reports the new version with provisioningState Succeeded, which takes about a minute.
  • Microsoft documents that an upgrade takes around 15 minutes to complete (Upgrade the API version of your Azure Cosmos DB for MongoDB account), and the MongoDB endpoint can go on serving the old version after the account reports the new one. Both web apps exited at startup on the old version, so a deployment in that window failed with "Deployment failed because the site failed to start within 10 mins". Both web apps now start without the database:
    • Python: the MongoDB client is created by the first request that needs it, under a lock, and closed when its database setup fails; until the database answers, the pages show "The database is not available. Try again in a minute." A missing connection string still stops the app at startup.
    • .NET: StoreInitializer runs as a background service that retries every 15 seconds instead of stopping the app after one failure, and ends quietly when the app stops; the pages show the same message instead of an error page, and /health answers 503 until the database answers.

Tests

On Azure (francecentral):

  • The Python script from main: the account runs 3.6, and the app answers HTTP 503 with the PyMongo error above.
  • With the server-version fix and the apps from main: a fresh Python deployment (3.6 upgraded to 7.0, the site started at the first try, GET / 200, POST / 302, the new activity listed) and a fresh .NET deployment (the same, /health ok, POST / with the antiforgery token). Over a deployment of the script from main, the account reported 7.0, but the endpoint still served wire version 6 two minutes later, the Python app exited at startup, App Service waited 10 minutes before starting it again, and az webapp deploy reported "Deployment failed because the site failed to start within 10 mins" about 40 seconds before the app came up healthy.
  • A first take, in 0e5e58e, waited with mongosh from the deploying machine for the account's MongoDB endpoint. Over a deployment of the script from main, that endpoint answered "Request blocked by network firewall" to the deploying machine for more than 30 minutes, while the web app, which reaches the account through the sample's private endpoint, served pages from the moment of the upgrade: the check stopped a deployment that would have worked. dc61c77 removes it.
  • The final Python app over an account downgraded to 3.6: the deployment reported "Site started successfully" (81 s) and GET / answered 200 with the database message. The final deploy.sh then upgraded the account to 7.0 and redeployed ("Site started successfully", 80 s); 90 seconds later the page showed no error, POST / answered 302, and the new activity was listed.
  • The final .NET app and deploy.sh, fresh deployment: 3.6 upgraded to 7.0, the site started at the first try (227 s), /health ok, GET / 200 with no database message, POST / 302 with the antiforgery token, the new activity listed.

Locally, against MongoDB 8.0 in Docker:

  • Python, with the Flask test client: with nothing listening on the configured port, the app loads, GET / answers 200 with the database message, and POST / 302. After mongod starts, without restarting the app, GET / shows no error, POST / adds the activity and lists it, and the delete removes it.
  • .NET, Release build with .NET 10: with nothing listening, the app listens, /health answers 503 and GET / 200 with the database message. After mongod starts, /health answers 200 within a second and GET / shows no error; after mongod stops again, GET / still answers 200 with the message and /health 503. SIGTERM ends the app with exit code 0 and no host error in the log.

On the emulator, this PR's CI runs every deployment method of both languages. The deploy script without the mongosh check is the one that passed on the emulator (localstack/localstack-azure 2026.11.0.dev842) in fresh and repeated runs before 0e5e58e.

Related

🤖 Generated with Claude Code

https://claude.ai/code/session_01FvNuJLfY2KkMwaymfFnz7R

… CLI ignores

deploy.sh of web-app-cosmosdb-mongodb-api (Python and .NET) creates the account with az cosmosdb create --server-version 7.0. Since azure-cli 2.90.0 the CLI sends that value as a ServerVersion property, which the service ignores, so the account runs MongoDB server version 3.6, and the MongoDB drivers of both web apps refuse it. On Azure, the Python app failed with "reports wire version 6, but this version of PyMongo requires at least 9 (MongoDB 4.4)" and answered HTTP 503.

When the account reports a server version other than MONGODB_API_VERSION, the scripts now set serverVersion with az rest and wait until the account reports it, which takes about a minute.

An account that already existed, such as one an earlier run left at 3.6, can hold data, and the MongoDB endpoint of such an account went on serving 3.6 for minutes after the account reported 7.0, so the web app deployed in the meantime failed to start. After upgrading such an account, the scripts wait with mongosh until the endpoint serves at least MongoDB 4.4, and only then deploy the web app. They check for mongosh before they change the account, and both READMEs list it for that case.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FvNuJLfY2KkMwaymfFnz7R

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.

🟡 Changes recommended

Both deployment scripts can skip endpoint readiness checks on rerun after an interrupted upgrade, allowing deployment against MongoDB 3.6 even when the account reports 7.0.

1 open issue
What changed in this PR

This PR works around Azure CLI’s ignored MongoDB server-version setting in the Python and .NET Cosmos DB samples, preventing driver incompatibility with the default MongoDB 3.6 version.

Changes:

  • Set serverVersion through a REST PATCH and wait for the account update to succeed.
  • Add a mongosh endpoint-readiness check after upgrading an existing account.
  • Document the conditional mongosh prerequisite in both READMEs.
File Description
samples/​web-app-cosmosdb-mongodb-api/​python/​scripts/​deploy.sh Adds version correction and endpoint-readiness checks for Python deployments.
samples/​web-app-cosmosdb-mongodb-api/​python/​README.md Documents when Python deployments require mongosh.
samples/​web-app-cosmosdb-mongodb-api/​dotnet/​scripts/​deploy.sh Adds version correction and endpoint-readiness checks for .NET deployments.
samples/​web-app-cosmosdb-mongodb-api/​dotnet/​README.md Documents when .NET deployments require mongosh.

Review effort: Balanced

…e mongosh check

The mongosh check that deploy.sh ran after upgrading an existing account reached the account's public MongoDB endpoint from the deploying machine. On Azure, that endpoint answered "Request blocked by network firewall" for more than 30 minutes after an upgrade, while the web app, which reaches the account through the private endpoint, was already serving pages, so the check stopped a deployment that would have worked. The check and its mongosh prerequisite are removed.

Both web apps now start without the database instead. The Python app creates its MongoDB client on the first request that needs it, under a lock, closes the client when the database setup fails, and shows "The database is not available. Try again in a minute." until the database answers; a missing connection string still stops it at startup. The .NET app initializes the store in a background service that retries every 15 seconds, and its pages show the same message instead of an error page; /health answers 503 until the database answers. A web app deployed while the MongoDB endpoint still serves the old server version therefore starts, and works as soon as the endpoint does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FvNuJLfY2KkMwaymfFnz7R
@paolosalvatori paolosalvatori changed the title Set the MongoDB server version of the MongoDB sample, which the Azure CLI ignores Set the MongoDB server version of the MongoDB sample, and start its web apps without the database Nov 11, 2026
@paolosalvatori
paolosalvatori merged commit 0ca233c into main Nov 11, 2026
8 checks passed

This branch was successfully deployed

1 active deployment
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.

3 participants