镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

--watch mode disables IPC communication between a spawning process and the actual child process #50880

Description

@znewsham

Version

v18.17.1

Platform

Linux zacknewsham-xps 6.2.0-33-generic #33~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Thu Sep 7 10:33:52 UTC 2 x86_64 x86_64 x86_64 GNU/Linux

Subsystem

No response

What steps will reproduce the bug?

This isn't quite a bug, nor is it quite a feature request, but somewhere in between when using watch mode.

in parent.js

import { spawn } from 'child_process';
spawn(
  'node',
  '--watch',
  'child.js'
);

child.sendMessage({ type: "whatever" }, console.log);

in child.js

process.on("message", console.log);

How often does it reproduce? Is there a required condition?

Always

What is the expected behavior? Why is that the expected behavior?

I expect to be able to communicate with a spawned child process, even when using --watch.

What do you see instead?

Because the --watch mode uses IPC itself (to listen to messages from the child process) it doesn't pass through messages from the child to the parent, or the parent to the child.

Additional information

As I said, this isn't quite a bug - but not quite a feature request either. I believe the fix is relatively simple, if the process has IPC enabled, then pass through messages (that aren't watch related) from the child -> parent and all messages from parent -> child

Activity

  1. znewsham commented on Nov 23, 2023

    @znewsham
    ContributorAuthor

    Ok, it turns out the fix is extremely simple, I'm happy to open a PR for this, I'm just not sure how to go about testing it. I think it's probably fine to require the parent process filter out unrelated (e.g., messages with watch: prefixed keys) IPC messages.

    in 'lib/internal/files_watcher.js', add:

      #setupIPC(child) {
        process.on("message", message => child.send(message));
        child.on("message", message => process.send(message));
      }
    

    then modify constructor to have:

      #wantsPassthroughIPC = false;
    
      constructor({ throttle = 500, mode = 'filter' } = kEmptyObject) {
        super();
        this.#wantsPassthroughIPC = !!process.send;
    

    then modify watchChildProcessModules to have:

      watchChildProcessModules(child, key = null) {
        if (this.#wantsPassthroughIPC) {
          this.#setupIPC(child);
        }
    
  2. znewsham commented on Nov 23, 2023

    @znewsham
    ContributorAuthor

    @MoLow not sure what your thoughts are on this

  3. MoLow commented on Nov 23, 2023

    @MoLow
    Member

    Sound like a great solution,
    for testing I would just add an extra case to https://github.com/nodejs/node/blob/main/test/sequential/test-watch-mode.mjs
    with a fixture printing messages, validating it receives messages

  4. added 2 commits that reference this issue on Nov 24, 2023
    ad21317
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions