nodejs / nodejs/node

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

オープン
#51,018 コメント 6 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

主要言語
JavaScript
スター
122k
フォーク
37.4k
平均マージ
4日 2時間
マージ済み PR(30日)
283

説明

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. リポジトリをフォークし、ブランチを切って変更します。
  4. 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 を短くまとめたダイジェスト。