-
-
Notifications
You must be signed in to change notification settings - Fork 34
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
Conversation
AriPerkkio
commented
Mar 24, 2024
- Fixes vitest hangs with stubbed process vitest-dev/vitest#5390
There was a problem hiding this 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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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:
Lines 35 to 36 in 6ca7bb4
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
There was a problem hiding this 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.