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

Spawned process pid not correct #6650

Open
subhero24 opened this issue Nov 7, 2018 · 12 comments
Open

Spawned process pid not correct #6650

subhero24 opened this issue Nov 7, 2018 · 12 comments

Comments

@subhero24
Copy link

subhero24 commented Nov 7, 2018

Do you want to request a feature or report a bug?

Regression

What is the current behavior?

Spawning a process when using 'yarn run' reports the wrong pid for the spawned process

If the current behavior is a bug, please provide the steps to reproduce.

Repository with reproducable bug can be found here https://github.com/subhero24/yarn

What is the expected behavior?

When running 'yarn run spawn' it prints the pid of the spawned process, which is not the correct pid!

Please mention your node.js, yarn and operating system version.

The bug appears in yarn 1.11.0 and higher under macOS 10.14 with node v10.13.0

@ghost ghost assigned torifat Nov 7, 2018
@ghost ghost added the triaged label Nov 7, 2018
@rally25rs
Copy link
Contributor

rally25rs commented Nov 7, 2018

Could you explain what you mean? I tried this on OSX, yarn 1.12, node v8 and v10.13.0.

I seem to get the pid that I would expect:

~/Projects/subhero24-yarn (master) 🐒   nvm use v10.13.0
Now using node v10.13.0 (npm v6.4.1)

~/Projects/subhero24-yarn (master) 🐒   yarn run spawn
yarn run v1.12.1
$ node index.js
47817

~ 🐒   ps ww 47817
  PID   TT  STAT      TIME COMMAND
47817 s004  S+     0:00.00 /bin/sh /var/folders/yf/gcmnw1y96k31lh9ttjhfm8v9ssxkkq/T/yarn--1541624929634-0.2825856047718529/node /Users/jvalore/Projects/subhero24-yarn/child.js

I'm not sure what you are seeing that you consider incorrect since you didn't include the output.


Since it is your code in index.js that is spawning the child process through node and reporting the pid, I don't see what this would have to do with yarn.

@subhero24
Copy link
Author

Here is the result of the script and Activity monitor showing the node processes that are running.

The pid printed to console is not in the list of the running processes pids:
yarn-1 11

In contrast with version 1.10 where the correct pid is returned:
yarn-1 10

@subhero24
Copy link
Author

subhero24 commented Nov 7, 2018

Btw you can see that under v1.11 the pids increment by 2. I suspect that this has something to do with the bug, as the logged pid is always in between 2 existing pids. Maybe the process was immediately closed and respawned or something, but still returning the old pid.

@rally25rs
Copy link
Contributor

strange. It shows up for me.

image

I still don't think this would be a yarn issue. You are calling node's child_process.spawn directly. Yarn wouldn't have anything to do with the pid that is returned.

I would venture to guess that you would see the same behavior is you just ran node index.js without yarn in the middle of it.

@rally25rs
Copy link
Contributor

rally25rs commented Nov 7, 2018

side note, it probably jumps by 2 pids because yarn/node is invoked, and child_process.spawn internally runs your command in a newly spawned shell (sh by default), so each time you run you would get a node and a sh process.

edit: actually you might get more processes than that, because you invoke yarn (1) which invokes your script through child_process.spawn which makes a sh (2) which executes node index.js (3) which runs child.js through child_process.spawn which makes an sh (4) so I would expect to pid to jump by 4 I guess...

@subhero24
Copy link
Author

It does work when invoking node index.js directly

yarn-without-script

@rally25rs
Copy link
Contributor

interesting that your pid shows a node process and mine shows an sh process 😕

@rally25rs
Copy link
Contributor

I'm on Sierra 10.12.4. I wonder if the OS difference has anything to do with it. Unfortunately I can't upgrade OSX on this laptop to try it out, as it doesn't belong to me.

In your process monitor, if you show all processes, do you get an sh command with that pid? From my 1st reply, you can see that my ps command also reports that it is a /bin/sh process, not a node process.

@subhero24
Copy link
Author

subhero24 commented Nov 7, 2018

I was indeed filtering because i thought it would be a node process. Yarn 1.10 spawns only node processes. While 1.11 also has a sh besides the node process.

yarn-1 10

yarn-1 11

So the pid is not the wrong one, but the following code broke for me:

function respawn() {
	if (runner != null) {
		runner.kill();
	}
	runner = child_process.spawn('node', ['build/backend/index.js'], {
		stdio: [process.stdin, process.stdout, process.stderr],
	});
}

This functions gets called repeatedly when it should restart the process.
I suspect the problem is that runner.kill() kills the sh process and does not terminate the node process. It does work under 1.10 however.

@reaktivo
Copy link

reaktivo commented Nov 9, 2018

Just came in here to say that this issue also bit us. We noticed this after we where having issues having the A webpack dev server restart correctly, and having the port being error out with in use.

As @subhero24 mentioned, reverting back to 1.10 helped or simply running the script via npm.

@scherler
Copy link

scherler commented Dec 5, 2018

I just tried with 1.10.0 but the issue still exists for me

screenshot_2018-12-05_13-55-59

using git@github.com:scherler/yarnWhitinYarn.git as mentioned in #6692

@danieltroger
Copy link

danieltroger commented Jan 1, 2024

I'm also having issues with yarn berry (4.0.2) and spawn, this is on node v21.5.0 on Ubuntu 22.04.3 LTS on a gcloud t2a-standard-1 instance.

Doing

const spawnedProcess = spawn(command, args, {
      cwd: cwd,
      env: { ...process.env },
    });
const pid = spawnedProcess.pid;

pid seems to become the pid of the current process (the one calling spawn) instead of the spawned process. spawnedProcess.kill(9) doesn't work either. I've been trying to kill a process for four hours before figuring out that spawn is broken and it has been very painful.

It only happens if the spawned command is another yarn command (in my case running in a different cwd)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants
@reaktivo @rally25rs @torifat @scherler @subhero24 @danieltroger and others