Description
- Version: v10.12.0
- Platform: Darwin zero-gravitas-lan.jetpants.com 18.0.0 Darwin Kernel Version 18.0.0: Wed Aug 22 20:13:40 PDT 2018; root:xnu-4903.201.2~1/RELEASE_X86_64 x86_64
- Subsystem: domain
The following code, which is a reduced test case using functionality that's relied upon by the popular Sentry error reporting module, works fine in Node 8.12.0 but consistently leaks memory on each request in Node 10.12.0, 11.0.0, and 9.11.2:
'use strict';
const domain = require('domain');
const app = require('express')();
app.use((req, res, next) => {
let requestDomain = domain.create();
requestDomain.add(req);
requestDomain.on('error', next);
requestDomain.run(() => {
next();
});
});
app.get('/', (req, res) => {
req.cachedPromise = new Promise(resolve => {
let tenMebibyteString = 'a'.repeat(10 * 1024 * 1024);
resolve(tenMebibyteString);
});
res.send('hi');
});
app.listen(5000);
I would expect the req
object and its req.cachedPromise
property to be GCed once the response is finished, since no references to it are being held. This is what happens in Node 8. But in Nodes 9, 10, and 11, req.cachedPromise
never seems to be GCed, causing a steady memory leak.
I first noticed this issue when I tried to upgrade a production service using Sentry to Node 10. It likely affects anyone who uses Sentry with Express and caches promises on the request object (or on any descendant of the request object).
The relevant code in Sentry is here: https://github.com/getsentry/sentry-javascript/blob/839326fbb498c669bd8b9c38bd93d39ca15b6266/packages/node/src/handlers.ts#L220-L229