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: add ChildProcess 'spawn' event #35369

Closed
wants to merge 1 commit into from

Conversation

zenflow
Copy link
Contributor

@zenflow zenflow commented Sep 27, 2020

The new event signals that the subprocess has spawned successfully and
no 'error' event will be emitted from failing to spawn.

Fixes: #35288

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines

Big thanks to @bnoordhuis for his guidance & the actual changes to lib/!

@nodejs-github-bot nodejs-github-bot added the child_process Issues and PRs related to the child_process subsystem. label Sep 27, 2020
@zenflow
Copy link
Contributor Author

zenflow commented Sep 27, 2020

The new event comes with the specification that, if emitted, it will come "before all other events and before any data is received via stdout or stderr". This is included in documentation & tests.

With this spec:

  1. Users can safely register event handlers after the 'spawn' event fires (without the risk of missing any event):
example
const { spawn } = require('child_process');
const { once } = require('events');

async function doSomethingWithChildProcess(){
  const subprocess = spawn(...spawnArgs);
  await once(subprocess, 'spawn');
  // At this point we know the 'exit' event hasn't been emitted yet.
  // We can expect it to be emitted in the future:
  await once(subprocess, 'exit');
}
  1. Users can safely handle stdout/stderr streams after the 'spawn' event fires (without the risk of missing any data):
example
const { spawn } = require('child_process');
const { once } = require('events');
const streamToPromise = require('stream-to-promise');

async function doSomethingWithChildProcess(){
  const subprocess = spawn(...spawnArgs);
  await once(subprocess, 'spawn');
  return streamToPromise(subprocess.stdout);
}

Is this a good idea? Or should this spec be left out?

@zenflow
Copy link
Contributor Author

zenflow commented Sep 27, 2020

Regarding the first item in the PR checklist: I can't check this because I haven't been able to run the whole test suite successfully,
on Windows (using vcbuild test) and on Windows Subsystem for Linux (using make -j4 test).

In both environments, various unrelated tests are failing (even without my changes applied).

But the test I added & the test I updated are both passing and the make lint & make test-doc tasks succeed. (I don't believe there are any other tasks to verify locally.)

If it's not super expensive, could we trigger the full CI run to verify the tests?

@zenflow
Copy link
Contributor Author

zenflow commented Sep 27, 2020

The "test-asan" GitHub Action failed due to a timeout in test/parallel/test-cluster-master-kill.js. This seems unrelated to the changes in the PR, and I didn't see it failing locally. Might this be a flaky test? Could we rerun the action to see?

(edit) Yup, it was just a flaky test. All the Github Actions are passing now.

doc/api/child_process.md Outdated Show resolved Hide resolved
@addaleax addaleax added the semver-minor PRs that contain new features and should be released in the next minor version. label Sep 29, 2020
@gireeshpunathil gireeshpunathil added the request-ci Add this label to start a Jenkins CI on a PR. label Oct 15, 2020
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Oct 15, 2020
@nodejs-github-bot
Copy link
Collaborator

@gireeshpunathil gireeshpunathil added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Oct 25, 2020
@aduh95 aduh95 added the commit-queue Add this label to land a pull request using GitHub Actions. label Oct 27, 2020
@github-actions github-actions bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Oct 27, 2020
@github-actions
Copy link
Contributor

Commit Queue failed
- Loading data for nodejs/node/pull/35369
✔  Done loading data for nodejs/node/pull/35369
----------------------------------- PR info ------------------------------------
Title      child_process: add ChildProcess 'spawn' event (#35369)
Author     Matthew Francis Brunetti  (@zenflow, first-time contributor)
Branch     zenflow:child-process-spawn-event -> nodejs:master
Labels     author ready, child_process, semver-minor
Commits    2
 - child_process: add ChildProcess 'spawn' event
 - doc: replace 'added' metadata for ChildProcess 'spawn' event with pla…
Committers 2
 - Matthew Francis Brunetti 
 - GitHub 
PR-URL: https://github.com/nodejs/node/pull/35369
Fixes: https://github.com/nodejs/node/issues/35288
Reviewed-By: Anna Henningsen 
Reviewed-By: Gireesh Punathil 
------------------------------ Generated metadata ------------------------------
PR-URL: https://github.com/nodejs/node/pull/35369
Fixes: https://github.com/nodejs/node/issues/35288
Reviewed-By: Anna Henningsen 
Reviewed-By: Gireesh Punathil 
--------------------------------------------------------------------------------
   ✔  Last GitHub Actions successful
   ℹ  Last Full PR CI on 2020-10-15T12:01:43Z: https://ci.nodejs.org/job/node-test-pull-request/33642/
- Querying data for job/node-test-pull-request/33642/
✔  Build data downloaded
   ✔  Last Jenkins CI successful
   ℹ  This PR was created on Sun, 27 Sep 2020 04:30:44 GMT
   ✔  Approvals: 2
   ✔  - Anna Henningsen (@addaleax): https://github.com/nodejs/node/pull/35369#pullrequestreview-498505074
   ✔  - Gireesh Punathil (@gireeshpunathil) (TSC): https://github.com/nodejs/node/pull/35369#pullrequestreview-509300281
--------------------------------------------------------------------------------
   ✔  No git cherry-pick in progress
   ✔  No git am in progress
   ✔  No git rebase in progress
--------------------------------------------------------------------------------
- Bringing origin/master up to date...
From https://github.com/nodejs/node
 * branch                  master     -> FETCH_HEAD
✔  origin/master is now up-to-date
- Downloading patch for 35369
From https://github.com/nodejs/node
 * branch                  refs/pull/35369/merge -> FETCH_HEAD
✔  Fetched commits as 0ca861745ad4..d3771c458cf4
--------------------------------------------------------------------------------
Auto-merging lib/internal/child_process.js
Auto-merging doc/api/child_process.md
warning: inexact rename detection was skipped due to too many files.
warning: you may want to set your merge.renamelimit variable to at least 2606 and retry the command.
[master 70e16da2ae] child_process: add ChildProcess 'spawn' event
 Author: Matthew Francis Brunetti 
 Date: Sat Sep 26 05:02:55 2020 -0400
 4 files changed, 51 insertions(+)
 create mode 100644 test/parallel/test-child-process-spawn-event.js
Auto-merging doc/api/child_process.md
warning: inexact rename detection was skipped due to too many files.
warning: you may want to set your merge.renamelimit variable to at least 2606 and retry the command.
[master 00c3842c02] doc: replace 'added' metadata for ChildProcess 'spawn' event with placeholder
 Author: Matthew Francis Brunetti 
 Date: Sun Sep 27 15:00:07 2020 -0400
 1 file changed, 1 insertion(+), 1 deletion(-)
   ✔  Patches applied
There are 2 commits in the PR. Attempting autorebase.
Rebasing (2/4)

Executing: git node land --amend --yes
⚠ Found Fixes: #35288, skipping..
--------------------------------- New Message ----------------------------------
child_process: add ChildProcess 'spawn' event

The new event signals that the subprocess has spawned successfully and
no 'error' event will be emitted from failing to spawn.

Fixes: #35288

PR-URL: #35369
Reviewed-By: Anna Henningsen [email protected]
Reviewed-By: Gireesh Punathil [email protected]

[detached HEAD 555eadadeb] child_process: add ChildProcess 'spawn' event
Author: Matthew Francis Brunetti [email protected]
Date: Sat Sep 26 05:02:55 2020 -0400
4 files changed, 51 insertions(+)
create mode 100644 test/parallel/test-child-process-spawn-event.js
Rebasing (3/4)
Rebasing (4/4)

Executing: git node land --amend --yes
--------------------------------- New Message ----------------------------------
doc: replace 'added' metadata for ChildProcess 'spawn' event with placeholder

Co-authored-by: mscdex [email protected]

PR-URL: #35369
Fixes: #35288
Reviewed-By: Anna Henningsen [email protected]
Reviewed-By: Gireesh Punathil [email protected]

[detached HEAD e71a315a34] doc: replace 'added' metadata for ChildProcess 'spawn' event with placeholder
Author: Matthew Francis Brunetti [email protected]
Date: Sun Sep 27 15:00:07 2020 -0400
1 file changed, 1 insertion(+), 1 deletion(-)

Successfully rebased and updated refs/heads/master.
✔ 555eadadebbfa626c8b19cca9b404f264a3178ea
✔ 4:7 Valid fixes URL. fixes-url
✔ 0:0 blank line after title line-after-title
✔ 0:0 line-lengths are valid line-length
✔ 0:0 metadata is at end of message metadata-end
✔ 6:8 PR-URL is valid. pr-url
✔ 0:0 reviewers are valid reviewers
✔ 0:0 valid subsystems subsystem
✔ 0:0 Title is formatted correctly. title-format
✔ 0:0 Title is <= 50 columns. title-length
✖ e71a315a34c8f49f11e8ebc2c6d29c7445adc80c
✔ 4:7 Valid fixes URL. fixes-url
✔ 0:0 blank line after title line-after-title
✔ 0:0 line-lengths are valid line-length
✔ 0:0 metadata is at end of message metadata-end
✔ 3:8 PR-URL is valid. pr-url
✔ 0:0 reviewers are valid reviewers
✔ 0:0 valid subsystems subsystem
✔ 0:0 Title is formatted correctly. title-format
✖ 0:72 Title must be <= 72 columns. title-length

ℹ Please fix the commit message and try again.

Commit Queue action: https://github.com/nodejs/node/actions/runs/332031152

@github-actions github-actions bot added the commit-queue-failed An error occurred while landing this pull request using GitHub Actions. label Oct 27, 2020
@zenflow
Copy link
Contributor Author

zenflow commented Oct 28, 2020

The Commit Queue failed due to the title length of the second commit "doc: replace 'added' metadata for ChildProcess 'spawn' event with placeholder" (d3771c4) which was really just a fixup for the initial commit. I didn't realize it would be included as a separate commit when merged into master.

@gireeshpunathil @aduh95 I will need to rewrite and force-push my branch to fix this, is that right?
I could just fix the title of the second commit, but I would rather squash it into the first commit.

Also regarding that second commit, which was suggested by @mscdex here #35369 (comment):
When can this REPLACEME be replaced with an actual version number?
I guess we need to wait for this PR to actually land in master before we know for certain what Node.js version the event will be introduced in.. so I guess I will make a tiny follow-up PR after this PR is merged.

@gireeshpunathil
Copy link
Member

@zenflow - yes pls, if you can squash the commits, I will trigger the flow again or land it manually.

The new event signals that the subprocess has spawned successfully and
no 'error' event will be emitted from failing to spawn.

Fixes: nodejs#35288
@gireeshpunathil gireeshpunathil added the request-ci Add this label to start a Jenkins CI on a PR. label Oct 28, 2020
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Oct 28, 2020
@nodejs-github-bot
Copy link
Collaborator

@zenflow
Copy link
Contributor Author

zenflow commented Oct 28, 2020

CI run failed due to failed test for http2 on freebsd.. seems to be just an unrelated flaky test

@nodejs-github-bot
Copy link
Collaborator

@gireeshpunathil
Copy link
Member

the only failure in parallel/test-worker-eventlooputil is captured in #35844

@gireeshpunathil gireeshpunathil added commit-queue Add this label to land a pull request using GitHub Actions. and removed commit-queue-failed An error occurred while landing this pull request using GitHub Actions. labels Oct 28, 2020
@github-actions github-actions bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Oct 28, 2020
@github-actions
Copy link
Contributor

Commit Queue failed
- Loading data for nodejs/node/pull/35369
✔  Done loading data for nodejs/node/pull/35369
----------------------------------- PR info ------------------------------------
Title      child_process: add ChildProcess 'spawn' event (#35369)
Author     Matthew Francis Brunetti  (@zenflow, first-time contributor)
Branch     zenflow:child-process-spawn-event -> nodejs:master
Labels     author ready, child_process, semver-minor
Commits    1
 - child_process: add ChildProcess 'spawn' event
Committers 1
 - Matthew Francis Brunetti 
PR-URL: https://github.com/nodejs/node/pull/35369
Fixes: https://github.com/nodejs/node/issues/35288
Reviewed-By: Anna Henningsen 
Reviewed-By: Gireesh Punathil 
------------------------------ Generated metadata ------------------------------
PR-URL: https://github.com/nodejs/node/pull/35369
Fixes: https://github.com/nodejs/node/issues/35288
Reviewed-By: Anna Henningsen 
Reviewed-By: Gireesh Punathil 
--------------------------------------------------------------------------------
   ⚠  Commits were pushed since the last review:
   ⚠  - child_process: add ChildProcess 'spawn' event
   ✔  Last GitHub Actions successful
   ℹ  Last Full PR CI on 2020-10-28T05:58:29Z: https://ci.nodejs.org/job/node-test-pull-request/33895/
- Querying data for job/node-test-pull-request/33895/
✔  Build data downloaded
- Querying failures of job/node-test-commit/41694/
✔  Data downloaded
   ✖  1 failure(s) on the last Jenkins CI run
   ℹ  This PR was created on Sun, 27 Sep 2020 04:30:44 GMT
   ✔  Approvals: 2
   ✔  - Anna Henningsen (@addaleax): https://github.com/nodejs/node/pull/35369#pullrequestreview-498505074
   ✔  - Gireesh Punathil (@gireeshpunathil) (TSC): https://github.com/nodejs/node/pull/35369#pullrequestreview-509300281
--------------------------------------------------------------------------------
   ✔  Aborted `git node land` session in /home/runner/work/node/node/.ncu

Commit Queue action: https://github.com/nodejs/node/actions/runs/333051293

@github-actions github-actions bot added the commit-queue-failed An error occurred while landing this pull request using GitHub Actions. label Oct 28, 2020
gireeshpunathil pushed a commit that referenced this pull request Oct 28, 2020
The new event signals that the subprocess has spawned successfully and
no 'error' event will be emitted from failing to spawn.

Fixes: #35288
PR-URL: #35369
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Gireesh Punathil <[email protected]>
@gireeshpunathil
Copy link
Member

landed in 3df5afb . thanks @zenflow for the contribution! 🎉 More importantly, thanks for the perseverance you showed in converting your own feature request, and the meticulous follow-ups on the PR life cycle aspects such as CI runs etc.!

@zenflow
Copy link
Contributor Author

zenflow commented Oct 28, 2020

😁 Thanks @gireeshpunathil for your help landing this!

@zenflow
Copy link
Contributor Author

zenflow commented Oct 28, 2020

@gireeshpunathil I think this would be a great feature to have in v14.
Do you think a PR to backport it (following the guide on backporting) would be accepted?

zenflow added a commit to zenflow/node that referenced this pull request Oct 28, 2020
This replaces a placeholder that was added in the commit which added
the new ChildProcess 'spawn' event.

Refs: nodejs#35369
@gireeshpunathil
Copy link
Member

Yes, I guess so.

targos pushed a commit that referenced this pull request Nov 3, 2020
The new event signals that the subprocess has spawned successfully and
no 'error' event will be emitted from failing to spawn.

Fixes: #35288
PR-URL: #35369
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Gireesh Punathil <[email protected]>
targos added a commit that referenced this pull request Nov 3, 2020
Notable changes:

child_process:
  * (SEMVER-MINOR) add ChildProcess 'spawn' event (Matthew Francis Brunetti) #35369
dns:
  * (SEMVER-MINOR) add setLocalAddress to Resolver (Josh Dague) #34824
http:
  * (SEMVER-MINOR) report request start and end with diagnostics_channel (Stephen Belanger) #34895
http2:
  * (SEMVER-MINOR) add updateSettings to both http2 servers (Vincent Boivin) #35383
lib:
  * (SEMVER-MINOR) create diagnostics_channel module (Stephen Belanger) #34895
src:
  * (SEMVER-MINOR) add --heapsnapshot-near-heap-limit option (Joyee Cheung) #33010
v8:
  * (SEMVER-MINOR) implement v8.stopCoverage() (Joyee Cheung) #33807
  * (SEMVER-MINOR) implement v8.takeCoverage() (Joyee Cheung) #33807
worker:
  * (SEMVER-MINOR) add eventLoopUtilization() (Trevor Norris) #35664

PR-URL: TODO
@targos targos mentioned this pull request Nov 3, 2020
targos added a commit that referenced this pull request Nov 3, 2020
Notable changes:

child_process:
  * (SEMVER-MINOR) add ChildProcess 'spawn' event (Matthew Francis Brunetti) #35369
dns:
  * (SEMVER-MINOR) add setLocalAddress to Resolver (Josh Dague) #34824
http:
  * (SEMVER-MINOR) report request start and end with diagnostics_channel (Stephen Belanger) #34895
http2:
  * (SEMVER-MINOR) add updateSettings to both http2 servers (Vincent Boivin) #35383
lib:
  * (SEMVER-MINOR) create diagnostics_channel module (Stephen Belanger) #34895
src:
  * (SEMVER-MINOR) add --heapsnapshot-near-heap-limit option (Joyee Cheung) #33010
v8:
  * (SEMVER-MINOR) implement v8.stopCoverage() (Joyee Cheung) #33807
  * (SEMVER-MINOR) implement v8.takeCoverage() (Joyee Cheung) #33807
worker:
  * (SEMVER-MINOR) add eventLoopUtilization() (Trevor Norris) #35664

PR-URL: #35948
MylesBorins pushed a commit that referenced this pull request Nov 4, 2020
Notable changes:

child_process:
  * (SEMVER-MINOR) add ChildProcess 'spawn' event (Matthew Francis Brunetti) #35369
dns:
  * (SEMVER-MINOR) add setLocalAddress to Resolver (Josh Dague) #34824
http:
  * (SEMVER-MINOR) report request start and end with diagnostics_channel (Stephen Belanger) #34895
http2:
  * (SEMVER-MINOR) add updateSettings to both http2 servers (Vincent Boivin) #35383
lib:
  * (SEMVER-MINOR) create diagnostics_channel module (Stephen Belanger) #34895
src:
  * (SEMVER-MINOR) add --heapsnapshot-near-heap-limit option (Joyee Cheung) #33010
v8:
  * (SEMVER-MINOR) implement v8.stopCoverage() (Joyee Cheung) #33807
  * (SEMVER-MINOR) implement v8.takeCoverage() (Joyee Cheung) #33807
worker:
  * (SEMVER-MINOR) add eventLoopUtilization() (Trevor Norris) #35664

PR-URL: #35948
targos added a commit that referenced this pull request Nov 4, 2020
Notable changes:

child_process:
  * (SEMVER-MINOR) add ChildProcess 'spawn' event (Matthew Francis Brunetti) #35369
dns:
  * (SEMVER-MINOR) add setLocalAddress to Resolver (Josh Dague) #34824
http:
  * (SEMVER-MINOR) report request start and end with diagnostics_channel (Stephen Belanger) #34895
http2:
  * (SEMVER-MINOR) add updateSettings to both http2 servers (Vincent Boivin) #35383
lib:
  * (SEMVER-MINOR) create diagnostics_channel module (Stephen Belanger) #34895
src:
  * (SEMVER-MINOR) add --heapsnapshot-near-heap-limit option (Joyee Cheung) #33010
v8:
  * (SEMVER-MINOR) implement v8.stopCoverage() (Joyee Cheung) #33807
  * (SEMVER-MINOR) implement v8.takeCoverage() (Joyee Cheung) #33807
worker:
  * (SEMVER-MINOR) add eventLoopUtilization() (Trevor Norris) #35664

PR-URL: #35948
targos added a commit that referenced this pull request Nov 4, 2020
Notable changes:

child_process:
  * (SEMVER-MINOR) add ChildProcess 'spawn' event (Matthew Francis Brunetti) #35369
dns:
  * (SEMVER-MINOR) add setLocalAddress to Resolver (Josh Dague) #34824
http:
  * (SEMVER-MINOR) report request start and end with diagnostics_channel (Stephen Belanger) #34895
http2:
  * (SEMVER-MINOR) add updateSettings to both http2 servers (Vincent Boivin) #35383
lib:
  * (SEMVER-MINOR) create diagnostics_channel module (Stephen Belanger) #34895
src:
  * (SEMVER-MINOR) add --heapsnapshot-near-heap-limit option (Joyee Cheung) #33010
v8:
  * (SEMVER-MINOR) implement v8.stopCoverage() (Joyee Cheung) #33807
  * (SEMVER-MINOR) implement v8.takeCoverage() (Joyee Cheung) #33807
worker:
  * (SEMVER-MINOR) add eventLoopUtilization() (Trevor Norris) #35664

PR-URL: #35948
targos pushed a commit that referenced this pull request Mar 3, 2021
The new event signals that the subprocess has spawned successfully and
no 'error' event will be emitted from failing to spawn.

Fixes: #35288
PR-URL: #35369
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Gireesh Punathil <[email protected]>
MylesBorins pushed a commit that referenced this pull request Apr 6, 2021
The new event signals that the subprocess has spawned successfully and
no 'error' event will be emitted from failing to spawn.

Fixes: #35288
PR-URL: #35369
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Gireesh Punathil <[email protected]>
@danielleadams danielleadams mentioned this pull request May 3, 2021
@targos targos removed the commit-queue-failed An error occurred while landing this pull request using GitHub Actions. label Sep 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author ready PRs that have at least one approval, no pending requests for changes, and a CI started. child_process Issues and PRs related to the child_process subsystem. semver-minor PRs that contain new features and should be released in the next minor version.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: ChildProcess 'spawn' event
7 participants