GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — SIGINT is not handled when a short-lived process is run in watch-mode · Issue #51466 · nodejs/node

SIGINT is not handled when a short-lived process is run in watch-mode #51466

New issue
Copy link
New issue
Copy link
Closed as not planned
Closed as not planned
SIGINT is not handled when a short-lived process is run in watch-mode#51466
Copy link
Labels
staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.watch-modeIssues and PRs related to watch mode.

Description

@azrsh
azrsh
opened on Jan 14, 2024
Issue body actions

Version

v20.11.0

Platform

Linux hostname 6.5.7-arch1-1 #\1 SMP PREEMPT_DYNAMIC Tue, 10 Oct 2023 21:10:21 +0000 x86_64 GNU/Linux

Subsystem

No response

What steps will reproduce the bug?

  1. Write some short-lived script and save it to index.js.
console.log("short-lived!");
  1. Run this script in watch-mode.
$ node --watch index.js
(node:1191877) ExperimentalWarning: Watch mode is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
short-lived!
Completed running 'index.js'

After following the above steps, you will no longer be able to terminate the Node.js process in watch-mode with SIGINT or SIGTERM.

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

If you follow the steps, it will always reproduce.

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

A Node.js process in watch-mode will be terminated if we send SIGINT or SIGTERM to the process, such as with Ctrl + C.

What do you see instead?

$ vim index.js
$ cat index.js
console.log("short-lived!");
$ node test.js
short-lived!
$ node --watch index.js
(node:1191877) ExperimentalWarning: Watch mode is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
short-lived!
Completed running 'index.js'
^C^C^C^C^C^C

I input Ctrl + C but the input is ignored.

Additional information

Mechanism of this behavior

The cause of this behavior is the killAndWait function located in /lib/internal/main/watch_mode.js:69.

Node.js process in watch-mode triggers a signal handler when it receives SIGINT or SIGTERM. In this signal handler, the killAndWait function is called as shown below.

node/lib/internal/main/watch_mode.js

Line 134 in c25878d

const exitCode = await killAndWait(signal, true);

At this time, since the argument force of the killAndWait function is set to true, processing continues without satisfying the condition in the code path below of the killAndWait function.

node/lib/internal/main/watch_mode.js

Lines 74 to 76 in c25878d

if ((child.killed || exited) && !force) {
return;
}

As a result, the killAndWait function will wait for a process that has already terminated to terminate. In other words, the wait never ends.

node/lib/internal/main/watch_mode.js

Lines 77 to 79 in c25878d

const onExit = once(child, 'exit');
child.kill(signal);
const { 0: exitCode } = await onExit;

My proposed modifications

Modify the killAndWait function so that if exited is true, return the killAndWait function regardless of the value of the argument force.
If you accept this modification, I can create a PR!

Thank you!

Activity

  1. azrsh
    added a commit that references this issue on Jan 14, 2024

    watch: handle signal correctly in short-lived process

    0d77811
  2. debadree25
    added
    watch-modeIssues and PRs related to watch mode.
    on Jan 15, 2024
  3. debadree25 commented on Jan 15, 2024

    @debadree25
    debadree25
    on Jan 15, 2024
    Contributor
    More actions

    Doing CtrlC is correctly terminating the process for me 🤔

    Reacted by Duncan Chiu and azarashi
  4. duncanchiu409 commented on Jan 15, 2024

    @duncanchiu409
    duncanchiu409
    on Jan 15, 2024
    Contributor
    More actions

    Ctrl C is terminating the process for me.

  5. azrsh commented on Jan 16, 2024

    @azrsh
    azrsh
    on Jan 16, 2024
    Author
    More actions

    Oh! The --watch option was working fine in my environment as well.

    Sorry, I had oversimplified the reproduction conditions and made a mistake. We can actually reproduce this by specifying the entry point script in --watch-path.

    $ vim index.js
    $ cat index.js
    console.log("test");
    $ node --watch-path index.js index.js
    (node:1512205) ExperimentalWarning: Watch mode is an experimental feature and might change at any time
    (Use `node --trace-warnings ...` to show where the warning was created)
    test
    Completed running 'test.js'
    ^C^C^C^C^C^C^C

    Also, this behavior only seems to work on Linux. Could not reproduce on Mac.

    Reacted by Duncan Chiu
  6. duncanchiu409 commented on Jan 16, 2024

    @duncanchiu409
    duncanchiu409
    on Jan 16, 2024
    Contributor
    More actions

    @azrsh Could re-produce the issue at v22.0.0-pre. However, I found your use of --watch-path is wrong. The correct command should be "node --watch-path=./ index.js". If you run my command, It should work as fine. Though, I could see a possible new issue on flag parsing. For example, not correct option on --watch-path should be stopped.

  7. duncanchiu409 commented on Jan 16, 2024

    @duncanchiu409
    duncanchiu409
    on Jan 16, 2024
    Contributor
    More actions

    Details on https://nodejs.org/docs/latest/api/cli.html#--watch-path

  8. azrsh commented on Jan 17, 2024

    @azrsh
    azrsh
    on Jan 17, 2024
    Author
    More actions

    In my environment, the command certainly works fine.

    Does it mean that specifying a file instead of a directory in --watch-path is wrong?
    I didn't find any such limitation written in the Node.js Command-line API documentation...

  9. github-actions commented on May 24, 2026

    @github-actions
    github-actionsBot
    on May 24, 2026 – with GitHub Actions
    Contributor
    More actions

    This issue has been marked as stale due to 210 days of inactivity.
    It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.

  10. github-actions
    added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on May 24, 2026
  11. github-actions commented on Jun 24, 2026

    @github-actions
    github-actionsBot
    on Jun 24, 2026 – with GitHub Actions
    Contributor
    More actions

    This issue has been automatically closed after 30 days of inactivity following its stale status (no activity for a total of 240 days).
    If this is still relevant, feel free to reopen it or leave a comment with additional details so we can continue the discussion.

  12. github-actions
    closed this as not plannedon Jun 24, 2026
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

    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.watch-modeIssues and PRs related to watch mode.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.