denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
94.57k stars 5.25k forks source link

Connection timed out when using Mongoose|MongoDB with deno cli #22730

Open Massayu opened 7 months ago

Massayu commented 7 months ago

deno-x86_64-pc-windows-msvc v1.41.1

Testing the following code using nodejs and on deno deploy works correctly:

import express    from 'express';
import mongoose   from 'mongoose';

const app = express();
await mongoose.connect("mongodb+srv://...?retryWrites=true&w=majority");
const db  = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
    console.log('Connected to MongoDB');
});

app.listen(3001, () => { console.log('starting...'); });

But when testing using deno cli it doesn't work.

I get the message on the console starting... but not the connection error: neither Connected to MongoDB

And after some time I get this error:

error: Uncaught (in promise) MongooseServerSelectionError: connection timed out
    at _handleConnectionErrors (file:///.../node_modules/.deno/mongoose@8.2.1/node_modules/mongoose/lib/connection.js:875:11)

The same thing happens using mongodb instead of mongoose:

import express from 'express';
import mongodb from 'mongodb';

const app    = express();
const client = new mongodb.MongoClient("mongodb+srv://...mongodb.net/...?retryWrites=true&w=majority")
await client.connect();
const db     = client.db();

app.listen(3001, () => { console.log('starting...'); });
C:\Windows\system32>deno run --allow-all ...\index.js
Warning: Not implemented: ClientRequest.options.createConnection
error: Uncaught (in promise) MongoServerSelectionError: connection timed out
    at AbortSignal.<anonymous> (file:///...node_modules/.deno/mongodb@6.4.0/node_modules/mongodb/lib/sdam/topology.js:292:34)

I'm starting the script as

deno run --allow-all ...\index.js

I also tested running it as admin, on the Mongo website I have all ip whitelisted Why do I get this error only on the CLI version?

Massayu commented 6 months ago

On my tests above i had the npm imports on deno.json now i tried with npm:

import { MongoClient } from "npm:mongodb@6.1.0";

const client = new MongoClient("mongodb+srv://...");
await client.connect();
console.log("Connected successfully to server");
C:\Users\Documents\NodejsConsoleApp1>deno run --allow-all app.js
error: Uncaught (in promise) MongoServerSelectionError: connection timed out
    at Timeout._onTimeout (file:///C:/Users/Documents/NodejsConsoleApp1/node_modules/.deno/mongodb@6.1.0/node_modules/mongodb/lib/sdam/topology.js:278:38)
    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:173:21)
Caused by: MongoNetworkTimeoutError: connection timed out
    at connectionFailureError (file:///C:/Users/Documents/NodejsConsoleApp1/node_modules/.deno/mongodb@6.1.0/node_modules/mongodb/lib/cmap/connect.js:381:20)
    at TLSSocket.<anonymous> (file:///C:/Users/Documents/NodejsConsoleApp1/node_modules/.deno/mongodb@6.1.0/node_modules/mongodb/lib/cmap/connect.js:285:22)
    at Object.onceWrapper (ext:deno_node/_stream.mjs:1929:32)
    at TLSSocket.emit (ext:deno_node/_stream.mjs:1854:9)
    at TLSSocket._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:173:21)
Massayu commented 6 months ago

@littledivy

jantoney commented 3 months ago

I've come across this issue and think the root cause is only when it's run on Windows. I didn't see this issue, wrote my own, then did 1 more search before posting and found this. So I'll just paste my issue here.

@Massayu - It might be worth updating the title to something like this for clarity, assuming you agree with my outcome Unable to connect to mongodb Atlas server using mongoose & mongodb+srv:// record on windows

Issue

When trying to connect to MongoDB Atlas using mongoose driver and a mongodb+srv:// record, the connection returns MongooseServerSelectionError: connection timed out

Quick Findings

The issue seems to only occur

A related issue raised on the mongoose GitHub (linked further below) raises a question that it seems to be only for some WiFi network configs. I've tested this on a dektop PC hardwired & on a laptop with WiFI. Both have the same issue.


This for others to test/confirm

I have Windows Sandbox installed on both of my machines. That's the only think I can think of that could be messing with a network connection (massive stretch though). So can someone without it check it's still an issue.


What I've tried - DENO (no work around found)

Versions

deno 1.44.4 (release, x86_64-pc-windows-msvc) ...Ubuntu Version: deno 1.44.4 (release, x86_64-unknown-linux-gnu) v8 12.6.228.9 typescript 5.4.5 "mongoose": "npm:mongoose@8.4.4" Windows 11 v23H2 (OS Build 22631.3737)


Related issues

This seems to be related to an issue on the mongoose GitHub page, but was closed as 'underlying library issue' with a recommendation to upgrade to mongoose v8, however that doesn't seem to have helped. https://github.com/Automattic/mongoose/issues/13988

Original issue seems to only happen on Windows, only on Deno, only with Atlas, only on certain WiFi, and only on Mongoose 7. I'm able to repro with Mongoose 7.6.3 on @IslandRhythms 's machine, but the issue comes down to the fact that tls.connect() hangs here. The socket never emits any events. There isn't much more we can do to fix this particular error, other than recommend upgrading to Mongoose 8, which seems to be unaffected.


The error from the console when running from VS Code

connectionString mongodb+srv://<REMOVED_USER>:<REMOVED_PASS>@cluster0.<REMOVED>.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0
error: Uncaught (in promise) MongooseServerSelectionError: connection timed out
    at _handleConnectionErrors (file:///C:/Users/jayan/AppData/Local/deno/npm/registry.npmjs.org/mongoose/8.4.3/lib/connection.js:897:11)
    at NativeConnection.openUri (file:///C:/Users/jayan/AppData/Local/deno/npm/registry.npmjs.org/mongoose/8.4.3/lib/connection.js:848:11)
    at eventLoopTick (ext:core/01_core.js:207:9)
    at async DB.connect (file:///D:/DevWork/<REMOVED_MY_APP_NAME>/config/db.ts:62:5)
    at async file:///D:/DevWork/<REMOVED_MY_APP_NAME>/config/db.ts:79:1
Watcher Process failed. Restarting on file change...

Error when running VSCode debugger in Edge

(To be honest, I've never done any debugging like this and thought if I tried I might be able to find something. I was wrong - I have no idea, so ignore if this doesn't help)

MongoNetworkTimeoutError: connection timed out
    at connectionFailureError (file:///C:/Users/jayan/AppData/Local/deno/npm/registry.npmjs.org/mongodb/6.6.2/lib/cmap/connect.js:355:20)
    at TLSSocket.<anonymous> (file:///C:/Users/jayan/AppData/Local/deno/npm/registry.npmjs.org/mongodb/6.6.2/lib/cmap/connect.js:269:43)
    at Object.onceWrapper (ext:deno_node/_stream.mjs:1926:32)
    at TLSSocket.emit (ext:deno_node/_stream.mjs:1851:9)
    at TLSSocket._onTimeout (node:net:849:10)
    at cb (ext:deno_node/internal/timers.mjs:64:31)
    at eventLoopTick (ext:core/01_core.js:203:13)