mongodb/node-mongodb-native · error · MongoUnexpectedServerResponseError

Unable to get response from server

Error message

Unable to get response from server

What it means

A MongoUnexpectedServerResponseError thrown at the end of Connection.command when the sendCommand async generator completes without yielding a single document. Normally the server always replies with at least one document; reaching the 'no response' throw means the protocol exchange ended empty, which should never happen on a healthy connection.

Solutions

  1. Retry the operation with retryable reads/writes enabled (the default).
  2. Investigate intermediate proxies/load balancers for premature connection closes.
  3. Upgrade the driver — older versions had generator-control paths that could surface this spuriously.
  4. Enable driver SDAM/command logging to correlate with connection close events.
Defensive patterns

Strategy: retry

Type guard

function isUnexpectedServerResponse(e: unknown): boolean {
  return e?.constructor?.name === 'MongoUnexpectedServerResponseError';
}

Try / catch

for (let attempt = 0; attempt < 3; attempt++) {
  try {
    return await operation();
  } catch (e) {
    if (e?.constructor?.name === 'MongoUnexpectedServerResponseError' && attempt < 2) continue;
    throw e;
  }
}

Prevention

When it happens

Trigger: The sendCommand generator returns before yielding — e.g., the socket closed cleanly between write and read, or an internal generator control-flow path exited early. Typically a symptom of a dropped/reset connection or a proxy that half-closes the stream.

Common situations: Network middleboxes or load balancers that close idle connections without RST; TLS termination proxies with aggressive timeouts; driver bugs in older versions; connections reaped by the pool between write and read.

Related errors


AI-assisted analysis of mongodb/node-mongodb-native@dce7939f86 (2026-08-11). Data as JSON: /api/errors/90d58a83dacef03e. Report an issue: GitHub.

Appendix: source

Thrown at src/cmap/connection.ts:661

          }
        } else {
          if (
            (Array.isArray(document?.writeErrors) &&
              document.writeErrors.some(
                error => error?.code === MONGODB_ERROR_CODES.MaxTimeMSExpired
              )) ||
            document?.writeConcernError?.code === MONGODB_ERROR_CODES.MaxTimeMSExpired
          ) {
            throw new MongoOperationTimeoutError('Server reported a timeout error', {
              cause: new MongoServerError(document)
            });
          }
        }
      }

      return document;
    }
    throw new MongoUnexpectedServerResponseError('Unable to get response from server');
  }

  public exhaustCommand(
    ns: MongoDBNamespace,
    command: Document,
    options: CommandOptions,
    replyListener: Callback
  ) {
    const exhaustLoop = async () => {
      this.throwIfAborted();
      for await (const reply of this.sendCommand(ns, command, options)) {
        replyListener(undefined, reply);
        this.throwIfAborted();
      }
      throw new MongoUnexpectedServerResponseError('Server ended moreToCome unexpectedly');
    };

    exhaustLoop().then(undefined, replyListener);

View on GitHub (pinned to dce7939f86)