Spawned process pid not correct
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
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.
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:

In contrast with version 1.10 where the correct pid is returned:

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.
strange. It shows up for me.

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.
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...
It does work when invoking node index.js directly

interesting that your pid shows a node process and mine shows an sh process 😕
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.
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.


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.
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.
I just tried with 1.10.0 but the issue still exists for me

using [email protected]:scherler/yarnWhitinYarn.git as mentioned in https://github.com/yarnpkg/yarn/issues/6692
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)