Skip to content

Dev server child process cannot be killed #5026

Description

@matthiasgeihs

Bug report

When spawning the webpack dev server via child_process.spawn in a node.js script, the child process cannot be killed via proc.kill(). If the parent process is exited via process.exit(0), the dev server child process continues running in the background and doesn't free up the bound port.

Platform: macOS 14.2.1

Relates to #2168 (comment).

Actual Behavior

proc.kill does not stop dev server.

Expected Behavior

proc.kill stops dev server.

How Do We Reproduce?

Create an npm project with the files below.
Run npm i and then npm run test.
Observe via task manager that the webpack server continues to be running in the background.

package.json

{
  "name": "webpack-test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "node test.js",
    "serve": "webpack serve --mode development"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "html-webpack-plugin": "^5.6.0",
    "webpack": "^5.89.0",
    "webpack-cli": "^5.1.4",
    "webpack-dev-server": "^4.15.1"
  }
}

webpack.config.js

const HtmlWebpackPlugin = require('html-webpack-plugin');
const path = require('path');

module.exports = {
  entry: './src/index.js',
  output: {
    path: path.resolve(__dirname, './dist'),
    filename: 'main.js',
  },
  plugins: [new HtmlWebpackPlugin()],
};

test.js

const { spawn } = require("child_process");

const proc = spawn("npm", ["run", "serve"], { stdio: "inherit" });

setTimeout(() => {
  proc.kill();
  process.exit(0);
}, 5000);

src/index.js

function component() {
  const element = document.createElement('div');
  element.innerHTML = 'Hello webpack';
  return element;
}

document.body.appendChild(component());

Output of npx webpack-cli info

% npx webpack-cli info

  System:
    OS: macOS 14.2.1
    CPU: (10) arm64 Apple M1 Pro
    Memory: 6.55 GB / 32.00 GB
  Binaries:
    Node: 21.5.0 - /opt/homebrew/bin/node
    Yarn: 3.7.0 - /opt/homebrew/bin/yarn
    npm: 10.2.4 - /opt/homebrew/bin/npm
  Browsers:
    Chrome: 120.0.6099.129
    Edge: 120.0.2210.61
    Safari: 17.2.1
  Packages:
    html-webpack-plugin: ^5.6.0 => 5.6.0 
    webpack: ^5.89.0 => 5.89.0 
    webpack-cli: ^5.1.4 => 5.1.4 
    webpack-dev-server: ^4.15.1 => 4.15.1 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions