PostgreSQL connection timeouts after Linux App Service settings recycle

Ben-4222 0 Reputation points
2026-12-03T09:45:14.7133333+00:00

We run Python 3.12 with Gunicorn/Uvicorn on Azure Linux App Service, using Oryx deployment and PostgreSQL. Configuration changes trigger container replacement. We intermittently see database connection deadlines during or shortly after these transitions.

Optimizing the deployment reduced the Oryx archive from 95.07 MB to 53.47 MB. In a matched comparison, the time from container running to the first successful Azure probe fell from 160.66 seconds on B1 to 44.36 seconds on B2. The B2 settings recycle still failed after its first successful HTTP probe. We observed physical PostgreSQL connection establishment timeouts and separate checkout timeouts while both worker-pool slots were unavailable. PostgreSQL resource utilization stayed low in the observed windows. After restoring B1, a similar worker blockage cleared on the same process without a restart, about 19 minutes after the failure was first observed.

The platform probe passed before the worker failures. We verify operational readiness separately using reusable pool capacity and worker progress. Microsoft Support said retained platform telemetry provides coarse lifecycle and resource timing, with limited visibility into per-probe routing, container overlap, CPU scheduling, and DNS, TCP, TLS or authentication timing for individual database connections.

Has anyone seen this pattern? Which supported App Service settings or deployment approaches, including prebuilt containers, helped with recycle reliability? What prospective diagnostics can correlate container replacement and scheduling with PostgreSQL connection establishment, pool checkout, and actual return of worker capacity?

Azure App Service
Azure App Service

Azure App Service is a service used to create and deploy scalable, mission-critical web apps.


2 answers

Sort by: Most helpful
  1. Allan Solomon Mejia 11,065 Reputation points
    2026-12-03T10:18:55.2833333+00:00

    Hi @Ben-4222

    The most useful clue is that the B2 instance passed the HTTP probe before the connection and pool checkout failures. The smaller Oryx artifact and faster startup helped get the app responding sooner, but they didn’t prevent the failure. The B1 worker recovering in the same process also means a restart wasn’t required. Neither observation tells us yet whether the bottleneck was connection setup, pool behavior, or worker scheduling.

    For recycle reliability, I’d make the startup probe reflect actual readiness. On Linux App Service, set WEBSITE_WARMUP_PATH=/ready and WEBSITE_WARMUP_STATUSES=200. Without an explicit status list, even a 503 can count as a successful warm-up response. Have /ready report whether startup has established usable pool capacity, with a bounded wait; don’t open a new database connection on every ping. With multiple Gunicorn workers, make sure the result reflects the capacity you intend to serve; a response from one worker doesn’t prove the others are ready. App Service Health Check is separate and is most useful for routing around an unhealthy instance when you run at least two instances. Neither setting makes a single-instance recycle interruption-free.

    A prebuilt custom container pinned to a specific image is a useful comparison: it takes Oryx packaging and extraction out of that test, but it won’t eliminate App Service recycles or fix a slow PostgreSQL handshake. If the goal is safer releases, consider a staging slot on Standard or higher, with swap warm-up configured separately. That helps with deployments; it doesn’t make direct production app-setting edits restart-free.

    For the next repro, capture three things on the same timeline:

    1. Container and worker: Log UTC timestamps, App Service instance ID, a unique startup ID, and worker PID for process start, readiness changes, and first successful work. Add a per-worker event-loop lag measurement to spot stalls; lag alone doesn’t prove CPU scheduling was the cause.
    2. Connection and pool: Log physical connect start/end separately from checkout start/end and connection return. Include exception type, elapsed time, configured deadline, and available/in-use/waiting counts where the pool exposes them. If the driver only reports a combined connect duration, label it that way rather than attributing it to DNS, TCP, TLS, or authentication. Also verify that each worker creates its pool after fork if Gunicorn preload is enabled.
    3. Database: If this is Azure Database for PostgreSQL Flexible Server, enable server logs before the test and correlate connection events with client attempts using UTC and a non-sensitive application_name. Capture the first successful checkout and the first completed unit of work after recovery. Those are better recovery markers than the first HTTP 200.

    For a support follow-up, send one failed recycle and one healthy control with the same image and worker settings. The key question is whether new physical connections were still failing when the slots became unavailable, or whether connections could already succeed while workers were still waiting for a slot. That distinction gives you a much better lead than another B1/B2 comparison.

    References:


    Help make this community better for everyone: If this answer helped or resolved your issue, please accept it or upvote it. If not, share more details in a comment so we can continue the discussion and find the right solution. Thank you.

    Was this answer helpful?


  2. Sedat SALMAN 14,545 Reputation points MVP
    2026-12-03T10:16:06.7566667+00:00

    Hi Ben,

    With default settings, the platform probe passing before your workers can use the database is expected. On Linux App Service the startup ping goes to WEBSITE_WARMUP_PATH (default /robots933456.txt), and unless WEBSITE_WARMUP_STATUSES is set, any response counts as ready, even a 404 or 502. At that point organic traffic moves to the new container, so "ready" only means the port answered HTTP.

    What I would change:

    1. Add a readiness endpoint that does what your own check does (check out a pooled connection, run a trivial query, return it), then set WEBSITE_WARMUP_PATH to that path and WEBSITE_WARMUP_STATUSES to 200. The platform keeps pinging until it gets a 200 before it routes traffic. If that can take a while on B1, raise WEBSITES_CONTAINER_START_TIME_LIMIT (default 230 seconds, max 1800); a container that never returns an allowed status is failed and retried, and you'd see 503s. With several Gunicorn workers one 200 only proves one worker, so I'd also open and test the pool in the app's startup hook so a worker doesn't accept requests before its pool works.
    2. Make settings changes on a deployment slot instead of on the serving container. Change the setting on a staging slot as a normal (non-sticky) setting, then swap. During a swap App Service applies the production slot's slot-specific settings to the staging instances, waits for them to restart and warm up, and only then switches routing, so production stays online throughout. Point WEBSITE_SWAP_WARMUP_PING_PATH at the same readiness endpoint and set WEBSITE_SWAP_WARMUP_PING_STATUSES to 200, and the swap stops if the new instances aren't ready. Slots need Standard, Premium or Isolated, so this means moving off Basic.
    3. A custom container with the dependencies baked in is supported and takes the Oryx artifact out of startup. That shortens the window but doesn't close the readiness gap, so I'd do 1 first.

    For diagnostics you can have in place before the next recycle:

    • Send AppServicePlatformLogs (container operation logs) and AppServiceConsoleLogs to a Log Analytics workspace, and log your pool events (connect start and end, checkout wait, checkout timeout) to stdout with timestamp, PID and container hostname. Then you can line your events up against the platform's container start and stop entries.
    • On Flexible Server, log_connections and log_disconnections are on by default. Add a diagnostic setting that sends PostgreSQLLogs to the same workspace. If the app logs a connect timeout and the server never logged "connection received" from the app's outbound address, the time was lost before the server (DNS, routing, SNAT). If the connection was received but never authorized, look at TLS or authentication.
    • If the database is reached over a public endpoint, check Diagnose and solve problems > Availability and Performance > SNAT Port Exhaustion for the same windows.

    References:

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.