2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-11-10 16:29:09 -05:00
|
|
|
import { assertEquals, assertThrows, delay } from "./test_util.ts";
|
2020-01-24 08:15:31 -05:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2020-04-28 12:35:23 -04:00
|
|
|
{ ignore: Deno.build.os !== "windows" },
|
2021-08-05 07:08:58 -04:00
|
|
|
function signalsNotImplemented() {
|
2022-06-13 16:39:46 -04:00
|
|
|
const msg =
|
2024-10-22 04:41:08 -04:00
|
|
|
"Windows only supports ctrl-c (SIGINT) and ctrl-break (SIGBREAK), but got ";
|
2020-01-24 08:15:31 -05:00
|
|
|
assertThrows(
|
|
|
|
() => {
|
2021-10-25 23:03:38 -04:00
|
|
|
Deno.addSignalListener("SIGALRM", () => {});
|
2020-01-24 08:15:31 -05:00
|
|
|
},
|
|
|
|
Error,
|
2024-10-22 04:41:08 -04:00
|
|
|
msg + "SIGALRM",
|
2020-01-24 08:15:31 -05:00
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
2021-10-25 23:03:38 -04:00
|
|
|
Deno.addSignalListener("SIGCHLD", () => {});
|
2020-01-24 08:15:31 -05:00
|
|
|
},
|
|
|
|
Error,
|
2024-10-22 04:41:08 -04:00
|
|
|
msg + "SIGCHLD",
|
2020-01-24 08:15:31 -05:00
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
2021-10-25 23:03:38 -04:00
|
|
|
Deno.addSignalListener("SIGHUP", () => {});
|
2020-01-24 08:15:31 -05:00
|
|
|
},
|
|
|
|
Error,
|
2024-10-22 04:41:08 -04:00
|
|
|
msg + "SIGHUP",
|
2020-01-24 08:15:31 -05:00
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
2021-10-25 23:03:38 -04:00
|
|
|
Deno.addSignalListener("SIGIO", () => {});
|
2020-01-24 08:15:31 -05:00
|
|
|
},
|
|
|
|
Error,
|
2024-10-22 04:41:08 -04:00
|
|
|
msg + "SIGIO",
|
2020-01-24 08:15:31 -05:00
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
2021-10-25 23:03:38 -04:00
|
|
|
Deno.addSignalListener("SIGPIPE", () => {});
|
2020-01-24 08:15:31 -05:00
|
|
|
},
|
|
|
|
Error,
|
2024-10-22 04:41:08 -04:00
|
|
|
msg + "SIGPIPE",
|
2020-01-24 08:15:31 -05:00
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
2021-10-25 23:03:38 -04:00
|
|
|
Deno.addSignalListener("SIGQUIT", () => {});
|
2020-01-24 08:15:31 -05:00
|
|
|
},
|
|
|
|
Error,
|
2024-10-22 04:41:08 -04:00
|
|
|
msg + "SIGQUIT",
|
2020-01-24 08:15:31 -05:00
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
2021-10-25 23:03:38 -04:00
|
|
|
Deno.addSignalListener("SIGTERM", () => {});
|
2020-01-24 08:15:31 -05:00
|
|
|
},
|
|
|
|
Error,
|
2024-10-22 04:41:08 -04:00
|
|
|
msg + "SIGTERM",
|
2020-01-24 08:15:31 -05:00
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
2021-10-25 23:03:38 -04:00
|
|
|
Deno.addSignalListener("SIGUSR1", () => {});
|
2020-01-24 08:15:31 -05:00
|
|
|
},
|
|
|
|
Error,
|
2024-10-22 04:41:08 -04:00
|
|
|
msg + "SIGUSR1",
|
2020-01-24 08:15:31 -05:00
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
2021-10-25 23:03:38 -04:00
|
|
|
Deno.addSignalListener("SIGUSR2", () => {});
|
2020-01-24 08:15:31 -05:00
|
|
|
},
|
|
|
|
Error,
|
2024-10-22 04:41:08 -04:00
|
|
|
msg + "SIGUSR2",
|
2020-01-24 08:15:31 -05:00
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
2021-10-25 23:03:38 -04:00
|
|
|
Deno.addSignalListener("SIGWINCH", () => {});
|
2020-01-24 08:15:31 -05:00
|
|
|
},
|
|
|
|
Error,
|
2024-10-22 04:41:08 -04:00
|
|
|
msg + "SIGWINCH",
|
2022-06-13 16:39:46 -04:00
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => Deno.addSignalListener("SIGKILL", () => {}),
|
|
|
|
Error,
|
2024-10-22 04:41:08 -04:00
|
|
|
msg + "SIGKILL",
|
2022-06-13 16:39:46 -04:00
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => Deno.addSignalListener("SIGSTOP", () => {}),
|
|
|
|
Error,
|
2024-10-22 04:41:08 -04:00
|
|
|
msg + "SIGSTOP",
|
2022-06-13 16:39:46 -04:00
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => Deno.addSignalListener("SIGILL", () => {}),
|
|
|
|
Error,
|
2024-10-22 04:41:08 -04:00
|
|
|
msg + "SIGILL",
|
2022-06-13 16:39:46 -04:00
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => Deno.addSignalListener("SIGFPE", () => {}),
|
|
|
|
Error,
|
2024-10-22 04:41:08 -04:00
|
|
|
msg + "SIGFPE",
|
2022-06-13 16:39:46 -04:00
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => Deno.addSignalListener("SIGSEGV", () => {}),
|
|
|
|
Error,
|
2024-10-22 04:41:08 -04:00
|
|
|
msg + "SIGSEGV",
|
2020-01-24 08:15:31 -05:00
|
|
|
);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{
|
|
|
|
ignore: Deno.build.os === "windows",
|
2021-10-25 23:03:38 -04:00
|
|
|
permissions: { run: true },
|
2021-09-22 19:50:50 -04:00
|
|
|
},
|
2021-10-25 23:03:38 -04:00
|
|
|
async function signalListenerTest() {
|
2020-01-24 08:15:31 -05:00
|
|
|
let c = 0;
|
2021-10-25 23:03:38 -04:00
|
|
|
const listener = () => {
|
|
|
|
c += 1;
|
|
|
|
};
|
2024-01-03 09:41:58 -05:00
|
|
|
// This test needs to be careful that it doesn't accidentally aggregate multiple
|
|
|
|
// signals into one. Sending two or more SIGxxx before the handler can be run will
|
|
|
|
// result in signal coalescing.
|
2021-10-25 23:03:38 -04:00
|
|
|
Deno.addSignalListener("SIGUSR1", listener);
|
2024-01-03 09:41:58 -05:00
|
|
|
// Sends SIGUSR1 3 times.
|
|
|
|
for (let i = 1; i <= 3; i++) {
|
|
|
|
await delay(1);
|
|
|
|
Deno.kill(Deno.pid, "SIGUSR1");
|
|
|
|
while (c < i) {
|
2021-06-24 21:44:14 -04:00
|
|
|
await delay(20);
|
2020-01-24 08:15:31 -05:00
|
|
|
}
|
2023-11-13 13:45:48 -05:00
|
|
|
}
|
2024-01-03 09:41:58 -05:00
|
|
|
Deno.removeSignalListener("SIGUSR1", listener);
|
|
|
|
await delay(100);
|
|
|
|
assertEquals(c, 3);
|
2021-10-25 23:03:38 -04:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-10-25 23:03:38 -04:00
|
|
|
{
|
|
|
|
ignore: Deno.build.os === "windows",
|
|
|
|
permissions: { run: true },
|
|
|
|
},
|
|
|
|
async function multipleSignalListenerTest() {
|
|
|
|
let c = "";
|
|
|
|
const listener0 = () => {
|
|
|
|
c += "0";
|
|
|
|
};
|
|
|
|
const listener1 = () => {
|
|
|
|
c += "1";
|
|
|
|
};
|
2024-01-03 09:41:58 -05:00
|
|
|
// This test needs to be careful that it doesn't accidentally aggregate multiple
|
|
|
|
// signals into one. Sending two or more SIGxxx before the handler can be run will
|
|
|
|
// result in signal coalescing.
|
2021-10-25 23:03:38 -04:00
|
|
|
Deno.addSignalListener("SIGUSR2", listener0);
|
|
|
|
Deno.addSignalListener("SIGUSR2", listener1);
|
2024-01-03 09:41:58 -05:00
|
|
|
|
|
|
|
// Sends SIGUSR2 3 times.
|
|
|
|
for (let i = 1; i <= 3; i++) {
|
|
|
|
await delay(1);
|
|
|
|
Deno.kill(Deno.pid, "SIGUSR2");
|
|
|
|
while (c.length < i * 2) {
|
2023-11-13 13:45:48 -05:00
|
|
|
await delay(20);
|
|
|
|
}
|
2024-01-03 09:41:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Deno.removeSignalListener("SIGUSR2", listener1);
|
|
|
|
|
|
|
|
// Sends SIGUSR2 3 times.
|
|
|
|
for (let i = 1; i <= 3; i++) {
|
|
|
|
await delay(1);
|
|
|
|
Deno.kill(Deno.pid, "SIGUSR2");
|
|
|
|
while (c.length < 6 + i) {
|
2021-10-25 23:03:38 -04:00
|
|
|
await delay(20);
|
|
|
|
}
|
2024-01-03 09:41:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sends SIGUSR1 (irrelevant signal) 3 times.
|
2024-03-11 13:22:28 -04:00
|
|
|
// By default SIGUSR1 terminates, so set it to a no-op for this test.
|
|
|
|
let count = 0;
|
|
|
|
const irrelevant = () => {
|
|
|
|
count++;
|
|
|
|
};
|
|
|
|
Deno.addSignalListener("SIGUSR1", irrelevant);
|
2024-01-03 09:41:58 -05:00
|
|
|
for (const _ of Array(3)) {
|
2021-10-25 23:03:38 -04:00
|
|
|
await delay(20);
|
2024-01-03 09:41:58 -05:00
|
|
|
Deno.kill(Deno.pid, "SIGUSR1");
|
|
|
|
}
|
2024-03-11 13:22:28 -04:00
|
|
|
while (count < 3) {
|
|
|
|
await delay(20);
|
|
|
|
}
|
|
|
|
Deno.removeSignalListener("SIGUSR1", irrelevant);
|
2023-11-13 13:45:48 -05:00
|
|
|
|
2024-01-03 09:41:58 -05:00
|
|
|
// No change
|
|
|
|
assertEquals(c, "010101000");
|
|
|
|
|
|
|
|
Deno.removeSignalListener("SIGUSR2", listener0);
|
2023-11-13 13:45:48 -05:00
|
|
|
|
2024-01-03 09:41:58 -05:00
|
|
|
await delay(100);
|
2020-01-24 08:15:31 -05:00
|
|
|
|
2021-10-25 23:03:38 -04:00
|
|
|
// The first 3 events are handled by both handlers
|
|
|
|
// The last 3 events are handled only by handler0
|
|
|
|
assertEquals(c, "010101000");
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2020-01-24 08:15:31 -05:00
|
|
|
|
2021-06-25 00:15:35 -04:00
|
|
|
// This tests that pending op_signal_poll doesn't block the runtime from exiting the process.
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{
|
|
|
|
permissions: { run: true, read: true },
|
|
|
|
},
|
2021-10-25 23:03:38 -04:00
|
|
|
async function canExitWhileListeningToSignal() {
|
2022-12-02 08:43:17 -05:00
|
|
|
const { code } = await new Deno.Command(Deno.execPath(), {
|
2022-05-18 16:00:11 -04:00
|
|
|
args: [
|
2021-06-25 00:15:35 -04:00
|
|
|
"eval",
|
2022-06-13 16:39:46 -04:00
|
|
|
"Deno.addSignalListener('SIGINT', () => {})",
|
2021-06-25 00:15:35 -04:00
|
|
|
],
|
2022-12-02 08:43:17 -05:00
|
|
|
}).output();
|
2022-07-18 09:16:12 -04:00
|
|
|
assertEquals(code, 0);
|
2021-06-25 00:15:35 -04:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-10-25 23:03:38 -04:00
|
|
|
{
|
2022-06-13 16:39:46 -04:00
|
|
|
ignore: Deno.build.os !== "windows",
|
2021-10-25 23:03:38 -04:00
|
|
|
permissions: { run: true },
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2022-06-13 16:39:46 -04:00
|
|
|
function windowsThrowsOnNegativeProcessIdTest() {
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
2022-06-14 15:31:50 -04:00
|
|
|
Deno.kill(-1, "SIGKILL");
|
2022-06-13 16:39:46 -04:00
|
|
|
},
|
|
|
|
TypeError,
|
2022-06-14 15:31:50 -04:00
|
|
|
"Invalid pid",
|
2022-06-13 16:39:46 -04:00
|
|
|
);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2022-01-04 15:55:06 -05:00
|
|
|
|
2022-06-13 16:39:46 -04:00
|
|
|
Deno.test(
|
|
|
|
{
|
|
|
|
ignore: Deno.build.os !== "windows",
|
|
|
|
permissions: { run: true },
|
|
|
|
},
|
|
|
|
function noOpenSystemIdleProcessTest() {
|
|
|
|
let signal: Deno.Signal = "SIGKILL";
|
|
|
|
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
Deno.kill(0, signal);
|
|
|
|
},
|
|
|
|
TypeError,
|
2022-06-14 15:31:50 -04:00
|
|
|
`Invalid pid`,
|
2022-06-13 16:39:46 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
signal = "SIGTERM";
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
Deno.kill(0, signal);
|
|
|
|
},
|
|
|
|
TypeError,
|
2022-06-14 15:31:50 -04:00
|
|
|
`Invalid pid`,
|
2022-06-13 16:39:46 -04:00
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Deno.test(function signalInvalidHandlerTest() {
|
|
|
|
assertThrows(() => {
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
Deno.addSignalListener("SIGINT", "handler" as any);
|
|
|
|
});
|
|
|
|
assertThrows(() => {
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
Deno.removeSignalListener("SIGINT", "handler" as any);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-01-04 15:55:06 -05:00
|
|
|
Deno.test(
|
|
|
|
{
|
|
|
|
ignore: Deno.build.os === "windows",
|
|
|
|
permissions: { run: true },
|
|
|
|
},
|
|
|
|
function signalForbiddenSignalTest() {
|
|
|
|
assertThrows(
|
|
|
|
() => Deno.addSignalListener("SIGKILL", () => {}),
|
|
|
|
TypeError,
|
|
|
|
"Binding to signal 'SIGKILL' is not allowed",
|
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => Deno.addSignalListener("SIGSTOP", () => {}),
|
|
|
|
TypeError,
|
|
|
|
"Binding to signal 'SIGSTOP' is not allowed",
|
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => Deno.addSignalListener("SIGILL", () => {}),
|
|
|
|
TypeError,
|
|
|
|
"Binding to signal 'SIGILL' is not allowed",
|
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => Deno.addSignalListener("SIGFPE", () => {}),
|
|
|
|
TypeError,
|
|
|
|
"Binding to signal 'SIGFPE' is not allowed",
|
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => Deno.addSignalListener("SIGSEGV", () => {}),
|
|
|
|
TypeError,
|
|
|
|
"Binding to signal 'SIGSEGV' is not allowed",
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
2024-06-19 04:11:09 -04:00
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ ignore: Deno.build.os !== "linux" },
|
|
|
|
function signalAliasLinux() {
|
|
|
|
const i = () => {};
|
|
|
|
Deno.addSignalListener("SIGUNUSED", i);
|
|
|
|
Deno.addSignalListener("SIGPOLL", i);
|
|
|
|
|
|
|
|
Deno.removeSignalListener("SIGUNUSED", i);
|
|
|
|
Deno.removeSignalListener("SIGPOLL", i);
|
|
|
|
},
|
|
|
|
);
|