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

fix(node): track SIG* listeners in process.listeners #23890

Merged
merged 1 commit into from
May 20, 2024
Merged
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
3 changes: 3 additions & 0 deletions ext/node/polyfills/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,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 @@ -490,6 +491,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 @@ -533,6 +535,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 @@ -1103,3 +1103,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);
});