-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Calling unref()
on a child process instance created by Node.js' spawn
API kills the sub process
#21446
Comments
@ymeine thanks for the report. I just tried your reproduction in both Deno (v1.42.3) and Node (v.21.5.0) on macOS and in both cases the file is not written. I think the behavior here is correct - unless you wanted to spawn the subprocess as "detached" one, meaning it should continue running after the parent exits. Please provide more information. |
Hi, I've tried again on my side:
I've adapted the reproduction code to make it a bit clearer:
import process from 'node:process';
import {spawn} from 'node:child_process';
const isDeno = globalThis.Deno != null;
if (isDeno) console.log(`Running Deno ${Deno.version.deno}`);
else console.log(`Running Node.js ${process.version}`);
const timer = 'Current time';
console.time(timer);
process.on('exit', () => console.timeEnd(timer)); // should print something around 1 second, not 2 seconds
const child = spawn(
'node', ['child.js', isDeno ? 'deno' : 'node'],
{stdio: 'ignore'}, // try commenting that line to see the difference between Node.js and Deno here
);
setTimeout(() => {
console.timeLog(timer); // should print something around 1 second
child.unref();
}, 1000);
import {writeFile} from 'node:fs/promises';
import {setTimeout} from 'node:timers/promises';
const runtime = process.argv[2];
console.log(`${runtime} running`);
await setTimeout(2000);
await writeFile(`from-${runtime}.txt`, `Hello from ${runtime}`); Now I am a bit confused here because the result is not how I remembered, but honestly I was opening too many detailed bug reports in various projects at that time, my brain fatigue was real. So here is what happens:
Given the more accurate testing, and given the result you gave for the original reproduction on macOS, it looks like there are discrepancies:
At that stage I don't know what to think, since with the discrepancy for Node.js itself between OSs, it's not possible to tell if the Windows behavior is correct or not. If it is, there's a compatibility issue between Deno and Node.js; if it isn't, then there's just a Node.js bug and no Deno bugs. |
Version: Deno 1.38.0
Platform: Microsoft Windows NT 10.0.22621.0 x64
Scenario
A parent program:
that spawns a Node.js sub process implemented by
test.js
:Bug
We can see Deno fails making the sub process go through because the file
from-node.txt
is never created.If you're wondering about the different logs and timings, as well as why I spawn a child Node.js process: it's an example I adapted from this Node.js issue
They help inspecting the behavior. Thanks to that, I can see in System Informer that the sub process is created, but that it gets terminated at the same time as the parent due to the
unref()
call in the middle.This code works with Node.js v20.9.0.
Expectations
As part of the Node.js compatibility work, this should eventually be fixed.
In the meantime, update the compatibility list page which tells only this:
Context
The text was updated successfully, but these errors were encountered: