-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
lib: allow custom names for spawned processes #7696
Conversation
@@ -347,7 +347,11 @@ function normalizeSpawnArguments(file /*, args, options*/) { | |||
} | |||
} | |||
|
|||
args.unshift(file); | |||
if ('name' in options) { |
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.
Perhaps this should be a bit more specific, like typeof options.name === 'string'
.
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.
Fixed.
32435ca
to
46f188a
Compare
customName.stdout.on('data', (data) => { | ||
customNameOutput += data; | ||
}); | ||
customName.on('close', (code, signal) => { |
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.
Better to be use common.mustCall
to wrap the callback function
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.
Fixed.
46f188a
to
1489f88
Compare
LGTM. CI Run: https://ci.nodejs.org/job/node-test-pull-request/3277/ Edit: Changes look LGTM. We can still hear from others how useful they think this feature would be. |
customNameOutput += data; | ||
}); | ||
customName.on('close', common.mustCall((code, signal) => { | ||
assert.strictEqual(customNameOutput.trim(), 'customName'); |
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'd be curious to see if this works on SmartOS. IIRC, SmartOS does not support changing process.title
Yea, looks like tests are failing on Windows and SmartOS |
1489f88
to
5fd63e8
Compare
Interesting. It appears that there's no mechanism to change the process name on Windows ( http://stackoverflow.com/questions/23783985/set-child-process-name-in-windows ). I've added a note to the documentation that this isn't supported on all platforms and added check that skips this test for Windows and SunOS. |
|
||
if (common.isWindows || common.isSunOS) { | ||
common.skip('Windows and SmartOS do not support changing process name'); | ||
return; |
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.
Indentation alright here?
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.
Oops. fixed.
5fd63e8
to
2d72e34
Compare
I do think we should be cautious about adding features that are not supported on all platforms. |
2d72e34
to
df6140d
Compare
IMHO this is not the right way to do it. Node already supports Now, if you want to do that after spawning a process you'd need to run that code in the child. A quick idea would be to have a Please don't follow this blindly, maybe there is a better option. I'd go for actually not doing anything. If you want your workers to have a different process title set some WORKER_NAME env variable and call |
I agree with both @evanlucas and @saghul |
Perhaps this is starting to evolve into a nomenclature issue then. My personal motivation was controlling process name, but in general, the ability to control the This patch would eliminate hacky-er things such as https://github.com/andrewffff/child_process_with_argv0 or needless intermediaries like https://github.com/andrep/argv0 Would this be more amenable as an option named |
I don't have all platforms to test right now, but are you certain about that? |
@saghul on my OS X laptop for example:
|
I should qualify that as "often limited" or "sometimes limited", it's not universally limited (i.e. Linux always allows 16 characters IIRC) |
As for having the workers change their process title, that is explicitly against my current goals which is to not have to modify the workers at all (i.e. they shouldn't know they're being run by a runner service and not being invoked directly, nor more importantly should a service need to be modified to be supported by this runner). |
@ppannuto I think it’s still the length of the entire argv array that counts, because that’s the space that’s available for it. @saghul Keep in mind that this applies not only to Node.js child processes. I have actually been wondering why Node.js doesn’t support setting Also, that specifying |
@addaleax you are correct that it's full I'll update the PR to call the option |
That does sound better to me, yes. |
@jasnell think I got everything. Lmk if anything else needs to change. |
@@ -395,6 +397,14 @@ child.on('error', (err) => { | |||
}); | |||
``` | |||
|
|||
*Note: Certain platforms (OS X, Linux) will use the value of `argv[0]` for the |
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.
s/_Note:/_Note*:
For this one and the next. :-)
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.
Every other "Note" in the file currently italicizes the whole note ( https://github.com/nodejs/node/blame/master/doc/api/child_process.md#L26, https://github.com/nodejs/node/blame/master/doc/api/child_process.md#L39, https://github.com/nodejs/node/blame/master/doc/api/child_process.md#L188 etc). I'm happy to change this one instance if you want, but should it really be inconsistent with the rest of the file?
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.
Ok, no worries. I'll go through and fix those in a separate PR :-)
Awesome... thank you @ppannuto. I left a couple more comments but I think this is just about ready to go! |
For historical and other reasons, node overwrites `argv[0]` on startup. See - 2c6f79c, - nodejs#7434 - nodejs#7449 - nodejs#7696 For cases where it may be useful, save the original value of `argv[0]` in `process.argv0`
In some cases it useful to control the value of `argv[0]`, c.f. - https://github.com/andrewffff/child_process_with_argv0 - https://github.com/andrep/argv0 This patch adds explicit support for setting the value of `argv[0]` when spawning a process.
d361830
to
c9880e3
Compare
Updated. I didn't currently change the |
Thank you! LGTM! |
New new CI (that last one failed on Windows): https://ci.nodejs.org/job/node-test-commit/4447/ |
For historical and other reasons, node overwrites `argv[0]` on startup. See - 2c6f79c, - #7434 - #7449 - #7696 For cases where it may be useful, save the original value of `argv[0]` in `process.argv0` PR-URL: #7696 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
In some cases it useful to control the value of `argv[0]`, c.f. - https://github.com/andrewffff/child_process_with_argv0 - https://github.com/andrep/argv0 This patch adds explicit support for setting the value of `argv[0]` when spawning a process. PR-URL: #7696 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
For historical and other reasons, node overwrites `argv[0]` on startup. See - 2c6f79c, - #7434 - #7449 - #7696 For cases where it may be useful, save the original value of `argv[0]` in `process.argv0` PR-URL: #7696 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
In some cases it useful to control the value of `argv[0]`, c.f. - https://github.com/andrewffff/child_process_with_argv0 - https://github.com/andrep/argv0 This patch adds explicit support for setting the value of `argv[0]` when spawning a process. PR-URL: #7696 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
Notable changes: * build: zlib symbols and additional OpenSSL symbols are now exposed on Windows platforms. (Alex Hultman) #7983 and #7576 * child_process, cluster: Forked child processes and cluster workers now support stdio configuration. (Colin Ihrig) #7811 and #7838 * child_process: argv[0] can now be set to arbitrary values in spawned processes. (Pat Pannuto) #7696 * fs: fs.ReadStream now exposes the number of bytes it has read so far. (Linus Unnebäck) #7942 * repl: The REPL now supports editor mode. (Prince J Wesley) #7275 * util: inspect() can now be configured globally using util.inspect.defaultOptions. (Roman Reiss) #8013 Refs: #8020 PR-URL: #8070
Notable changes: * build: zlib symbols and additional OpenSSL symbols are now exposed on Windows platforms. (Alex Hultman) #7983 and #7576 * child_process, cluster: Forked child processes and cluster workers now support stdio configuration. (Colin Ihrig) #7811 and #7838 * child_process: argv[0] can now be set to arbitrary values in spawned processes. (Pat Pannuto) #7696 * fs: fs.ReadStream now exposes the number of bytes it has read so far. (Linus Unnebäck) #7942 * repl: The REPL now supports editor mode. (Prince J Wesley) #7275 * util: inspect() can now be configured globally using util.inspect.defaultOptions. (Roman Reiss) #8013 Refs: #8020 PR-URL: #8070
Notable changes: * build: zlib symbols and additional OpenSSL symbols are now exposed on Windows platforms. (Alex Hultman) nodejs#7983 and nodejs#7576 * child_process, cluster: Forked child processes and cluster workers now support stdio configuration. (Colin Ihrig) nodejs#7811 and nodejs#7838 * child_process: argv[0] can now be set to arbitrary values in spawned processes. (Pat Pannuto) nodejs#7696 * fs: fs.ReadStream now exposes the number of bytes it has read so far. (Linus Unnebäck) nodejs#7942 * repl: The REPL now supports editor mode. (Prince J Wesley) nodejs#7275 * util: inspect() can now be configured globally using util.inspect.defaultOptions. (Roman Reiss) nodejs#8013 Refs: nodejs#8020 PR-URL: nodejs#8070
@MylesBorins is there a reason this was never backported to v4? |
@gibfahn it was never brought up. Semver minors need to be proposed |
@MylesBorins okay, didn't realise that. Is this how I propose that it be backported? |
Checklist
make -j4 test
(UNIX), orvcbuild test nosign
(Windows) passesAffected core subsystem(s)
lib/child_process.js
Description of change
Add a
name
parameter to theoptions
accepted bychild_process.spawn
that sets the spawned process name (argv[0]
)to a custom value. The default behavior is unchanged if a
name
parameter is not supplied.
It is useful to be able to set the name of a process you are spawning.
One motivating example is an application runner service that spawn
several other node applications. Instead of showing up as 20 different
node
processes, the application runner can give them meaningful names.While child processes can write
process.title
to change their name,this has two limitations:
argv[0]
in practice