Skip to content

Commit

Permalink
process: clean up signal handler setup
Browse files Browse the repository at this point in the history
PR-URL: nodejs#18330
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Jon Moss <me@jonathanmoss.me>
  • Loading branch information
apapirovski authored and maclover7 committed Jan 26, 2018
1 parent d8c0f2d commit a5a8118
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions lib/internal/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,24 +178,23 @@ function setupKillAndExit() {


function setupSignalHandlers() {
// Load events module in order to access prototype elements on process like
// process.addListener.
const signalWraps = {};
const signalWraps = Object.create(null);
let Signal;

function isSignal(event) {
return typeof event === 'string' && constants[event] !== undefined;
}

// Detect presence of a listener for the special signal types
process.on('newListener', function(type, listener) {
if (isSignal(type) &&
!signalWraps.hasOwnProperty(type)) {
const Signal = process.binding('signal_wrap').Signal;
process.on('newListener', function(type) {
if (isSignal(type) && signalWraps[type] === undefined) {
if (Signal === undefined)
Signal = process.binding('signal_wrap').Signal;
const wrap = new Signal();

wrap.unref();

wrap.onsignal = function() { process.emit(type, type); };
wrap.onsignal = process.emit.bind(process, type, type);

const signum = constants[type];
const err = wrap.start(signum);
Expand All @@ -208,8 +207,8 @@ function setupSignalHandlers() {
}
});

process.on('removeListener', function(type, listener) {
if (signalWraps.hasOwnProperty(type) && this.listenerCount(type) === 0) {
process.on('removeListener', function(type) {
if (signalWraps[type] !== undefined && this.listenerCount(type) === 0) {
signalWraps[type].close();
delete signalWraps[type];
}
Expand Down

0 comments on commit a5a8118

Please sign in to comment.