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

fix: prevent hang when process is overwritten #83

Merged
merged 1 commit into from
Mar 25, 2024

Conversation

AriPerkkio
Copy link
Member

Copy link

@hi-ogawa hi-ogawa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@@ -23,6 +23,9 @@ process.__tinypool_state__ = {
workerId: Number(process.env.TINYPOOL_WORKER_ID),
}

const memoryUsage = process.memoryUsage.bind(process)
const send = process.send!.bind(process)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this stabilizes the process and keeps the same reference, correct?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we will preserve the original process methods in the module scope. The file that is loaded later may overwrite globals:

if (filename !== null) {
await getHandler(filename, name)

So basically:

process.on("message", async (msg) => {
  const originalSend = process.send.bind(process);

  await import(msg.filename); // Contains globalThis.process = Mock;

  process.send(response);
  //      ^^^^ send is not a function

  originalSend(response);
  // ^^ works
})

birpc has similar implementation: https://github.com/antfu/birpc/blob/72510536cedb301953e0550d884667804d7268d0/src/index.ts#L145-L147

Copy link
Member

@Aslemammad Aslemammad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Feel free to merge.

@AriPerkkio AriPerkkio merged commit 18c8684 into tinylibs:main Mar 25, 2024
6 checks passed
@AriPerkkio AriPerkkio deleted the fix/overriden-process branch March 25, 2024 16:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

vitest hangs with stubbed process
3 participants