On Feb 19 or so, my server started to exhibit hourly increasing CPU spikes:
I traced these to the node process which serves my website. Git shows no changes between Jan 12 and Feb 24. On Feb 24 I made the following upgrades:
- "mongoose": "^4.11.6",
+ "mongoose": "^5.9.2",
- "passport-local-mongoose": "^4.1.0",
+ "passport-local-mongoose": "^6.0.1",
- "jwt-simple": "^0.5.1",
+ "jwt-simple": "^0.5.6",
- "orm": "^5.0.2",
+ "orm": "^5.0.5",
By July my CPU was spending all its time at 100%
Some profiling revealed that session-file-store is either responsible or is being called by the responsible code:
Here's the call tree, which is probably informative, though I don't really understand it
The code in which I set up session-file-store is
const next = require("next")
const express = require("express")
const passport = require("passport")
const session = require("express-session")
const FileStore = require("session-file-store")(session)
const app = next({
dir: ".",
dev: config.dev
})
app
.prepare()
.then(() => {
server = express()
server.use(cookieParser())
server.use(
session({
secret: process.env.SESSION_SECRET,
store: new FileStore({
path: "/tmp/sessions",
secret: process.env.SESSION_SECRET
}),
resave: false,
rolling: true,
saveUninitialized: true,
httpOnly: true,
cookie: {
maxAge: 60000 * 60 * 24 * 7 * 4 // 4 weeks
}
})
)
server.use(passport.initialize())
server.use(passport.session())
passportConfig(passport)
Does something in my configuration explain why session-file-store would have an hourly task with perpetually increasing volume? Is this a passport-local-mongoose bug?
The problem was that I was using the session option saveUninitialized. This was causing a huge buildup of useless session files, which are processed hourly in some way.
I solved the problem by setting saveUninitialized
to false
.