Skip to content

[gateway] Restart, shutdown and outage handling gaps #768

Open

Description

@guanzhousongmicrosoft

These gaps show up when PostgreSQL or the gateway stops, restarts or fails to start. Each one is small on its own; together they make restarts noisier and riskier than they need to be.

Tested on release/1.0-RC2-v2 @ 40f86c0: the documentdb-local PG18 image (standalone gateway), and a source build running the gateway as a background worker (start_oss_server.sh -g).

Items

  • A PostgreSQL outage during the dynamic-config refresh resets every setting to its built-in default
  • The standalone gateway doesn't drain connections on SIGTERM
  • gw_host: a panic while loading config leaves a worker with no listener
  • Smart shutdown never completes while the gateway is connected; writes get InternalError meanwhile
  • Legacy opcodes and oversized pre-auth messages return InternalError, or no reply on the v1 runtime
  • Every client disconnect is logged as ERROR ... InternalError

1. Config refresh during an outage resets settings to defaults

PgConfiguration::load_configurations turns a pg_settings failure into an empty list (configuration/pg_configuration.rs:67-87, Vec::new()), and the refresh stores it. Until the next refresh succeeds, every documentdb.* value falls back to its default, and so does max_connections.

Repro: set DynamicConfigurationRefreshIntervalSecs: 20 and restart PostgreSQL immediately so the restart overlaps a refresh. The gateway logs Failed to get connection for pg_settings and then Dynamic configurations loaded: {"IsPostgresInRecovery": "false", "SendShutdownResponses": "false", "IsPrimary": ""}. For about 19 s hello advertised maxWriteBatchSize: 100000 (the engine allows 25000), and a 30k insert_many failed with nothing inserted.

Fix: keep the last good configuration when the load fails. Related to #720 (a pool key flip on refresh), but a different failure.

2. SIGTERM skips the graceful drain

documentdb_gateway/src/main.rs:110 listens only for ctrl_c(). systemd and docker stop send SIGTERM.

  • On SIGTERM: the gateway exits 143 within about 2 s, an in-flight updateMany (200k docs) is aborted, and createIndexes returns AutoReconnect.
  • On SIGINT: it drains for about 11 s, and both operations complete.

Fix: handle SIGTERM the same way as SIGINT.

3. gw_host zombie worker

pg_documentdb_gw_host/src/bgworker.rs:97 and :113 call .expect() inside the spawned tokio task. A panic kills only the task, so SHUTDOWN_CONTROLLER.shutdown() is never reached and the worker stays in its latch loop.

  • Bad JSON, a missing setup file or a bad TLS cert path each left one worker process alive with no listener on 10260, and no worker exit in the PG log.
  • With documentdb_gateway.database unset, the worker exits with code 1 and is restarted every second (12 exits in 12 s).

Expected: log the error and exit once.

4. Smart shutdown

pg_ctl stop -m smart was still waiting 12 minutes later. The gateway's pooled connections keep it open.

During that time, writes failed with InternalError (code 1). The internal message was FATAL: the database system is shutting down, SQLSTATE 08006, coming from the engine's loopback connection (pg_documentdb/src/utils/query_utils.c, PGConnReportError). The gateway retried them as InternalError.

Expected: ShutdownInProgress (91), and either release the connections so smart shutdown can finish, or document that it can't.

5. Wire-protocol error paths

  • v2 runtime: OP_UPDATE, OP_DELETE, OP_GET_MORE, OP_KILL_CURSORS, OP_COMPRESSED and unknown opcodes return InternalError "An unexpected internal error has occurred." The cause is protocol/reader.rs, which builds internal_error("Unimplemented: ...").
  • v1 runtime: the same messages get no reply and the connection isn't closed, so the client hangs until its timeout.
  • A 300 KB pre-auth hello returns InternalError instead of a clear size error.
  • An OP_MSG document sequence with the wrong identifier is accepted (ok: 1).

Expected: CommandNotSupported / a clear error, and close the connection on an oversized pre-auth message.

6. Disconnects logged as errors

Every client disconnect logs ERROR documentdb_gateway_core::runtime::v2::listener: Gateway runtime connection failed: Request failed with kind Gateway, code InternalError, error_message_internal: Connection closed. That includes the healthcheck and readiness probes. It makes InternalError-based alerting useless. Log it at debug or info.

Activity

  1. added
    gateway
    documentdb-1.0
    on Oct 5, 2026
  2. self-assigned this
    on Oct 30, 2026
  3. documentdb-triage-tool commented on Nov 13, 2026

    @documentdb-triage-tool

    👋 @tsharp — this issue has been assigned for 14 days with no activity. Is it still in progress? If you've made progress, please leave a quick update. Otherwise it'll be flagged for re-assignment at the next triage meeting.

  4. guanzhousongmicrosoft commented on Nov 23, 2026

    @guanzhousongmicrosoft
    MemberAuthor

    Item 1 is fixed on main: a failed dynamic-configuration refresh now keeps the last good values instead of silently falling back to defaults (1739c69). The other items are still open.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugdocumentdb-1.0gateway

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions