Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MongoDB connection loss during request in progress causing unhandled exception to crash node. #9355

Closed
ghost opened this issue Aug 24, 2020 · 5 comments
Milestone

Comments

@ghost
Copy link

ghost commented Aug 24, 2020

Do you want to request a feature or report a bug?
Bug

If the current behavior is a bug, please provide the steps to reproduce.

  1. start node with mongoose to connect to mongodb successfully.
  2. break the connection (e.g. turn off internet to simulate mongodb connection loss)
  3. before mongoose "disconnected" event comes through (based on timeout set), execute a request via model (e.g. model. instertMany)
  4. after some time, node process will crash with the exception in mongoose which is impossible to be catched:
/Users/mdabychy/WorkLabs/automation_toolkit_js/mongoservice/node_modules/mongodb/lib/utils.js:106
      throw err;
      ^

TypeError: Cannot read property 'map' of undefined
    at /Users/mdabychy/WorkLabs/automation_toolkit_js/mongoservice/node_modules/mongoose/lib/model.js:3320:58
    at executeCallback (/Users/mdabychy/WorkLabs/automation_toolkit_js/mongoservice/node_modules/mongodb/lib/operations/execute_operation.js:70:5)
    at /Users/mdabychy/WorkLabs/automation_toolkit_js/mongoservice/node_modules/mongodb/lib/operations/insert_many.js:42:23
    at /Users/mdabychy/WorkLabs/automation_toolkit_js/mongoservice/node_modules/mongodb/lib/operations/bulk_write.js:70:16
    at /Users/mdabychy/WorkLabs/automation_toolkit_js/mongoservice/node_modules/mongodb/lib/utils.js:385:14
    at executeCallback (/Users/mdabychy/WorkLabs/automation_toolkit_js/mongoservice/node_modules/mongodb/lib/utils.js:375:25)
    at handleCallback (/Users/mdabychy/WorkLabs/automation_toolkit_js/mongoservice/node_modules/mongodb/lib/utils.js:102:55)
    at resultHandler (/Users/mdabychy/WorkLabs/automation_toolkit_js/mongoservice/node_modules/mongodb/lib/bulk/common.js:505:14)
    at handler (/Users/mdabychy/WorkLabs/automation_toolkit_js/mongoservice/node_modules/mongodb/lib/core/sdam/topology.js:945:16)
    at /Users/mdabychy/WorkLabs/automation_toolkit_js/mongoservice/node_modules/mongodb/lib/cmap/connection_pool.js:354:13
    at Object.handleOperationResult [as cb] (/Users/mdabychy/WorkLabs/automation_toolkit_js/mongoservice/node_modules/mongodb/lib/core/sdam/server.js:558:5)
    at /Users/mdabychy/WorkLabs/automation_toolkit_js/mongoservice/node_modules/mongodb/lib/cmap/connection.js:66:12
    at Map.forEach (<anonymous>)
    at Socket.<anonymous> (/Users/mdabychy/WorkLabs/automation_toolkit_js/mongoservice/node_modules/mongodb/lib/cmap/connection.js:65:20)
    at Socket.emit (events.js:314:20)
    at TCP.<anonymous> (net.js:673:12)
[nodemon] app crashed - waiting for file changes before starting...

What is the expected behavior?
The behavior I expect is to be able to at least catch all the possible errors to avoid crashing the whole process.
Also, connection timeouts should be handled within the library itself.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
node 14,
"mongodb": "^3.6.0",
"mongoose": "^5.10.0",

@ghost
Copy link
Author

ghost commented Aug 24, 2020

Just re-tested, something like findOne works fine, the problem is with insertMany in my case. Not sure about other "*Many" methods

@ghost
Copy link
Author

ghost commented Aug 24, 2020

I've made a deeper investigation and it seems the problem is in insertMany method at the line 3320:
const erroredIndexes = new Set(error.writeErrors.map(err => err.index));

It seems it is not considered that there can be errors other than writeErrors and therefore if there is an error not related to writing (e.g. connection timeout error) - it just crashes at the line pasted above as it can't .map over undefined.

in node14, adding optional chaining like this:
const erroredIndexes = new Set(error?.writeErrors?.map(err => err.index));
resolves the problem.

Please fix it in the lib.

@vkarpov15 vkarpov15 added this to the 5.10.2 milestone Aug 27, 2020
@Nate97320
Copy link

connection error: MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
2021-12-17T12:46:50.705374+00:00 app[web.1]: at NativeConnection.Connection.openUri (/app/node_modules/mongoose/lib/connection.js:797:32)
2021-12-17T12:46:50.705375+00:00 app[web.1]: at /app/node_modules/mongoose/lib/index.js:332:10
2021-12-17T12:46:50.705375+00:00 app[web.1]: at /app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
2021-12-17T12:46:50.705376+00:00 app[web.1]: at new Promise ()
2021-12-17T12:46:50.705376+00:00 app[web.1]: at promiseOrCallback (/app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
2021-12-17T12:46:50.705376+00:00 app[web.1]: at Mongoose._promiseOrCallback (/app/node_modules/mongoose/lib/index.js:1158:10)
2021-12-17T12:46:50.705377+00:00 app[web.1]: at Mongoose.connect (/app/node_modules/mongoose/lib/index.js:331:20)
2021-12-17T12:46:50.705377+00:00 app[web.1]: at Object. (/app/app.js:34:10)
2021-12-17T12:46:50.705377+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:1085:14)
2021-12-17T12:46:50.705378+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10) {
2021-12-17T12:46:50.705378+00:00 app[web.1]: reason: TopologyDescription {
2021-12-17T12:46:50.705379+00:00 app[web.1]: type: 'Unknown',
2021-12-17T12:46:50.705379+00:00 app[web.1]: servers: Map(1) { 'localhost:27017' => [ServerDescription] },
2021-12-17T12:46:50.705379+00:00 app[web.1]: stale: false,
2021-12-17T12:46:50.705380+00:00 app[web.1]: compatible: true,
2021-12-17T12:46:50.705380+00:00 app[web.1]: heartbeatFrequencyMS: 10000,
2021-12-17T12:46:50.705380+00:00 app[web.1]: localThresholdMS: 15,
2021-12-17T12:46:50.705380+00:00 app[web.1]: logicalSessionTimeoutMinutes: undefined
2021-12-17T12:46:50.705381+00:00 app[web.1]: }
2021-12-17T12:46:50.705381+00:00 app[web.1]: }
2021-12-17T12:46:50.706357+00:00 app[web.1]: (node:35) UnhandledPromiseRejectionWarning: MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
2021-12-17T12:46:50.706357+00:00 app[web.1]: at NativeConnection.Connection.openUri (/app/node_modules/mongoose/lib/connection.js:797:32)
2021-12-17T12:46:50.706358+00:00 app[web.1]: at /app/node_modules/mongoose/lib/index.js:332:10
2021-12-17T12:46:50.706358+00:00 app[web.1]: at /app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
2021-12-17T12:46:50.706359+00:00 app[web.1]: at new Promise ()
2021-12-17T12:46:50.706360+00:00 app[web.1]: at promiseOrCallback (/app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
2021-12-17T12:46:50.706360+00:00 app[web.1]: at Mongoose._promiseOrCallback (/app/node_modules/mongoose/lib/index.js:1158:10)
2021-12-17T12:46:50.706360+00:00 app[web.1]: at Mongoose.connect (/app/node_modules/mongoose/lib/index.js:331:20)
2021-12-17T12:46:50.706361+00:00 app[web.1]: at Object. (/app/app.js:34:10)
2021-12-17T12:46:50.706361+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:1085:14)
2021-12-17T12:46:50.706361+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
2021-12-17T12:46:50.706362+00:00 app[web.1]: (Use node --trace-warnings ... to show where the warning was created)
2021-12-17T12:46:50.706580+00:00 app[web.1]: (node:35) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
2021-12-17T12:46:50.711118+00:00 app[web.1]: (node:35) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
2021-12-17T12:46:50.739589+00:00 app[web.1]: /app/node_modules/connect-mongo/node_modules/mongodb/lib/utils.js:698
2021-12-17T12:46:50.739590+00:00 app[web.1]: throw error;
2021-12-17T12:46:50.739591+00:00 app[web.1]: ^
2021-12-17T12:46:50.739591+00:00 app[web.1]:
2021-12-17T12:46:50.739592+00:00 app[web.1]: MongoServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
2021-12-17T12:46:50.739592+00:00 app[web.1]: at Timeout._onTimeout (/app/node_modules/connect-mongo/node_modules/mongodb/lib/core/sdam/topology.js:438:30)
2021-12-17T12:46:50.739593+00:00 app[web.1]: at listOnTimeout (internal/timers.js:557:17)
2021-12-17T12:46:50.739593+00:00 app[web.1]: at processTimers (internal/timers.js:500:7) {
2021-12-17T12:46:50.739594+00:00 app[web.1]: reason: TopologyDescription {
2021-12-17T12:46:50.739594+00:00 app[web.1]: type: 'Single',
2021-12-17T12:46:50.739594+00:00 app[web.1]: setName: null,
2021-12-17T12:46:50.739595+00:00 app[web.1]: maxSetVersion: null,
2021-12-17T12:46:50.739595+00:00 app[web.1]: maxElectionId: null,
2021-12-17T12:46:50.739595+00:00 app[web.1]: servers: Map(1) {
2021-12-17T12:46:50.739596+00:00 app[web.1]: 'localhost:27017' => ServerDescription {
2021-12-17T12:46:50.739596+00:00 app[web.1]: address: 'localhost:27017',
2021-12-17T12:46:50.739597+00:00 app[web.1]: error: Error: connect ECONNREFUSED 127.0.0.1:27017
2021-12-17T12:46:50.739597+00:00 app[web.1]: at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1159:16) {
2021-12-17T12:46:50.739597+00:00 app[web.1]: name: 'MongoNetworkError'
2021-12-17T12:46:50.739597+00:00 app[web.1]: },
2021-12-17T12:46:50.739598+00:00 app[web.1]: roundTripTime: -1,
2021-12-17T12:46:50.739598+00:00 app[web.1]: lastUpdateTime: 912464405,
2021-12-17T12:46:50.739598+00:00 app[web.1]: lastWriteDate: null,
2021-12-17T12:46:50.739599+00:00 app[web.1]: opTime: null,
2021-12-17T12:46:50.739599+00:00 app[web.1]: type: 'Unknown',
2021-12-17T12:46:50.739599+00:00 app[web.1]: topologyVersion: undefined,
2021-12-17T12:46:50.739599+00:00 app[web.1]: minWireVersion: 0,
2021-12-17T12:46:50.739600+00:00 app[web.1]: maxWireVersion: 0,
2021-12-17T12:46:50.739600+00:00 app[web.1]: hosts: [],
2021-12-17T12:46:50.739600+00:00 app[web.1]: passives: [],
2021-12-17T12:46:50.739600+00:00 app[web.1]: arbiters: [],
2021-12-17T12:46:50.739601+00:00 app[web.1]: tags: []
2021-12-17T12:46:50.739601+00:00 app[web.1]: }
2021-12-17T12:46:50.739601+00:00 app[web.1]: },
2021-12-17T12:46:50.739601+00:00 app[web.1]: stale: false,
2021-12-17T12:46:50.739602+00:00 app[web.1]: compatible: true,
2021-12-17T12:46:50.739602+00:00 app[web.1]: compatibilityError: null,
2021-12-17T12:46:50.739602+00:00 app[web.1]: logicalSessionTimeoutMinutes: null,
2021-12-17T12:46:50.739602+00:00 app[web.1]: heartbeatFrequencyMS: 10000,
2021-12-17T12:46:50.739603+00:00 app[web.1]: localThresholdMS: 15,
2021-12-17T12:46:50.739603+00:00 app[web.1]: commonWireVersion: null
�[31m[nodemon] app crashed - waiting for file changes before starting...�[39m

@Nate97320
Copy link

im having this error its been a week, i cant find a solution

@vkarpov15
Copy link
Collaborator

@Nate97320 please open a new issue and follow the issue template.

@Automattic Automattic locked and limited conversation to collaborators Dec 23, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants