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

Can't test signal handling #10374

Closed
glasser opened this issue Aug 6, 2020 · 4 comments
Closed

Can't test signal handling #10374

glasser opened this issue Aug 6, 2020 · 4 comments

Comments

@glasser
Copy link

glasser commented Aug 6, 2020

🐛 Bug Report

I want to be able to test my code's signal handling by registering a handle with process.on('SIGINT') and process.kill(process.pid, 'SIGINT') but it kills my jest process. (Or using a signal like SIGUSR1 shows that the signal is ignored.)

To Reproduce

I made a gist that reproduces the behavior. I am testing on a Mac with Node v14.10. Note that using console.log doesn't work due to the process crashing early, so I'm "logging" via appendFileSync to /tmp/test.out and my test script clears the file before the test and cats it after. (It also shows the exit code, which is 130 = 128 + 2 = SIGINT.)

glasser@dsg-mbp 0 08:40:44 /tmp $ git clone https://gist.github.com/cc6e7b429d8c388ac5de970dac805e11.git jest-signals
[...]
glasser@dsg-mbp 0 08:40:49 /tmp $ cd jest-signals
glasser@dsg-mbp 0 08:40:50 /tmp/jest-signals master u= p= $ cat signals.test.js
const { appendFileSync } = require('fs');

function log(x) {
  appendFileSync('/tmp/test.out', `${x}\n`);
}

it('will not kill jest', () => {
  log(`starting ${process.listenerCount('SIGINT')}`);
  process.once('SIGINT', () => {});
  log(`set up handler ${process.listenerCount('SIGINT')}`);
  process.kill(process.pid, 'SIGINT');
  log(`killed`);
});
glasser@dsg-mbp 0 08:40:54 /tmp/jest-signals master u= p= $ npm it
npm WARN normal-jest@1.0.0 No description
npm WARN normal-jest@1.0.0 No repository field.

added 507 packages from 346 contributors and audited 507 packages in 4.8s

20 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities


> normal-jest@1.0.0 test /private/tmp/jest-signals
> >/tmp/test.out; jest; echo exit code $?; cat /tmp/test.out

exit code 130
starting 0
set up handler 1

Expected behavior

I expect the signal to be handled by the handler I've installed (which even shows up when I check process.listenerCount), for the process to not be killed, for the test to succeed, and for "killed" to appear in the log file.

Link to repl or repo (highly encouraged)

https://gist.github.com/glasser/cc6e7b429d8c388ac5de970dac805e11

envinfo

  System:
    OS: macOS Mojave 10.14.6
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
  Binaries:
    Node: 14.1.0 - /usr/local/bin/node
    Yarn: 1.22.4 - /usr/local/bin/yarn
    npm: 6.14.4 - /usr/local/bin/npm
  npmPackages:
    jest: ^26.2.2 => 26.2.2 
@glasser
Copy link
Author

glasser commented Aug 6, 2020

Something that I'm seeing is that process.listenerCount('newListener') is 0 inside my test instead of 1 without jest. That sounds like the special setup on process to turn listening on a signal in JS into actual signal handling has gotten disconnected somehow?

That's set up in this is_main_thread file: https://github.com/nodejs/node/blob/v14.1.0/lib/internal/bootstrap/switches/is_main_thread.js#L29
and removed in is_not_main_thread:
https://github.com/nodejs/node/blob/v14.1.0/lib/internal/bootstrap/switches/is_not_main_thread.js#L27

So maybe jest is doing something odd with threads? On the other hand, when I log process._debugProcess I get an actual function, which matches the assignment at the top of is_main_thread and not the delete at the top of is_not_main_thread.

@github-actions
Copy link

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days.

@github-actions github-actions bot added the Stale label Feb 17, 2023
@github-actions
Copy link

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Mar 19, 2023
@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants