R
Railway•9mo 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•9mo ago
Project ID: 8b67ba4d-ba67-4f38-97ef-8caab4e245fe
Brody
Brody•9mo ago
mongoose?
BenIsenstein
BenIsenstein•9mo ago
nope, nodejs mongo driver repo:
BenIsenstein
BenIsenstein•9mo ago
GitHub
GitHub - BenIsenstein/notion_integration_1
Contribute to BenIsenstein/notion_integration_1 development by creating an account on GitHub.
Brody
Brody•9mo ago
looking
BenIsenstein
BenIsenstein•9mo ago
Thanks Brody 🙂
Brody
Brody•9mo ago
BenIsenstein
BenIsenstein•9mo ago
SUre, it's a part of the Client options object?
Brody
Brody•9mo ago
looks to be, yeah
BenIsenstein
BenIsenstein•9mo ago
pushed to my repo, let's monitor the deployment
Brody
Brody•9mo ago
you tell me what happens, i cant monitor anything lol
BenIsenstein
BenIsenstein•9mo ago
lol I figured I was giving you implicit consent to observe my project infra
Brody
Brody•9mo ago
dont work for railway so i cant see anything
BenIsenstein
BenIsenstein•9mo ago
Oh I thought you worked for them! What's your relation to them/how did you end up being a conductor?
Brody
Brody•9mo ago
i help so they gave me cool role
BenIsenstein
BenIsenstein•9mo ago
sweety
BenIsenstein
BenIsenstein•9mo ago
No description
BenIsenstein
BenIsenstein•9mo ago
alright still a failed health check after 10s
Brody
Brody•9mo ago
what do the deployment logs look like?
BenIsenstein
BenIsenstein•9mo ago
No description
Brody
Brody•9mo ago
that doesnt look like there was even a connection attempt?
BenIsenstein
BenIsenstein•9mo 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•9mo ago
keep me updated
BenIsenstein
BenIsenstein•9mo 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•9mo ago
looks good to me
BenIsenstein
BenIsenstein•9mo 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•9mo ago
little tip, wrap your code in triple back ticks
BenIsenstein
BenIsenstein•9mo 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•9mo ago
all looks good to me, though keep in mind im not a js/ts dev
BenIsenstein
BenIsenstein•9mo ago
fair enough And this was working seamlessly for close to a year...
Brody
Brody•9mo ago
updated any deps recently? are you using private networking?
BenIsenstein
BenIsenstein•9mo 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•9mo ago
but are you actively utilizing the private networking?
BenIsenstein
BenIsenstein•9mo 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•9mo ago
its on by default, but im asking if you use it
BenIsenstein
BenIsenstein•9mo ago
Not actively using it
Brody
Brody•9mo ago
disable it
BenIsenstein
BenIsenstein•9mo ago
Sure most recent deploy logs:
npm WARN config production Use `--omit=dev` instead.

> notion_integration_1@1.0.0 prestart

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

> notion_integration_1@1.0.0 start

> 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.

> notion_integration_1@1.0.0 prestart

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

> notion_integration_1@1.0.0 start

> 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.

> notion_integration_1@1.0.0 prestart

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

> notion_integration_1@1.0.0 start

> 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.

> notion_integration_1@1.0.0 prestart

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

> notion_integration_1@1.0.0 start

> 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•9mo ago
okay let me know if anything improves with private networking disabled
BenIsenstein
BenIsenstein•9mo ago
Currently building What's your background? Thakns for being so helpful btw 🙂
Brody
Brody•9mo ago
i have no background, in tech at least last job was a landscaper
BenIsenstein
BenIsenstein•9mo ago
lol that counts How did you get involved in techy stuff?
Brody
Brody•9mo ago
uh id probably say arduino
BenIsenstein
BenIsenstein•9mo 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•9mo 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•9mo ago
you can connect to mongo from something like dbgate right?
BenIsenstein
BenIsenstein•9mo ago
I think so
Brody
Brody•9mo ago
check again i have to go do some plumbing, ill be back in a bit
BenIsenstein
BenIsenstein•9mo 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•9mo ago
What did it end up being? Did it just work after disabling the private network?
BenIsenstein
BenIsenstein•9mo ago
No So, I had another module trying to run a test script on import. -_-
Solution
BenIsenstein
BenIsenstein•9mo ago
The test script was trying to connect and close at the same time
BenIsenstein
BenIsenstein•9mo ago
Just forgot to comment it out once I was done with local testing How can I mark this solved?
Brody
Brody•9mo ago
glad you've solved it!