Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leaks in Jest #300

Closed
tommyjs007 opened this issue Apr 13, 2020 · 9 comments · Fixed by #357
Closed

Memory leaks in Jest #300

tommyjs007 opened this issue Apr 13, 2020 · 9 comments · Fixed by #357

Comments

@tommyjs007
Copy link

tommyjs007 commented Apr 13, 2020

Versions

  • NodeJS: 11.15.0
  • mongodb-memory-server-*: 6.5.2
  • system: Ubuntu 18.04

What is your question?

I'm running a simple test using jest with --detectLeaks argument and it detects memory leaks. Is it a normal behavior?
I experimented with the code below and tried to change uri to a real mongodb instance in this case the test passes without memory leaks errors, so I guess it suggests that the problem might be in mongodb-memory-server package.

import { MongoMemoryServer } from 'mongodb-memory-server-core';
import Mongoose from 'mongoose';

describe('Testing nonceUtils', () => {
  it('can generate nonce', async () => {
    const mongoServer = new MongoMemoryServer();
    await Mongoose.connect(await mongoServer.getUri(), {
      useNewUrlParser: true,
    });
    expect(5).toBe(5);
    await Mongoose.disconnect();
    await mongoServer.stop();
  });
});
 Your test suite is leaking memory. Please ensure all references are cleaned.

    There is a number of things that can leak memory:
      - Async operations that have not finished (e.g. fs.readFile).
      - Timers not properly mocked (e.g. setInterval, setTimeout).
      - Keeping references to the global scope.
@hasezoey
Copy link
Member

any relation with #297?

it is probably the result of the mongo_killer script that this package uses

@tommyjs007
Copy link
Author

any relation with #297?

it is probably the result of the mongo_killer script that this package uses

Yes, I closed that issue thinking that the problem was in Mongoose, but after more investigation realized that Mongoose is alright

@hasezoey
Copy link
Member

i think this project should get refactored to try and remove this mongo_killer script

@nodkz what was the intention for having this script?

@nodkz
Copy link
Collaborator

nodkz commented Apr 13, 2020

@hasezoey MMS was created not only for Jest. And this script was created to kill mongod process when the main process is killed or dies from an internal error. So this script is required for proper cleanups if some unhandled problems occur.

But we can avoid it starting if CI or NODE_ENV=test envs are provided. Or maybe disable it for Jest (it also adds some env var). BUT we should keep in mind that in some cases we may get a leak of mongod processes 😈

@silverark
Copy link

I'm experiencing the same thing. Do you need any more information?
child process handle { pid: 19474, cmd: undefined, args: undefined }
child process handle { pid: 19459, cmd: undefined, args: undefined }

The only two processes attached are node and mongod. I've tried calling stop() and kill() on the mongod server in my tests but it's still there. Killing the process mongod manually on the command line closes both remaining processes and the tests finish.

  PID TTY          TIME CMD
19474 pts/4    00:00:00 node
19459 pts/4    00:00:00 mongod

@sibelius
Copy link

sibelius commented Jun 6, 2020

what is the proper fix for this?

I think this only happens when running multiple tests at the same time

@silverark
Copy link

@sibelius It also does it for single tests for me

@sibelius
Copy link

sibelius commented Jun 8, 2020

@nodkz I think I've found some weird behavior of mongodb-memory-server on CircleCI

circleci@abe6b4207204:~$ ps aux | grep mongod
circleci  7281  3.0  0.0 1372764 54228 pts/1   Sl+  12:32   0:00 /home/circleci/app/node_modules/.cache/mongodb-memory-server/mongodb-binaries/4.2.7/mongod --bind_ip 127.0.0.1 --port 42983 --storageEngine ephemeralForTest --dbpath /tmp/mongo-mem-5185ZYHxhK91Udic --noauth
circleci  7300  2.3  0.0 562004 29364 pts/1    Sl+  12:32   0:00 /usr/local/bin/node /home/circleci/app/node_modules/mongodb-memory-server-core/scripts/mongo_killer.js 5185 7281
circleci  7307  2.0  0.0 1365568 53172 pts/1   Sl+  12:32   0:00 /home/circleci/app/node_modules/.cache/mongodb-memory-server/mongodb-binaries/4.2.7/mongod --bind_ip 127.0.0.1 --port 38677 --storageEngine ephemeralForTest --dbpath /tmp/mongo-mem-6138t8jNj2Gu0E1G --noauth
circleci  7328  2.0  0.0 562036 29092 pts/1    Sl+  12:32   0:00 /usr/local/bin/node /home/circleci/app/node_modules/mongodb-memory-server-core/scripts/mongo_killer.js 6138 7307
circleci  7335 11.0  0.0 1365568 53300 pts/1   Sl+  12:32   0:00 /home/circleci/app/node_modules/.cache/mongodb-memory-server/mongodb-binaries/4.2.7/mongod --bind_ip 127.0.0.1 --port 41563 --storageEngine ephemeralForTest --dbpath /tmp/mongo-mem-4375ELnIxSOc4Fkp --noauth
circleci  7354 10.0  0.0 562036 29160 pts/1    Sl+  12:32   0:00 /usr/local/bin/node /home/circleci/app/node_modules/mongodb-memory-server-core/scripts/mongo_killer.js 4375 7335
circleci  7368  0.0  0.0  12864  1024 pts/3    S+   12:32   0:00 grep mongod

Here as you can see neither mongod, neither mongo_killer.js script is being completed

this happened to us before doing the following upgrades

mongodb version 4.0.5 -> 4.2.7
mongodb-memory-server-global 6.5.2 -> 6.6.1

@sibelius
Copy link

sibelius commented Jun 8, 2020

check this approach

shelfio/jest-mongodb#209

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants