Document [sub]process.channel.refCounted()
and [sub]process.channel.unrefCounted()
#53128
Labels
feature request
Issues that request new features to be added to Node.js.
process
Issues and PRs related to the process subsystem.
stale
What is the problem this feature will solve?
Background
When using Node.js IPC, if the child process sets a listener on the
message
ordisconnect
events, the child process is kept alive as long as those listeners exist. This is good and done automatically by Node.js usingnewListener
andremoveListener
hooks.node/lib/child_process.js
Lines 180 to 185 in c137d6e
Users can opt out of this if they want to manually manage this instead, by calling
process.channel.ref()
andprocess.channel.unref()
. When they do, the automatic reference counting stops happening.node/lib/internal/child_process.js
Lines 547 to 572 in f05baff
Problem
I am contributing to a library (Execa) that has a higher-level API built on top of Node.js builtin IPC. For several reasons, I need to proxy IPC messages. To do this, I need to listen to
message
events. However, that listener should not keep the process alive.The solution to this problem is currently to call
process.channel.unref()
. However, this has the following undesirable side effect: if the library's user is also setting up their ownmessage
/disconnect
event handlers, those will not be automatically reference counted anymore. Which means the child process might accidentally exit too early.What is the feature you are proposing to solve the problem?
A better solution to this problem is, after setting up
message
event listeners that should not be ref'd, to callprocess.channel.unrefCounted()
instead (notprocess.channel.unref()
). This has the same desired effect of preventing the child process from being kept alive. But it does not impede on automatic reference counting..refCounted()
and.unrefCounted()
have been added 5 years ago in #30165. They are stable and could be useful for other use cases that the one described above.My suggestion is: those two methods should be documented. Both for the parent process and the child process. Please note that
subprocess.channel.ref()
,subprocess.channel.unref()
,process.channel.ref()
andprocess.channel.unref()
are already documented, so this would effectively document "sibling" methods.The text was updated successfully, but these errors were encountered: