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

Uncaught mongodb errors in try-catch when using deno #14237

Open
1 task done
SaTae66 opened this issue Jan 6, 2024 · 8 comments
Open
1 task done

Uncaught mongodb errors in try-catch when using deno #14237

SaTae66 opened this issue Jan 6, 2024 · 8 comments
Labels
underlying library issue This issue is a bug with an underlying library, like the MongoDB driver or mongodb-core

Comments

@SaTae66
Copy link

SaTae66 commented Jan 6, 2024

Prerequisites

  • I have written a descriptive issue title

Mongoose version

8.x.x

Node.js version

1.39.0 (deno)

MongoDB version

7.x

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

22.04

Issue

code:

const MONGO_URI = `mongodb://${MONGO_USER}:${MONGO_PWD}@${MONGO_URL}:${MONGO_PORT}`;

const client_options: mongoose.ConnectOptions = {
  dbName: MONGO_DB,
  authMechanism: AuthMechanism.MONGODB_SCRAM_SHA256,
  authSource: MONGO_AUTHSOURCE,
};

...

try {
  await mongoose.connect(MONGO_URI, client_options);
} catch (err) {
  Deno.exit(0);
}

...

Connecting and everything else works perfectly fine, but to see what happens on connection issues, I deliberately changed the IP to something else, expecting the program just to exit. Instead, I now get an:

error: Uncaught Error: connect ECONNREFUSED 192.168.0.9:27017 - Local (undefined:undefined)
    at __node_internal_captureLargerStackTrace (ext:deno_node/internal/errors.ts:91:9)
    at __node_internal_exceptionWithHostPort (ext:deno_node/internal/errors.ts:215:10)
    at TCPConnectWrap._afterConnect [as oncomplete] (node:net:170:16)
    at TCP.afterConnect (ext:deno_node/internal_binding/connection_wrap.ts:45:11)
    at ext:deno_node/internal_binding/tcp_wrap.ts:302:14
    at eventLoopTick (ext:core/01_core.js:181:11)

The error is expected due to the IP misconfiguration, but the program should not just die with an Uncaught Error but only exit silently instead. I am not very proficient in Typescript, but this seems not to be the expected behavior for this try-catch block. I would appreciate any help, thank you!

@SaTae66 SaTae66 added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary help wanted labels Jan 6, 2024
@vkarpov15
Copy link
Collaborator

I'm unable to repro, the following script demonstrates that the connection error is successfully caught:

import mongoose from 'npm:mongoose@8.x';

run().catch(err => console.log(err));

async function run() {
  try {
    await mongoose.connect('mongodb://192.168.0.9/test', { serverSelectionTimeoutMS: 1000 });
  } catch (err) {
    console.log('Caught', err.message);
    Deno.exit(0);
  }
}

Output:

$ deno run --allow-net --allow-read --allow-sys --allow-env gh-14237.js 
Caught Server selection timed out after 1000 ms
$ 

Can you please modify the provided script to demonstrate the issue you're seeing?

@vkarpov15 vkarpov15 added can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. and removed help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary help wanted labels Jan 6, 2024
@SaTae66
Copy link
Author

SaTae66 commented Jan 7, 2024

Those are my new findings regarding this:

Interestingly, adding the serverSelectionTimeoutMS option allows me to catch the MongooseServerSelectionError BUT only if the value is set to <=3000ms. Setting connectTimeoutMS to <=3000ms allows serverSelectionTimeoutMS to be bigger (I assume thats probably due to some reconnect logic before the server selection finally fails).

Also, the catchable error (< 3000ms) is a mongoose error (MongooseServerSelectionError), the UNcatchable error is a mongo error (ECONNREFUSED).

Finally, this issue might be caused by or at least related to denoland/deno#19078

@vkarpov15 vkarpov15 added has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue and removed can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. labels Jan 7, 2024
@vkarpov15 vkarpov15 added this to the 8.0.4 milestone Jan 7, 2024
@vkarpov15
Copy link
Collaborator

I'm still not able to repro even without serverSelectionTimeoutMS, I get the following output:

$ deno run --allow-net --allow-read --allow-sys --allow-env gh-14237.js 
Caught connection timed out

Switching connectTimeoutMS to be greater than serverSelectionTimeoutMS changes the error message to "Server selection timed out after 5000 ms", but that error is still caught.

Do you still get this uncaught error when running the exact script below?

import mongoose from 'npm:mongoose@8.x';

run().catch(err => console.log(err));

async function run() {
  try {
    await mongoose.connect('mongodb://192.168.0.9/test', { });
  } catch (err) {
    console.log('Caught', err.message);
    Deno.exit(0);
  }
}

@vkarpov15 vkarpov15 added can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Jan 8, 2024
@vkarpov15 vkarpov15 removed this from the 8.0.4 milestone Jan 8, 2024
@SaTae66
Copy link
Author

SaTae66 commented Jan 10, 2024

Yes, this exact script also results in the uncaught exception:

error: Uncaught Error: connect ECONNREFUSED 192.168.0.9:27017 - Local (undefined:undefined)
    at __node_internal_captureLargerStackTrace (ext:deno_node/internal/errors.ts:91:9)
    at __node_internal_exceptionWithHostPort (ext:deno_node/internal/errors.ts:215:10)
    at TCPConnectWrap._afterConnect [as oncomplete] (node:net:170:16)
    at TCP.afterConnect (ext:deno_node/internal_binding/connection_wrap.ts:45:11)
    at ext:deno_node/internal_binding/tcp_wrap.ts:302:14
    at eventLoopTick (ext:core/01_core.js:182:7)

I also just freshly installed deno in a Gentoo VM, in which I didn't do any prior stuff with deno/node and the exact same thing happens.

@vkarpov15
Copy link
Collaborator

What's the IP misconfiguration that caused this issue? Maybe I can try to replicate that.

@SaTae66
Copy link
Author

SaTae66 commented Jan 15, 2024

Two options:

  1. I give it a wrong subnet, the error MongooseServerSelectionError: connection timed out is caught
  2. I specify the correct net but use a non existing host, it results in the uncaught error: Uncaught Error: connect ECONNREFUSED 192.168.0.9:27017 - Local (undefined:undefined).

It might be totally unrelated but for 1., according to wireshark, it retries the TCP request multiple time with an exponential backoff until it finally fails after not receiving ANY answere. For 2., after a couple of seconds, an ICMP Host unreachable is sent back by the gateway and it fails.

Here are the 2 stacktraces:

  1. (shortened path for better overview):
 at connectionFailureError (file://.../mongodb/6.2.0/lib/cmap/connect.js:381:20)
 at Socket.<anonymous> (file://.../mongodb/6.2.0/lib/cmap/connect.js:285:22)
 at Object.onceWrapper (ext:deno_node/_stream.mjs:1929:32)
 at Socket.emit (ext:deno_node/_stream.mjs:1854:9)
 at Socket._onTimeout (node:net:848:10)
 at cb (ext:deno_node/internal/timers.mjs:63:31)
 at Object.action (ext:deno_web/02_timers.js:154:11)
 at handleTimerMacrotask (ext:deno_web/02_timers.js:68:10)
 at eventLoopTick (ext:core/01_core.js:189:21)

2.:

at __node_internal_captureLargerStackTrace (ext:deno_node/internal/errors.ts:91:9)
at __node_internal_exceptionWithHostPort (ext:deno_node/internal/errors.ts:215:10)
at TCPConnectWrap._afterConnect [as oncomplete] (node:net:170:16)
at TCP.afterConnect (ext:deno_node/internal_binding/connection_wrap.ts:43:11)
at ext:deno_node/internal_binding/tcp_wrap.ts:302:14
at eventLoopTick (ext:core/01_core.js:182:7)

@vkarpov15
Copy link
Collaborator

I was able to repro using 127.0.0.1:27018: correct IP, wrong port. I'll do some debugging to see if there's any way to catch this error.

The stack trace from (2) looks like it might be an internal Deno issue: if the stack trace doesn't include anything Mongoose or MongoDB related, just Deno internals, that means there's no way for us to handle this error. Hopefully I can find a way.

@vkarpov15
Copy link
Collaborator

Looks like I found the root cause, I opened an issue with Deno here with the details: denoland/deno#21951. It looks like a Deno issue, we'll see what the Deno team says.

@vkarpov15 vkarpov15 added underlying library issue This issue is a bug with an underlying library, like the MongoDB driver or mongodb-core and removed can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. labels Jan 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
underlying library issue This issue is a bug with an underlying library, like the MongoDB driver or mongodb-core
Projects
None yet
Development

No branches or pull requests

2 participants