Skip to content

Commit

Permalink
fix(node): track SIG* listeners in process.listeners (#23890)
Browse files Browse the repository at this point in the history
Some npm libraries like `signal-exit` rely on the length of the listener
array returned by `process.listeners("SIGNT")` to be correct to
function. We weren't tracking `SIG*` events there, which broke those npm
libraries.

Fixes #22892
  • Loading branch information
marvinhagemeister committed May 20, 2024
1 parent d7709da commit fb3f82b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ext/node/polyfills/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,7 @@ Process.prototype.on = function (
} else if (event === "SIGTERM" && Deno.build.os === "windows") {
// Ignores SIGTERM on windows.
} else {
EventEmitter.prototype.on.call(this, event, listener);
Deno.addSignalListener(event as Deno.Signal, listener);
}
} else {
Expand All @@ -494,6 +495,7 @@ Process.prototype.off = function (
} else if (event === "SIGTERM" && Deno.build.os === "windows") {
// Ignores SIGTERM on windows.
} else {
EventEmitter.prototype.off.call(this, event, listener);
Deno.removeSignalListener(event as Deno.Signal, listener);
}
} else {
Expand Down Expand Up @@ -537,6 +539,7 @@ Process.prototype.prependListener = function (
if (event === "SIGBREAK" && Deno.build.os !== "windows") {
// Ignores SIGBREAK if the platform is not windows.
} else {
EventEmitter.prototype.prependListener.call(this, event, listener);
Deno.addSignalListener(event as Deno.Signal, listener);
}
} else {
Expand Down
16 changes: 16 additions & 0 deletions tests/unit_node/process_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1108,3 +1108,19 @@ Deno.test({
process.constructor.call({});
},
});

// Test for https://github.com/denoland/deno/issues/22892
Deno.test("process.listeners - include SIG* events", () => {
const listener = () => console.log("SIGINT");
process.on("SIGINT", listener);
assertEquals(process.listeners("SIGINT").length, 1);

const listener2 = () => console.log("SIGINT");
process.prependListener("SIGINT", listener2);
assertEquals(process.listeners("SIGINT").length, 2);

process.off("SIGINT", listener);
assertEquals(process.listeners("SIGINT").length, 1);
process.off("SIGINT", listener2);
assertEquals(process.listeners("SIGINT").length, 0);
});

0 comments on commit fb3f82b

Please sign in to comment.