{"id":"90d58a83dacef03e","repo":"mongodb/node-mongodb-native","slug":"unable-to-get-response-from-server","errorCode":null,"errorMessage":"Unable to get response from server","messagePattern":"Unable to get response from server","errorType":"exception","errorClass":"MongoUnexpectedServerResponseError","httpStatus":null,"severity":"error","filePath":"src/cmap/connection.ts","lineNumber":661,"sourceCode":"          }\n        } else {\n          if (\n            (Array.isArray(document?.writeErrors) &&\n              document.writeErrors.some(\n                error => error?.code === MONGODB_ERROR_CODES.MaxTimeMSExpired\n              )) ||\n            document?.writeConcernError?.code === MONGODB_ERROR_CODES.MaxTimeMSExpired\n          ) {\n            throw new MongoOperationTimeoutError('Server reported a timeout error', {\n              cause: new MongoServerError(document)\n            });\n          }\n        }\n      }\n\n      return document;\n    }\n    throw new MongoUnexpectedServerResponseError('Unable to get response from server');\n  }\n\n  public exhaustCommand(\n    ns: MongoDBNamespace,\n    command: Document,\n    options: CommandOptions,\n    replyListener: Callback\n  ) {\n    const exhaustLoop = async () => {\n      this.throwIfAborted();\n      for await (const reply of this.sendCommand(ns, command, options)) {\n        replyListener(undefined, reply);\n        this.throwIfAborted();\n      }\n      throw new MongoUnexpectedServerResponseError('Server ended moreToCome unexpectedly');\n    };\n\n    exhaustLoop().then(undefined, replyListener);","sourceCodeStart":643,"sourceCodeEnd":679,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/cmap/connection.ts#L643-L679","documentation":"Thrown at the end of Connection.command() (src/cmap/connection.ts:661) when the async generator returned by sendCommand completes without yielding any document. The command() method expects at least one response frame; if the server closed the stream or returned nothing, the loop body never executed and this MongoUnexpectedServerResponseError fires as a safety net against returning undefined.","triggerScenarios":"The server or network closed the socket between sending the command and reading the reply, but in a way that did not surface as an explicit error - the read stream simply ended cleanly with zero frames. Internal to command() at src/cmap/connection.ts:636-661.","commonSituations":"Connection killed by an idle-timeout LB (Azure, AWS NLB) between write and read; server process restarted mid-command; TLS handshake completed but the server sent RST without payload; proxy that returns empty on upstream close; race between pool pruning and command dispatch.","solutions":["Enable retryable reads/writes (retryWrites=true, retryReads=true - default on) so transient single-connection failures are retried on a fresh connection.","Raise maxIdleTimeMS or configure keepalive so LBs do not drop idle connections.","Check server logs for restarts or crashes around the error time.","Reduce connection pool idle time or use minPoolSize to keep connections warm."],"exampleFix":"// before\nnew MongoClient(uri, { retryWrites: false, maxIdleTimeMS: 30 });\n\n// after\nnew MongoClient(uri, { retryWrites: true, maxIdleTimeMS: 60000, minPoolSize: 1 });","handlingStrategy":"retry","validationCode":"function poolWarmOptions() {\n  return { retryWrites: true, retryReads: true, minPoolSize: 1, maxIdleTimeMS: 60000 };\n}","typeGuard":"import { MongoUnexpectedServerResponseError } from 'mongodb';\nfunction isEmptyStreamError(e: unknown): e is MongoUnexpectedServerResponseError {\n  return e instanceof MongoUnexpectedServerResponseError &&\n    /Unable to get response/.test(e.message);\n}","tryCatchPattern":"import { MongoUnexpectedServerResponseError } from 'mongodb';\nasync function resilientFind(coll: any, q: any, attempts = 3) {\n  for (let i = 0; i < attempts; i++) {\n    try { return await coll.findOne(q); }\n    catch (e) {\n      if (e instanceof MongoUnexpectedServerResponseError && i < attempts - 1) continue;\n      throw e;\n    }\n  }\n}","preventionTips":["Keep retryReads/retryWrites enabled (the default).","Set minPoolSize >= 1 and a sensible maxIdleTimeMS to avoid LB idle drops.","Enable keepalive (default) so idle connections are probed before use."],"tags":["network","connection","unexpected-response","retryable"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}