yarn icon indicating copy to clipboard operation
yarn copied to clipboard

Spawned process pid not correct

Open subhero24 opened this issue 7 years ago • 12 comments

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

subhero24 avatar Nov 07 '18 16:11 subhero24

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.

rally25rs avatar Nov 07 '18 21:11 rally25rs

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 avatar Nov 07 '18 21:11 subhero24

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.

subhero24 avatar Nov 07 '18 21:11 subhero24

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 avatar Nov 07 '18 21:11 rally25rs

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...

rally25rs avatar Nov 07 '18 21:11 rally25rs

It does work when invoking node index.js directly

yarn-without-script

subhero24 avatar Nov 07 '18 21:11 subhero24

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

rally25rs avatar Nov 07 '18 21:11 rally25rs

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.

rally25rs avatar Nov 07 '18 21:11 rally25rs

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.

subhero24 avatar Nov 07 '18 21:11 subhero24

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.

reaktivo avatar Nov 09 '18 10:11 reaktivo

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

screenshot_2018-12-05_13-55-59

using [email protected]:scherler/yarnWhitinYarn.git as mentioned in https://github.com/yarnpkg/yarn/issues/6692

scherler avatar Dec 05 '18 12:12 scherler

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)

danieltroger avatar Jan 01 '24 13:01 danieltroger