Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows: child_process.spawn leaving orphaned processes around when running processes in an sh shell #34830

Closed
ManiacDC opened this issue Aug 18, 2020 · 7 comments
Labels
child_process Issues and PRs related to the child_process subsystem. windows Issues and PRs related to the Windows platform.

Comments

@ManiacDC
Copy link

ManiacDC commented Aug 18, 2020

  • Version: 12.14.1
  • Platform: Windows 10
  • Subsystem: x64 1909

What steps will reproduce the bug?

Please see this reproduction utility & its readme:
https://github.com/ManiacDC/npm-sh-issue

Basically, launching node from an sh shell, then running tsc from a new shell (sh -c) from within node, will leave orphaned processes behind when ctrl-c is hit in the terminal.

For reference, stdio is set to pipe, windowsHide is set to false, and shell is set to true. If stdio is set to 'inherit', this issue does not occur.

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

On demand. stdio must NOT be set to inherit. The shell launching node must be "sh" or "bash", the shell being used to launch tsc must be "sh" or "bash".

What is the expected behavior?

All child processes should close.

What do you see instead?

The child processes are orphaned and remain running until killed in the task manager (since they run tsc -w, which runs until terminated).

Additional information

This was originally discovered via lerna/lerna#2284 , reported on sindresorhus/execa#433, and narrowed down to this. I think this may actually be an issue with git-for-windows/msys2's implementation of sh & bash, but I'm not sure.

@bzoz
Copy link
Contributor

bzoz commented Aug 19, 2020

It might be that npm waits for your pipes to be closed. You can try something like this we used to fix calling npm from CITGM: nodejs/citgm@595dcf6

Anyway, there are a lot of moving parts here. It is possible that there is a Node bug here, or bash incorrectly sets up pipes or maybe npm is doing something off. Could you try to boil it down to something that at least does not include npm? A perfect reproducible example would only use your Node.js code, without npm and without tsc which also could introduce this.

My guess is that when you do all this shell switcheroos npm ends up not using winpty (#14100 (comment)).

@ManiacDC
Copy link
Author

ManiacDC commented Aug 19, 2020

Yes, it's not using winpty, but that's out of my control as this is being launched from a cross-platform shell script - which won't be using winpty. Also, how node launches the npm script is somewhat out of my control (this is being launched from within lerna in my original issue).

The original example was a shell script launching lerna via npx to call npm scripts. I was able to get rid of the npx and lerna parts, but that's it.

edit1:
I figured out a way to remove npm from the picture - check the reproduction repo... tsc is still involved, but I'm just using that as a looping process and it exits normally in other situations.

edit2:
I also found a way to remove tsc. I added a second reproduction method using "sleep", instructions in readme. If sleep is launched directly from node, or even via "sh -c 'tsc -w'" from node - this issue does not happen. I had to launch it from within a script to get it to happen.

@ManiacDC ManiacDC changed the title Windows: child_process.spawn leaving orphaned processes around when running npm scripts in an sh shell Windows: child_process.spawn leaving orphaned processes around when running processes in an sh shell Aug 19, 2020
@justinmchase
Copy link

I'm running in WSL and it has the same issue. The same code on osx behaves as expected.

@Leko Leko added child_process Issues and PRs related to the child_process subsystem. windows Issues and PRs related to the Windows platform. labels Aug 29, 2020
@huseyinacacak-janea
Copy link
Contributor

Hi @ManiacDC, I wanted to look into this issue but wasn't able to reproduce it (I tried Node 18.20.2, 20.12.2, and 22.0.0). Do you think this can be closed now?

@RedYetiDev
Copy link
Member

@nodejs/child_process

@ManiacDC
Copy link
Author

ManiacDC commented May 5, 2024

@huseyinacacak-janea I'm no longer developing in Node on Windows. If you can't reproduce, feel free to close, thanks!

@huseyinacacak-janea
Copy link
Contributor

Node.js v12 has reached its end of life and I wasn't able to reproduce it with the latest versions.
@RedYetiDev, @ManiacDC I don't have the right to close the issue. Can one of you please close it?

@targos targos closed this as completed May 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
child_process Issues and PRs related to the child_process subsystem. windows Issues and PRs related to the Windows platform.
Projects
None yet
Development

No branches or pull requests

7 participants