Skip to content

Exit Code is Zero if Subcommand Killed by Signal #2021

@dsanders11

Description

@dsanders11

If a subcommand child process is killed by a signal (e.g. SIGKILL), then commander will exit with exit code zero. I'm seeing this where OOM was killing the subcommand but commander was exiting with zero.

Relevant code looks to be here. The arguments to the close event are (code, signal) and in the case of killed by signal, code will be null, so process.exit gets called with null which leads to exiting with zero. Additionally, should the exitCallback case (on line 1036) be using proc.exitCode instead of process.exitCode? The latter doesn't seem correct.

commander.js/lib/command.js

Lines 1032 to 1038 in 4ef19fa

if (!exitCallback) {
proc.on('close', process.exit.bind(process));
} else {
proc.on('close', () => {
exitCallback(new CommanderError(process.exitCode || 0, 'commander.executeSubCommandAsync', '(close)'));
});
}

Possible fix for the issue could be to bubble up signals, although I'm not sure how to handle the exitCallback case then, since AFAIK there's no way to get an exit code for a signal from Node.js (other than mapping it yourself).

proc.on('close', (code, signal) => {
  if (signal) {
    process.kill(process.pid, signal);
  } else {
    process.exit.bind(process)(code);
  }
});

Alternatively a fixed exit code of 1 could be used in the case of a signal, which would work for the exitCallback case. However, that's not desirable since it will hide the underlying exit code from the subcommand.

Manually mapping signals to exit codes might be the simplest way to handle this for both cases.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugCommander is not working as intended

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions