Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions doc/api/process.md
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,17 @@ added:

The `'worker'` event is emitted after a new {Worker} thread has been created.

### Event: `'signalMonitor'`

<!-- YAML
added:
- REPLACEME
-->

* `signal` {string} The signal name, such as `SIGINT`.

The `'signalMonitor'` event is emitted after a signal is received.

### Signal events

<!--type=event-->
Expand Down
5 changes: 5 additions & 0 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ const {
validateString,
} = require('internal/validators');
const { addAbortListener } = require('internal/events/abort_listener');
const { signals } = internalBinding('constants').os;

const kCapture = Symbol('kCapture');
const kErrorMonitor = Symbol('events.errorMonitor');
Expand Down Expand Up @@ -454,6 +455,10 @@ function enhanceStackTrace(err, own) {
* @returns {boolean}
*/
EventEmitter.prototype.emit = function emit(type, ...args) {
if (this === process && signals[type] !== undefined) {
process.emit('signalMonitor', type);
}

let doError = (type === 'error');

const events = this._events;
Expand Down
18 changes: 18 additions & 0 deletions test/parallel/test-event-emitter-signal-monitor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const EventEmitter = require('events');

// Test that EventEmitter.signalMonitor is not called on EE.
const EE = new EventEmitter();
EE.on('signalMonitor', common.mustNotCall());
EE.emit('SIGINT');

// Test that EventEmitter.signalMonitor is called on process.
process.on('signalMonitor', common.mustCall(function(signal) {
assert.strictEqual(signal, 'SIGINT');
}, 1));
process.emit('SIGINT');

// Test that EventEmitter.signalMonitor is not called on other events.
process.emit('test');
Loading