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

doc: add note about stdio streams in child_process #55322

Merged
merged 4 commits into from
Oct 22, 2024

Conversation

Ed-roro
Copy link
Contributor

@Ed-roro Ed-roro commented Oct 8, 2024

This is a PR for a old issue in hope for it to be resolved and closed. This will be my first contribution to node 😊

Fixes: #15714

@nodejs-github-bot nodejs-github-bot added child_process Issues and PRs related to the child_process subsystem. doc Issues and PRs related to the documentations. labels Oct 8, 2024
@Ed-roro Ed-roro changed the title Update documentation for child_process doc: Update documentation for child_process Oct 8, 2024
@Ed-roro Ed-roro changed the title doc: Update documentation for child_process doc: update documentation for child_process Oct 8, 2024
Comment on lines 1064 to 1066
**NOTE:** You cannot pass `stdin` as a writable or `stdout`/`stderr` as readable
under `options.stdio`. If you do so on a stream object, there is no guarantee
that the callback will be called. If the stream errors, all callbacks will be dropped.
Copy link
Member

Choose a reason for hiding this comment

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

IIUC you can pass a readable where a writable should be (and vise-versa), however it's not recommended, right?

Copy link
Contributor Author

@Ed-roro Ed-roro Oct 8, 2024

Choose a reason for hiding this comment

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

@RedYetiDev It seems from further research, you can technically pass a readable stream where a writable is expected (and vice-versa) in Node.js, but it's not recommended because:

  1. Stream Behavior: Readable and writable streams have distinct behaviors. A readable stream emits data that can be consumed, whereas a writable stream receives data to process. Swapping them might cause unexpected results, errors, or simply do nothing if the stream isn't used correctly.

  2. Compatibility Issues: Some operations depend on specific stream types. For example, trying to write to a readable stream or read from a writable stream could result in runtime errors or program malfunction since the intended stream behavior is different.

  3. Design Consistency: Keeping streams properly defined ensures that your code is easier to read, debug, and maintain. It's best to follow the convention that stdin is writable and stdout/stderr are readable, which aligns with how processes and streams are designed to work in Node.js.

Example of potential issues:

  • If you mistakenly treat stdout (which should be readable) as writable, you'd get an error when trying to write to it.

  • Similarly, if you pass a readable stream (like stdout) to something expecting writable input (like stdin), it would do nothing since stdout cannot accept writes.

Overall doing this is strongly discouraged and should always use streams as intended ((readable for output, writable for input) to avoid unexpected behaviors and erros.

I can edit the docs to reflect this discovery.

@RedYetiDev
Copy link
Member

Please edit the first commit message to be a valid commit, such as doc: add note about stdio streams in child_process

@Ed-roro Ed-roro changed the title doc: update documentation for child_process doc: add note about stdio streams in child_process Oct 8, 2024
@@ -1061,6 +1061,15 @@ pipes between the parent and child. The value is one of the following:
corresponds to the index in the `stdio` array. The stream must have an
underlying descriptor (file streams do not start until the `'open'` event has
occurred).
**NOTE:** While it is technically possible to pass `stdin` as a writable or
`stdout`/`stderr` as readable under `options.stdio`, it is not recommended.
Copy link
Member

Choose a reason for hiding this comment

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

nit: I don't think you need to need to explicitly state that this is under options.stdio, as this whole section is referring to that optional already

@RedYetiDev
Copy link
Member

@Ed-roro you need to update the commit message to follow guidelines, not the PR title.

@Ed-roro
Copy link
Contributor Author

Ed-roro commented Oct 8, 2024

@Ed-roro you need to update the commit message to follow guidelines, not the PR title.

@RedYetiDev Ah that's my mistake 😅. Should i do so for the other commits as well?

@RedYetiDev
Copy link
Member

No need to do the other comments, only the first one matters

@Ed-roro
Copy link
Contributor Author

Ed-roro commented Oct 9, 2024

@RedYetiDev First commit message changed to doc: add note about stdio streams in child_process 👍🏿

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

@RedYetiDev RedYetiDev added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Oct 12, 2024
@aduh95 aduh95 added commit-queue Add this label to land a pull request using GitHub Actions. commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. labels Oct 22, 2024
@nodejs-github-bot nodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Oct 22, 2024
@nodejs-github-bot nodejs-github-bot merged commit 1c2eecd into nodejs:main Oct 22, 2024
25 checks passed
@nodejs-github-bot
Copy link
Collaborator

Landed in 1c2eecd

aduh95 pushed a commit that referenced this pull request Oct 23, 2024
@aduh95 aduh95 mentioned this pull request Oct 24, 2024
louwers pushed a commit to louwers/node that referenced this pull request Nov 2, 2024
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. commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. doc Issues and PRs related to the documentations.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Handing spawn() stdio as socket wont duplex properly
5 participants