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

child_process: clean event listener correctly #36424

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 4 additions & 5 deletions lib/child_process.js
Expand Up @@ -378,14 +378,13 @@ function execFile(file /* , args, options, callback */) {
if (options.signal.aborted) {
process.nextTick(() => kill());
} else {
const childController = new AbortController();
options.signal.addEventListener('abort', () => {
if (!ex) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not blocking but: Unrelated style change?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I just noticed it's different from the "common" way in other places in the event target code so I fixed my code there. Putting it in a new/different PR/commit seemed like overkill I think. I don't mind reverting it if it's an issue.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah, I just wanted to make sure there was a reason beyond personal preference. The inconsistency in curly brace usage throughout the code base bugs me, and I want to make sure any changes have a justification beyond personal preference. It will at least allow us to inch towards consistency. Maybe we'll get there by about 2027.

if (!ex)
ex = new AbortError();
}
kill();
});
const remove = () => options.signal.removeEventListener('abort', kill);
child.once('close', remove);
}, { signal: childController.signal });
child.once('close', () => childController.abort());
}
}

Expand Down
11 changes: 11 additions & 0 deletions test/parallel/test-child-process-execfile.js
Expand Up @@ -3,6 +3,7 @@
const common = require('../common');
const assert = require('assert');
const execFile = require('child_process').execFile;
const { getEventListeners } = require('events');
const { getSystemErrorName } = require('util');
const fixtures = require('../common/fixtures');

Expand Down Expand Up @@ -68,5 +69,15 @@ const execOpts = { encoding: 'utf8', shell: true };

execFile(process.execPath, [echoFixture, 0], { signal: 'hello' }, callback);
}, { code: 'ERR_INVALID_ARG_TYPE', name: 'TypeError' });
}
{
// Verify that the process completing removes the abort listener
const ac = new AbortController();
const { signal } = ac;

const callback = common.mustCall((err) => {
assert.strictEqual(getEventListeners(ac.signal).length, 0);
assert.strictEqual(err, null);
});
execFile(process.execPath, [fixture, 0], { signal }, callback);
}