简体   繁体   English

如何从Visual Studio代码中调试child_process fork过程

[英]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. 从vscode调试过程执行child_process.fork无法运行,并返回退出代码12。从终端会话运行相同的测试成功。

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 SimplyExit.js

process.exit(0);

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. 如果您没有手动指定其他inspect-brk端口或删除该选项,则从以inspect-brk选项激活的父节点进程执行child_process.fork将导致此错误。

Here's the line of code in the node.js source that causes this to happen 这是导致这种情况发生的node.js源代码行

Solution

Add execArgv: [] to your fork options to prevent the child process from inheriting the inspect-brk option. execArgv: []添加到fork选项中,以防止子进程继承inspect-brk选项。 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();
    });
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM