npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

deepkill

v0.2.0

Published

Small helper to kill a process and all its children

Downloads

230

Readme

deepkill

Small helper to kill a process and all its children

Background

Say you're practicing TDD devotely. All your files are watched and Mocha does its thing while you keep improving your code.

Say you're working on a library that spawns a subprocess. Now the latter has a bug. Your unit tests fail as expected: You haven't received the right signal, or captured the right message, or whatever.

You fix, you hope, the bug but nothing happens. Your files are still watched, but Mocha doesn't react. Duh?

Is Mocha still waiting for your unit tests to complete? Shouldn't it have timed out by now?

The problem is that the buggy subprocess is hanging, and make the test runner itself hang. The unit tests are complete, but not the test runner. You kill it by hand, restart it and everything goes back to normal, once! Your watcher will remain unusable as long as your subprocess has bugs.

Of course, your child processes should recover from exceptions, or they should send the right messages and their parents clean up the mess, but that's when everything works and the library is published. For now the watcher process is broken and may remain so for a while.

Using deepKill is a hack. You call it after each test that spawns a buggy subprocess that makes your watcher hang and the run will complete as expected, whether the child process returned naturally or not.

Usage

deepKill has the following signature:

async function deepKill(pid: number, signal: string = "SIGKILL"): Promise<void>

where:

  • pid is the PID of the child process you need to kill with all its children.
  • signal defaults to "SIGKILL" and is sent to the child process you need to kill and to all its children.

In the following example, we kill a child process that prevents our code from completing without relying on its killing properly its own children, that is we use deepKill(pid) and not process.kill(pid).

import deepKill from "deepkill";
import { spawn } from "child_process";

// We create several child processes that will prevent this very process from ever completing.
const   p = spawn("node", ["./fork1.js"]);

// Now we do stuff while the children are running...

// By killing the spawned process explicitly with deepKill, we also kill all the processes
// it has ever created and that have not closed yet, thus allowing this very process to return
deepKill(p.pid).then(
  () => {
    // We do more cleanup stuff after the children have closed...
  },
  () => {
    // We could not somehow terminate all the child processes.
    console.warn(`
Some helpful message telling the user how to:
  - abort
  - find and kill the surviving child processes by hand
  - track and report the exposed bug
`);
  }
);

First we spawn "fork1.js":

// File "./fork1.js"
import { fork } from "child_process";

// fork1.js launches fork2.js which launches hang.js. The latter (an infinite loop)
// won't close, so neither fork2.js nor fork1.js will complete unless killed explicitly.
fork("./fork2.js");

which in turn forks "fork2.js":

// File "./fork2.js"
import { fork } from "child_process";

// fork2.js launches hang.js, which cannot complete by itself, therefore
// this process cannot complete either
fork("./hang.js");

which forks "hang.js", an infinite loop:

// File "./hang.js"
// hang.js will never complete unless killed explicitly
while (1) {}

Now our code will hang forever, unless we kill it, or kill "node hang.js".

If we kill "node fork2.js", then "node fork1.js" will return, but not "node hang.js", nor our code.

Until we write properly our subprocesses, deepKill keeps our development process from hanging.

License

deepkill is MIT licensed.

© 2017-2019 Jason Lenoble