Skip to content
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

Kill all descendants tree of a process #34

Closed
wants to merge 16 commits into from

Conversation

tomsotte
Copy link

Fixes #21

Added tree option to default (linux) and macOS kill methods, like it has already been implemented for Windows.

When tree option is enabled the process specified by the input, and its descendants, will be killed.
To kill the whole tree it has been added and used the module tree-kill.
To handle a process name as input, which can match to multiple processes, it has been added the function getPidsFromInput, which uses ps-list to get all the pids by the proces name. This is needed because tree-kill needs to know the PID of the parent process.

Some fixtures have been added for test. The tests added check if the process and its descendant has been killed as well. A test also tries to kill a named process, to test getPidsFromInput.

When `tree` option is enabled the process specified by the input, and its descendants, will be killed.
To kill the whole tree it has been added and used the module `tree-kill`.
To handle a process name as input, which can match to multiple processes, it has
been added the function `getPidsFromInput`, which uses `ps-list` to get all the
pids by the proces name.
Various errors for pathing for fixtures has been corrected, mostly caused
by `child_process.spawn`.
On Windows it's not possible to rename a process in the task list via JS,
so the test to kill tree by name has been disabled for Windows.
On Windows, to taskkill a tree `force` must be enabled.
});
}

// eslint-disable-next-line ava/test-ended
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've disabled eslint for those line because the rule ava/test-ended seems to not correctly report the usage of t.end() when using a macro. Later on I may report as an issue to ava repo.

@sindresorhus
Copy link
Owner

Can you fix the merge conflict?

@sindresorhus
Copy link
Owner

See pkrumins/node-tree-kill#20, maybe we should use pidtree instead?

// eslint-disable-next-line ava/test-ended
test.cb('kill all descendants tree by pid', testKillDescendant);

if (process.platform !== 'win32') {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this not tested on Windows?

Copy link
Author

@tomsotte tomsotte Jul 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To test killing a tree of processes I need a parent process that can spawn a descendant.
To test killing a process by its name I need a fixture that can change name, otherwise I'm not able to pinpoint that process easily.
The fixture descendant that I'm using can change its own name via process.title but cannot on Windows. I've just tested this on Windows 10.

There are three solutions that comes to mind:

  • create a binary fixture for Windows only that spawn a descendant and has a custom process name
  • kill indiscriminately all processes called node.exe
  • change the process title using some other tool, better if built-in in Windows (but I've found nothing so far and haven't searched enough)

readme.md Show resolved Hide resolved

if (process.platform !== 'win32') {
// eslint-disable-next-line ava/test-ended
test.cb('kill all descendants tree by name', testKillDescendant, 'fkill-descen');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
test.cb('kill all descendants tree by name', testKillDescendant, 'fkill-descen');
test.cb('kill all descendants tree by name', testKillDescendant, 'fkill-descendant');

?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for not mentioning it before.
I've used the string "fkill-descen" because it's 12 chars, which is the theoretical maximum of chars I can change on process.title because of the limitation that have Linux and macOS as explained in the Node.js process.title docs.
If I'd got above the limit it might not match the process title and fail the test.

@tomsotte
Copy link
Author

tomsotte commented Jul 3, 2019

See pkrumins/node-tree-kill#20, maybe we should use pidtree instead?

I'll have a look. I've already seen that issue but for some reasons, which I don't recall now, I've decided to use node-tree-kill instead anyway.

AFAIK macOS and Linux can use the same technique for killing a process tree
and a single process via `process.kill`, as such their implementation have
been merged into `defaultKill`;

I've removed missingBinaryError, because it was used for checking if the
commands `kill` or `killall` were available on the host, but now it just
use `process.kill`.

`options.tree` has been defaulted from the start because it's now used for
both the kill methods implementations (Linux/macOS and Windows).

The type of `input` in `handleKill` is now more important and the method
have been streamlined; if the input is a:

- process name (string), it gathers all the pids, similar to what
`killAll` would do, and filter all the processes by it's name, excluding
the `fkill` process itself;
- process PID (number), it checks the existance of the process and proceed
to kill it.

As per the above changes the check for the existance of a process have
been moved from the catch and duplicated accordingly to the type of the
input.
@tomsotte
Copy link
Author

tomsotte commented Jul 4, 2019

Before trying to fix the last test "don't kill fkill when killing node" that won't pass I'll wait for an answer at #30 as I may have not understand if that test is correct and should work or not on Windows.

@sindresorhus
Copy link
Owner

How can this move forward?

@sindresorhus
Copy link
Owner

that won't pass I'll wait for an answer at #30

That is stuck on #38. If you have any thoughts there, please do comment.

@tomsotte
Copy link
Author

tomsotte commented Jul 6, 2020

I'll try to think of something, but it's been so long I need some time to understand where I left this haha

Base automatically changed from master to main January 23, 2021 11:20
@sindresorhus
Copy link
Owner

Closing as this is not moving forward.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Kill tree (all sub-processes)
2 participants