R
Railway•14mo ago
BenIsenstein

MongoDB Error: "MongoServerClosedError: Server is closed"

Project id: 8b67ba4d-ba67-4f38-97ef-8caab4e245fe Hi folks, my express server has trouble connecting to my mongo service on startup. It was functioning with a mongodb plugin until a couple weeks ago. I thought maybe I'll try switching to a mongodb image-based service in case it magically starts working. But the exact same error. WOuld appreciate some insight, thank you!
Solution:
The test script was trying to connect and close at the same time
Jump to solution
55 Replies
Percy
Percy•14mo ago
Project ID: 8b67ba4d-ba67-4f38-97ef-8caab4e245fe
Brody
Brody•14mo ago
mongoose?
BenIsenstein
BenIsenstein•14mo ago
nope, nodejs mongo driver repo:
BenIsenstein
BenIsenstein•14mo ago
GitHub
GitHub - BenIsenstein/notion_integration_1
Contribute to BenIsenstein/notion_integration_1 development by creating an account on GitHub.
Brody
Brody•14mo ago
looking
BenIsenstein
BenIsenstein•14mo ago
Thanks Brody 🙂
Brody
Brody•14mo ago
BenIsenstein
BenIsenstein•14mo ago
SUre, it's a part of the Client options object?
Brody
Brody•14mo ago
looks to be, yeah
BenIsenstein
BenIsenstein•14mo ago
pushed to my repo, let's monitor the deployment
Brody
Brody•14mo ago
you tell me what happens, i cant monitor anything lol
BenIsenstein
BenIsenstein•14mo ago
lol I figured I was giving you implicit consent to observe my project infra
Brody
Brody•14mo ago
dont work for railway so i cant see anything
BenIsenstein
BenIsenstein•14mo ago
Oh I thought you worked for them! What's your relation to them/how did you end up being a conductor?
Brody
Brody•14mo ago
i help so they gave me cool role
BenIsenstein
BenIsenstein•14mo ago
sweety
BenIsenstein
BenIsenstein•14mo ago
No description
BenIsenstein
BenIsenstein•14mo ago
alright still a failed health check after 10s
Brody
Brody•14mo ago
what do the deployment logs look like?
BenIsenstein
BenIsenstein•14mo ago
No description
Brody
Brody•14mo ago
that doesnt look like there was even a connection attempt?
BenIsenstein
BenIsenstein•14mo ago
So I'm trying to synchronously fetch the Google OAuth refresh tokens before the connection is established Kay let me dig to the. bottom
Brody
Brody•14mo ago
keep me updated
BenIsenstein
BenIsenstein•14mo ago
If you want to look in my repo the file is /repositories/mongo Some background I run many one-time mongo operations inside of a wrapper function called "withConnectAndClose()"
export const withConnectAndClose = async <
TDocument,
TReturn
>(
db: string,
collection: string,
callback: (col: Collection<TDocument>) => Promise<TReturn>
) => {
try {
let returnVal: TReturn

await mongoClient.connect()
returnVal = await callback(mongoClient.db(db).collection(collection))
await mongoClient.close()

return returnVal
} catch (error) {
await mongoClient.close()
throw new Error(error)
}
}
export const withConnectAndClose = async <
TDocument,
TReturn
>(
db: string,
collection: string,
callback: (col: Collection<TDocument>) => Promise<TReturn>
) => {
try {
let returnVal: TReturn

await mongoClient.connect()
returnVal = await callback(mongoClient.db(db).collection(collection))
await mongoClient.close()

return returnVal
} catch (error) {
await mongoClient.close()
throw new Error(error)
}
}
Brody
Brody•14mo ago
looks good to me
BenIsenstein
BenIsenstein•14mo ago
my entry file for the project starts by running a function "initGoogleApi()"
require("dotenv").config()
import { existsSync } from 'fs'
import createError from 'http-errors'
import express from 'express'
import cookieParser from 'cookie-parser'
import logger from 'morgan'
import apiRouter from './routes/apiRouter'
import { startJobs } from './cron'

require('./helpers').initGoogleApi().then(() => {
const app = express()
...
})
require("dotenv").config()
import { existsSync } from 'fs'
import createError from 'http-errors'
import express from 'express'
import cookieParser from 'cookie-parser'
import logger from 'morgan'
import apiRouter from './routes/apiRouter'
import { startJobs } from './cron'

require('./helpers').initGoogleApi().then(() => {
const app = express()
...
})
Brody
Brody•14mo ago
little tip, wrap your code in triple back ticks
BenIsenstein
BenIsenstein•14mo ago
That initGoogleApi() function is warpped with withConnectAndClose()
import { writeFileSync } from 'fs'
import { oauth2Client, google } from '../repositories'
import { getGoogleTokens } from '../services'
import { USER_ID } from '../values'

export const initGoogleApi = async () => {
oauth2Client.setCredentials(await getGoogleTokens(USER_ID))
google.options({
auth: oauth2Client
})
writeFileSync(
`${__dirname}/../credentials/articles-service-account-credentials.json`,
process.env.ARTICLES_SERVICE_ACCOUNT_CREDENTIALS
)
writeFileSync(
`${__dirname}/../credentials/cloud-tasks-client-account-credentials.json`,
process.env.CLOUD_TASKS_CLIENT_ACCOUNT_CREDENTIALS
)
}
import { writeFileSync } from 'fs'
import { oauth2Client, google } from '../repositories'
import { getGoogleTokens } from '../services'
import { USER_ID } from '../values'

export const initGoogleApi = async () => {
oauth2Client.setCredentials(await getGoogleTokens(USER_ID))
google.options({
auth: oauth2Client
})
writeFileSync(
`${__dirname}/../credentials/articles-service-account-credentials.json`,
process.env.ARTICLES_SERVICE_ACCOUNT_CREDENTIALS
)
writeFileSync(
`${__dirname}/../credentials/cloud-tasks-client-account-credentials.json`,
process.env.CLOUD_TASKS_CLIENT_ACCOUNT_CREDENTIALS
)
}
Brody
Brody•14mo ago
all looks good to me, though keep in mind im not a js/ts dev
BenIsenstein
BenIsenstein•14mo ago
fair enough And this was working seamlessly for close to a year...
Brody
Brody•14mo ago
updated any deps recently? are you using private networking?
BenIsenstein
BenIsenstein•14mo ago
Private networking needs to be set correct? I haven't manually set it to private This exact issue was happening with the mongodb plugin before So not a networking issue I believe No deps updated
Brody
Brody•14mo ago
but are you actively utilizing the private networking?
BenIsenstein
BenIsenstein•14mo ago
Is private networking active by default? I just started this project from a template and haven't made any networking adjustements manually
Brody
Brody•14mo ago
its on by default, but im asking if you use it
BenIsenstein
BenIsenstein•14mo ago
Not actively using it
Brody
Brody•14mo ago
disable it
BenIsenstein
BenIsenstein•14mo ago
Sure most recent deploy logs:
npm WARN config production Use `--omit=dev` instead.


> rm -rf dist/credentials && mkdir dist/credentials


> node dist/index.js

No Google tokens found, sending email to reset them...

/app/dist/repositories/mongodb.repository.js:31

throw new Error(error);

^

Error: MongoNotConnectedError: Client must be connected before running operations

at /app/dist/repositories/mongodb.repository.js:31:15

at Generator.next (<anonymous>)

at fulfilled (/app/dist/repositories/mongodb.repository.js:5:58)

at processTicksAndRejections (node:internal/process/task_queues:96:5)

npm WARN config production Use `--omit=dev` instead.


> rm -rf dist/credentials && mkdir dist/credentials


> node dist/index.js

/app/dist/repositories/mongodb.repository.js:31

throw new Error(error);

^

Error: MongoNetworkError: connection establishment was cancelled

at /app/dist/repositories/mongodb.repository.js:31:15

at Generator.next (<anonymous>)

at fulfilled (/app/dist/repositories/mongodb.repository.js:5:58)

at processTicksAndRejections (node:internal/process/task_queues:96:5)

npm ERR! path /app

npm ERR! command failed

npm ERR! signal SIGTERM

npm ERR! command sh -c -- node dist/index.js

npm ERR! A complete log of this run can be found in:

npm ERR! /root/.npm/_logs/2023-09-28T22_15_05_618Z-debug-0.log
npm WARN config production Use `--omit=dev` instead.


> rm -rf dist/credentials && mkdir dist/credentials


> node dist/index.js

No Google tokens found, sending email to reset them...

/app/dist/repositories/mongodb.repository.js:31

throw new Error(error);

^

Error: MongoNotConnectedError: Client must be connected before running operations

at /app/dist/repositories/mongodb.repository.js:31:15

at Generator.next (<anonymous>)

at fulfilled (/app/dist/repositories/mongodb.repository.js:5:58)

at processTicksAndRejections (node:internal/process/task_queues:96:5)

npm WARN config production Use `--omit=dev` instead.


> rm -rf dist/credentials && mkdir dist/credentials


> node dist/index.js

/app/dist/repositories/mongodb.repository.js:31

throw new Error(error);

^

Error: MongoNetworkError: connection establishment was cancelled

at /app/dist/repositories/mongodb.repository.js:31:15

at Generator.next (<anonymous>)

at fulfilled (/app/dist/repositories/mongodb.repository.js:5:58)

at processTicksAndRejections (node:internal/process/task_queues:96:5)

npm ERR! path /app

npm ERR! command failed

npm ERR! signal SIGTERM

npm ERR! command sh -c -- node dist/index.js

npm ERR! A complete log of this run can be found in:

npm ERR! /root/.npm/_logs/2023-09-28T22_15_05_618Z-debug-0.log
Brody
Brody•14mo ago
okay let me know if anything improves with private networking disabled
BenIsenstein
BenIsenstein•14mo ago
Currently building What's your background? Thakns for being so helpful btw 🙂
Brody
Brody•14mo ago
i have no background, in tech at least last job was a landscaper
BenIsenstein
BenIsenstein•14mo ago
lol that counts How did you get involved in techy stuff?
Brody
Brody•14mo ago
uh id probably say arduino
BenIsenstein
BenIsenstein•14mo ago
Nice! Okay so interesting now Several errors in order:
Error: MongoServerClosedError: Server is closed
Error: MongoServerClosedError: Server is closed
Error: MongoServerClosedError: Server is closed
Error: MongoServerClosedError: Server is closed
BenIsenstein
BenIsenstein•14mo ago
Error: MongoPoolClosedError: Attempted to check out a connection from closed connection pool
Error: MongoPoolClosedError: Attempted to check out a connection from closed connection pool
Error: MongoServerClosedError: Server is closed
Error: MongoServerClosedError: Server is closed
Brody
Brody•14mo ago
you can connect to mongo from something like dbgate right?
BenIsenstein
BenIsenstein•14mo ago
I think so
Brody
Brody•14mo ago
check again i have to go do some plumbing, ill be back in a bit
BenIsenstein
BenIsenstein•14mo ago
I'm gonna go at this from scratch again, removing everything but the google init and logging everywhere all good, see ya!
jr
jr•14mo ago
What did it end up being? Did it just work after disabling the private network?
BenIsenstein
BenIsenstein•14mo ago
No So, I had another module trying to run a test script on import. -_-
Solution
BenIsenstein
BenIsenstein•14mo ago
The test script was trying to connect and close at the same time
BenIsenstein
BenIsenstein•14mo ago
Just forgot to comment it out once I was done with local testing How can I mark this solved?
Brody
Brody•14mo ago
glad you've solved it!
Want results from more Discord servers?
Add your server