-
-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor reference counting with IPC (#1089)
- Loading branch information
Showing
10 changed files
with
96 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,32 @@ | ||
// By default, Node.js keeps the subprocess alive while it has a `message` or `disconnect` listener. | ||
// We replicate the same logic for the events that we proxy. | ||
// This ensures the subprocess is kept alive while `sendMessage()`, `getOneMessage()` and `getEachMessage()` are ongoing. | ||
// This ensures the subprocess is kept alive while `getOneMessage()` and `getEachMessage()` are ongoing. | ||
// This is not a problem with `sendMessage()` since Node.js handles that method automatically. | ||
// We do not use `anyProcess.channel.ref()` since this would prevent the automatic `.channel.refCounted()` Node.js is doing. | ||
// We keep a reference to `anyProcess.channel` since it might be `null` while `getOneMessage()` or `getEachMessage()` is still processing debounced messages. | ||
// See https://github.com/nodejs/node/blob/2aaeaa863c35befa2ebaa98fb7737ec84df4d8e9/lib/internal/child_process.js#L547 | ||
export const addReference = anyProcess => { | ||
anyProcess.channel?.refCounted(); | ||
export const addReference = channel => { | ||
channel.refCounted(); | ||
}; | ||
|
||
export const removeReference = anyProcess => { | ||
anyProcess.channel?.unrefCounted(); | ||
export const removeReference = channel => { | ||
channel.unrefCounted(); | ||
}; | ||
|
||
// To proxy events, we setup some global listeners on the `message` and `disconnect` events. | ||
// Those should not keep the subprocess alive, so we remove the automatic counting that Node.js is doing. | ||
// See https://github.com/nodejs/node/blob/1b965270a9c273d4cf70e8808e9d28b9ada7844f/lib/child_process.js#L180 | ||
export const undoAddedReferences = (anyProcess, isSubprocess) => { | ||
export const undoAddedReferences = (channel, isSubprocess) => { | ||
if (isSubprocess) { | ||
removeReference(anyProcess); | ||
removeReference(anyProcess); | ||
removeReference(channel); | ||
removeReference(channel); | ||
} | ||
}; | ||
|
||
// Reverse it during `disconnect` | ||
export const redoAddedReferences = (channel, isSubprocess) => { | ||
if (isSubprocess) { | ||
addReference(channel); | ||
addReference(channel); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/usr/bin/env node | ||
import process from 'node:process'; | ||
import {getOneMessage} from '../../index.js'; | ||
|
||
process.disconnect(); | ||
console.log(process.channel); | ||
await getOneMessage(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters