Skip to content

discussion about how to fix child_process 'spawn' not ready to send messages on ESM #41134

Description

@ErickWendel

BTW: I'd like to help fixing this bug 🤩

Is your feature request related to a problem? Please describe.

There's a current issue on spawn's child processes using ES modules mentioned in those issues (#37782, #39140, #39140, #34785, and help/issues/1383 ).

When you fork a file and immediately use the .send event. The child process doesn't receive messages because it's not ready yet.

So we need to make a workaround. The child emits an event, the parent waits for .on('message', we check the message and then we start sending messages to the child.

Describe the solution you'd like
My plan initially was to add an event like child.on("ready to specify when we can start sending messages to the child process.

I spoke to @addaleax and she suggested a behavior that is closer to what MessagePorts expose, which is queueing up messages until a message listener is installed.

I know that it would increase implementation complexity a bit, but it would be very helpful for avoiding pitfalls like the ones linked here

What do you think? Any ideas of what could we do?

Activity

  1. changed the title [-]discussion about how to fix child_process 'spawn' event is emitted too soon on ESM[/-] [+]discussion about how to fix child_process 'spawn' not ready to send messages on ESM[/+] on Dec 10, 2021
  2. added
    child_processIssues and PRs related to the child_process subsystem.
    on Dec 10, 2021
  3. Trott commented on Dec 12, 2021

    @Trott
    Member

    Seems like a fine idea to me but would want to hear what @nodejs/modules and/or @nodejs/child_process folks might have to say about it.

  4. bmeck commented on Dec 12, 2021

    @bmeck
    Member

    I'd prefer if we could queue things because this problem isn't unique to ESM and I've seen it in the long past using purely CJS and callbacks.

  5. ErickWendel commented on Dec 13, 2021

    @ErickWendel
    MemberAuthor

    I'd prefer if we could queue things because this problem isn't unique to ESM and I've seen it in the long past using purely CJS and callbacks.

    I think I didn't get it. What do you mean by queue things?

  6. gabrielsimas commented on Dec 13, 2021

    @gabrielsimas

    I'd prefer if we could queue things because this problem isn't unique to ESM and I've seen it in the long past using purely CJS and callbacks.

    I don't get it too... do you can explain please?

  7. bmeck commented on Dec 13, 2021

    @bmeck
    Member

    @gabrielsimas MessagePort will queue messages (events) and wait for a listener before draining the messages. The problem in this issue is that event listeners which are used in child_process are not queueing messages and fire messages that the child process has loaded prior to the child process attaching any listeners to handle events.

  8. weritontmachado commented on Dec 13, 2021

    @weritontmachado

    I spoke to @addaleax and she suggested a behavior that is closer to what MessagePorts expose, which is queueing up messages until a message listener is installed.

    This was my first thought

  9. ErickWendel commented on Dec 13, 2021

    @ErickWendel
    MemberAuthor

    @gabrielsimas MessagePort will queue messages (events) and wait for a listener before draining the messages. The problem in this issue is that event listeners which are used in child_process are not queueing messages and fire messages that the child process has loaded prior to the child process attaching any listeners to handle events.

    niiice! It's the same strategy @addaleax suggested on the post, right?

  10. ktfth commented on Dec 13, 2021

    @ktfth

    You have a reproduction of the behavior @ErickWendel?

  11. ktfth commented on Dec 13, 2021

    @ktfth

    Or the behavior is the same on the related content you shared?

  12. ErickWendel commented on Dec 13, 2021

    @ErickWendel
    MemberAuthor

    Or the behavior is the same on the related content you shared?

    Yes, Actually they already had reproduced the behavior at all of the issues I mentioned in the post. See this one #39140

  13. mcollina commented on Dec 14, 2021

    @mcollina
    SponsorMember

    I'd prefer if we could queue things because this problem isn't unique to ESM and I've seen it in the long past using purely CJS and callbacks.

    This is the way to go

  14. 11 remaining items

  15. added a commit that references this issue on Dec 30, 2021
  16. added a commit that references this issue on Jan 14, 2022
  17. Peeja commented on Jan 29, 2022

    @Peeja

    For future searchers: This has been resolved and released in v17.4.0. (🎉)

  18. Methuselah96 commented on Mar 3, 2022

    @Methuselah96

    Also released in 16.14.0 if you're on LTS.

  19. added a commit that references this issue on May 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    child_processIssues and PRs related to the child_process subsystem.feature requestIssues requesting new Node.js features.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions