From afe1a20b9173bb0e2267254acf2c1b982c13df01 Mon Sep 17 00:00:00 2001 From: chenyuebiao Date: Sun, 26 Apr 2026 23:15:29 +0800 Subject: [PATCH] lib: add signalMonitor event for process --- doc/api/process.md | 11 +++++++++++ lib/events.js | 5 +++++ .../test-event-emitter-signal-monitor.js | 18 ++++++++++++++++++ 3 files changed, 34 insertions(+) create mode 100644 test/parallel/test-event-emitter-signal-monitor.js diff --git a/doc/api/process.md b/doc/api/process.md index 61ea3c673e5f19..411084f588bf5e 100644 --- a/doc/api/process.md +++ b/doc/api/process.md @@ -625,6 +625,17 @@ added: The `'worker'` event is emitted after a new {Worker} thread has been created. +### Event: `'signalMonitor'` + + + +* `signal` {string} The signal name, such as `SIGINT`. + +The `'signalMonitor'` event is emitted after a signal is received. + ### Signal events diff --git a/lib/events.js b/lib/events.js index c6fc170d590aea..b72eaa61f1bfe1 100644 --- a/lib/events.js +++ b/lib/events.js @@ -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'); @@ -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; diff --git a/test/parallel/test-event-emitter-signal-monitor.js b/test/parallel/test-event-emitter-signal-monitor.js new file mode 100644 index 00000000000000..53c40c1c0715b0 --- /dev/null +++ b/test/parallel/test-event-emitter-signal-monitor.js @@ -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');