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

Node forking will include any --import from the original command #912

Open
hasezoey opened this issue Feb 4, 2025 · 3 comments
Open

Node forking will include any --import from the original command #912

hasezoey opened this issue Feb 4, 2025 · 3 comments

Comments

@hasezoey
Copy link
Member

hasezoey commented Feb 4, 2025

Versions

  • NodeJS: 23.3.0
  • mongodb-memory-server-*: 9.x, 10.x, 10.1.3
  • mongodb(the binary version): not relevant, quickest to notice is 7.0.14
  • mongodb(the js package): not relevant
  • system: linux, but likely others

package: mongo-memory-server-core

What is the Problem?

It seems like when we do child_process.fork now, it implies --import PRELOAD if the original process has been started with --import, like in the following example.
This causes the PRELOAD to be re-executed upon forking to the killer_script, causing a infinite loop of starting and never cleaning-up extra mongo servers.

see #907 (comment)

Code Example

// setup.js
const t = require("node:test");
const { MongoMemoryServer } = require("mongodb-memory-server-core");

let dbServer;

module.exports.connect = async () => {
  dbServer = await MongoMemoryServer.create();
};

module.exports.closeDatabase = async () => {
  await new Promise((res) => {
    setTimeout(res, 1000 * 10);
  })
  await dbServer.stop({ doCleanup: true });
};

t.before(async () => {
  await module.exports.connect();
});

t.after(async () => {
  await module.exports.closeDatabase();
});

// a.test.js
const t = require("node:test");

t.describe("name", () => {
  t.it("test", () => {
    1+1
  });
});

Execute with:

node --test --import ./setup.js

Debug Output

no output by default as we disable stdio for the forked process

Do you know why it happenes?

Yes, as described above.

@hasezoey
Copy link
Member Author

hasezoey commented Feb 4, 2025

This seems to be because --import affects all sub-processes (workers, forks), as per nodejs/node#52930 and nodejs/node#52939, snippet:

Modules are preloaded into the main thread as well as any worker threads,
forked processes, or clustered processes.

hasezoey added a commit that referenced this issue Feb 4, 2025
Copy link

github-actions bot commented Feb 4, 2025

🎉 This issue has been resolved in version 10.1.4-beta.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

@hasezoey
Copy link
Member Author

hasezoey commented Feb 4, 2025

@orimdominic could you test 10.1.4-beta.1 in your test cases and report back?

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

No branches or pull requests

1 participant