wizebin
wizebin

Reputation: 730

How can I debug a child_process fork process from visual studio code

Description

Executing child_process.fork from a vscode debug process fails to run with and returns exit code 12. Running the same test from a terminal session succeeds.

Sample Mocha Unit Test

import { expect } from 'chai';
import { fork } from 'child_process';
import path from 'path';

describe('Child Process Fork', () => {
  it('Successfully Forks A Simple Process', (done) => {
    const child = fork(path.join(__dirname, 'SimplyExit.js'), [], { stdio: 'pipe' });
    child.on('exit', (data) => {
      expect(data).to.equal(0);
      done();
    });
  });
});

SimplyExit.js

process.exit(0);

Upvotes: 2

Views: 2478

Answers (1)

wizebin
wizebin

Reputation: 730

Executing child_process.fork from a parent node process that was started with an inspect-brk option active will cause this error if you don't manually specify a different inspect-brk port or remove the option.

Here's the line of code in the node.js source that causes this to happen

Solution

Add execArgv: [] to your fork options to prevent the child process from inheriting the inspect-brk option. Here's the full working code.

import { expect } from 'chai';
import { fork } from 'child_process';
import path from 'path';

describe('Child Process Fork', () => {
  it('Successfully Forks A Simple Process', (done) => {
    const child = fork(path.join(__dirname, 'SimplyExit.js'), [], { stdio: 'pipe', execArgv: [] });
    child.on('exit', (data) => {
      expect(data).to.equal(0);
      done();
    });
  });
});

Upvotes: 5

Related Questions