nodejs / nodejs/node

Under Windows, spawning a `pwsh` child process using `spawn` and `detached: true` does not work

未关闭
#51,018 6 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

主要语言
JavaScript
星标
122k
派生
37.4k
平均合并
4 天 3 小时
30 天内合并 PR
272

描述

Version

v20.9.0

Platform

Microsoft Windows NT 10.0.22621.0 x64

Subsystem

child_process

What steps will reproduce the bug?

With this Node.js program:

import {spawn} from 'node:child_process';

const timer = `Running program`;
console.time(timer);
process.on('exit', () => console.timeEnd(timer));

const options = {
	detached: true, stdio: 'ignore', // try commenting to see both sub processes work but not being detached
};
const procNode = spawn('node', ['test-detached.js'], options); procNode.unref(); // always works
const procPwsh = spawn('pwsh', ['-File', 'test-detached.ps1'], options); procPwsh.unref(); // fails when detached: true

and those files next to it:

test-detached.js

import {writeFile} from 'node:fs/promises';
import {setTimeout} from 'node:timers/promises';

await setTimeout(2000);
await writeFile('from-node.txt', 'Hello from Node.js');

test-detached.ps1

Start-Sleep -Seconds 2
'Hello from PowerShell (pwsh)' > ./from-pwsh.txt

You can see that when activating the detached option, only from-node.txt is generated (after 2 seconds), and the parent program indeed doesn't wait for the child processes to complete.

If you deactivate that option, you can see both files from-node.txt and from-pwsh.txt appear, but the parent process waits (around 2 seconds + overhead).

How often does it reproduce? Is there a required condition?

It always happens.

What is the expected behavior? Why is that the expected behavior?

The PowerShell execution should go through.

What do you see instead?

The PowerShell execution does not go through.

Additional information

I am a bit clueless here. Given the fact it works with node.exe but doesn't with pwsh.exe, it is clearly contextual.

Either Node.js should handle such cases automatically, or it should open its API more to allow fine tuning the spawn configuration to make such cases work. I am really wondering what PowerShell does/expects to have the process fail.

Note that according to one of the comments on this thread on StackOverflow, it seems to be an issue on Windows only. So maybe indeed a need to adjust the use of Windows APIs...

I may push my investigation further and post updates here. For instance I'll try the same test from Python. I'll also double check on Linux.

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 child_process.spawn 入口开始,使用 detached: true 和 stdio: 'ignore',通过 test-detached.js 和 test-detached.ps1 重现 Windows 情况。比较正常工作的 node 子进程和失败的 pwsh 子进程,并跟踪 Windows 对分离进程的处理。完成标准是 pwsh 独立运行并创建 from-pwsh.txt,同时父进程退出而无需等待。

由索引模型根据 Issue 内容生成。

评估

技术栈
javascript, node.js, powershell
领域
backend, operating-systems
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
冷清
描述清晰度
基本清楚
新手友好度
45/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。