Mongodb-memory-server: "MongoMemoryReplSet" in "jest" gives open handle error

Created on 12 Aug 2019  路  20Comments  路  Source: nodkz/mongodb-memory-server

If I close the client & await replSet.stop(), jest spits out an error:

Jest did not exit one second after the test run has completed.

This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.

If I don't use a replset, then it works.

describe('MyTests', () => {
  let replSet: MongoMemoryReplSet;
  let client: MongoClient;

  beforeAll(async () => {
    replSet = new MongoMemoryReplSet({
      binary: {
        version: '4.0.5'
      },
      instanceOpts: [{
        storageEngine: 'wiredTiger'
      }],
      autoStart: true,
      debug: true
    });

    await replSet.waitUntilRunning();

    // ... connect to client, etc
  });

  afterAll(async () => {
    await client.close();
    await replSet.stop();
  });

  // ...
});

If I replace the above with just a single mongo server and stop it, it works.

bug Needs More Information stale

Most helpful comment

@abishek28007 like i said, there isnt an "smooth" transition release yet, please read the changelog on what changed

not sure what was added in beta 7 but getting "instanceInfo" is undefined for const uri = await mongod.getUri(); for MongoMemoryServer

this is because getUri is in 7.0.0 not async anymore and dosnt wait for the server start anymore


aside from this, autoStart got removed, so you need to either manually call .start, or use the static method .create

All 20 comments

@j does this still happen on later versions of this package? and could you provide your used mongodb version?

i think #166 is related

closing in favor of #166 and #300

@j does this still happen on later versions of this package? and could you provide your used mongodb version?

Which later version, I am facing this in package version 6.9.2,

An Process didnt exit with signal "SIGINT" within 10 seconds, using "SIGKILL"!
Enable debug logs for more information

  at Timeout._onTimeout (node_modules/mongodb-memory-server-core/src/util/MongoInstance.ts:162:21)

@abishek28007 your "warning" has nothing to do with this issue, it comes when MongoInstance.stop is called where SIGINT is tried first, 10 seconds waiting and this warning comes up and SIGKILL is used
-> and then if the process exists, the promise resolves - so it has nothing to do with jest open handle error

@hasezoey I posted the underlining log

Jest did not exit one second after the test run has completed.

This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.
  console.warn
    An Process didnt exit with signal "SIGINT" within 10 seconds, using "SIGKILL"!
    Enable debug logs for more information

      at Timeout._onTimeout (node_modules/mongodb-memory-server-core/src/util/MongoInstance.ts:162:21)

@abishek28007 this seems like you didnt await an .stop, did you check that already?

yup using await replSet.stop() in afterall

works fine for await mongod.stop() created with MongoMemoryServer

@abishek28007 this case is known and already fixed in 7.0.0 (beta), will not be fixed in an 6.x release (because it requires refactor of some code)
-> i mean the warning, it shouldnt hinder jest from exiting if awaited

@hasezoey which beta version I updated to "mongodb-memory-server": "^7.0.0-beta.1" still facing this issue

@abishek28007 every beta below 7 is an broken build (build process was not called directly), so use that at lowest if using beta - this warning (for replsets) got fixed in 6 (but like i said, broken build)

-> because the beta is still ongoing, there are no deprecation warnings yet on an 6.x release

@hasezoey not sure what was added in beta 7 but getting "instanceInfo" is undefined for const uri = await mongod.getUri(); for MongoMemoryServer

@hasezoey error for MongoMemoryReplSet

 `State is not "running" or "init" - cannot wait on something that dosnt start

  at MongoMemoryReplSet.<anonymous> (node_modules/mongodb-memory-server-core/src/MongoMemoryReplSet.ts:417:15)
  at fulfilled (node_modules/mongodb-memory-server-core/node_modules/tslib/tslib.js:111:62)`

@abishek28007 like i said, there isnt an "smooth" transition release yet, please read the changelog on what changed

not sure what was added in beta 7 but getting "instanceInfo" is undefined for const uri = await mongod.getUri(); for MongoMemoryServer

this is because getUri is in 7.0.0 not async anymore and dosnt wait for the server start anymore


aside from this, autoStart got removed, so you need to either manually call .start, or use the static method .create

Hello,
If I understood correctly, the workaround for now is to just stop calling replSet.stop() basically?

Thanks!

P.S.: I'm also experiencing this in Mocha, not just Jest.

@jverce no, basically the "smooth shutdown" is just not released yet, so either wait for it to use SIGKILL, or try to use 7.0 beta

I am experiencing the same issue. Tried to update to ^7.0.0-beta.1, and changed to:

    /**
     * Connect to the in-memory database.
     */
    async connect() {
        this.mongodReplSet = await MongoMemoryReplSet.create({
            replSet: { storageEngine: 'wiredTiger' },
        });
        await this.mongodReplSet.waitUntilRunning();
        const uri = await this.mongodReplSet.getUri();

        const mongooseOpts = {
            useNewUrlParser: true,
            useUnifiedTopology: true,
            useCreateIndex: true,
        };

        await connect(uri, mongooseOpts);
    }

But the database never comes online. An exception is catched inside the tmp module saying that /var/folders/qm/25v7s8kx2rb1gm5pgdphv2mw0000gn/T/mongo-mem--72763-iHJC5AJDHcfT does not exist.

@hasezoey do you have any suggestions?

I have a lot of testcases and running all of them takes quite a while, when I need to wait the 10 sec for the SIGKILL :-)

@JonasWestAlro please open an new issue, with all the information required

PS: all 7.0.0 betas below .7 were broken, and if possible always use the latest beta

Was this page helpful?
0 / 5 - 0 ratings

Related issues

yavorski picture yavorski  路  6Comments

hasezoey picture hasezoey  路  7Comments

nguyenhoangvi000 picture nguyenhoangvi000  路  5Comments

elreeda picture elreeda  路  3Comments

ecalcutin picture ecalcutin  路  6Comments