Under Windows, spawning a `pwsh` child process using `spawn` and `detached: true` does not work
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- JavaScript
- Star
- 122k
- Fork
- 37.4k
- Merge trung bình
- 4 ngày 3 giờ
- Pull request đã merge (30 ngày)
- 272
Mô tả
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.
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu tại điểm vào child_process.spawn và tái hiện trường hợp Windows bằng test-detached.js và test-detached.ps1 với detached: true và stdio: 'ignore'. So sánh tiến trình con node đang hoạt động với tiến trình con pwsh bị lỗi và truy vết cách xử lý tiến trình tách rời trên Windows. Hoàn tất khi pwsh chạy độc lập và tạo from-pwsh.txt, còn tiến trình cha thoát mà không chờ.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- javascript, node.js, powershell
- Lĩnh vực
- backend, operating-systems
- Loại issue
- Lỗi
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức độ hoạt động
- Ít trao đổi
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 45/100