2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2021-11-23 11:45:18 -05:00
|
|
|
import { assertEquals, assertThrows, deferred, 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 =
|
|
|
|
"Windows only supports ctrl-c (SIGINT) and ctrl-break (SIGBREAK).";
|
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,
|
2022-06-13 16:39:46 -04:00
|
|
|
msg,
|
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,
|
2022-06-13 16:39:46 -04:00
|
|
|
msg,
|
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,
|
2022-06-13 16:39:46 -04:00
|
|
|
msg,
|
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,
|
2022-06-13 16:39:46 -04:00
|
|
|
msg,
|
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,
|
2022-06-13 16:39:46 -04:00
|
|
|
msg,
|
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,
|
2022-06-13 16:39:46 -04:00
|
|
|
msg,
|
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,
|
2022-06-13 16:39:46 -04:00
|
|
|
msg,
|
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,
|
2022-06-13 16:39:46 -04:00
|
|
|
msg,
|
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,
|
2022-06-13 16:39:46 -04:00
|
|
|
msg,
|
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,
|
2022-06-13 16:39:46 -04:00
|
|
|
msg,
|
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => Deno.addSignalListener("SIGKILL", () => {}),
|
|
|
|
Error,
|
|
|
|
msg,
|
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => Deno.addSignalListener("SIGSTOP", () => {}),
|
|
|
|
Error,
|
|
|
|
msg,
|
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => Deno.addSignalListener("SIGILL", () => {}),
|
|
|
|
Error,
|
|
|
|
msg,
|
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => Deno.addSignalListener("SIGFPE", () => {}),
|
|
|
|
Error,
|
|
|
|
msg,
|
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => Deno.addSignalListener("SIGSEGV", () => {}),
|
|
|
|
Error,
|
|
|
|
msg,
|
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-11-26 11:22:36 -05:00
|
|
|
const resolvable = deferred();
|
2020-01-24 08:15:31 -05:00
|
|
|
let c = 0;
|
2021-10-25 23:03:38 -04:00
|
|
|
const listener = () => {
|
|
|
|
c += 1;
|
|
|
|
};
|
|
|
|
Deno.addSignalListener("SIGUSR1", listener);
|
2020-01-24 08:15:31 -05:00
|
|
|
setTimeout(async () => {
|
2021-10-25 23:03:38 -04:00
|
|
|
// Sends SIGUSR1 3 times.
|
2020-01-24 08:15:31 -05:00
|
|
|
for (const _ of Array(3)) {
|
2021-06-24 21:44:14 -04:00
|
|
|
await delay(20);
|
2021-10-25 23:03:38 -04:00
|
|
|
Deno.kill(Deno.pid, "SIGUSR1");
|
2020-01-24 08:15:31 -05:00
|
|
|
}
|
2021-10-25 23:03:38 -04:00
|
|
|
await delay(20);
|
|
|
|
Deno.removeSignalListener("SIGUSR1", listener);
|
2020-03-03 12:22:53 -05:00
|
|
|
resolvable.resolve();
|
2020-01-24 08:15:31 -05:00
|
|
|
});
|
|
|
|
|
2021-10-25 23:03:38 -04:00
|
|
|
await resolvable;
|
2020-01-24 08:15:31 -05:00
|
|
|
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() {
|
|
|
|
const resolvable = deferred();
|
|
|
|
let c = "";
|
|
|
|
const listener0 = () => {
|
|
|
|
c += "0";
|
|
|
|
};
|
|
|
|
const listener1 = () => {
|
|
|
|
c += "1";
|
|
|
|
};
|
|
|
|
Deno.addSignalListener("SIGUSR2", listener0);
|
|
|
|
Deno.addSignalListener("SIGUSR2", listener1);
|
|
|
|
setTimeout(async () => {
|
|
|
|
// Sends SIGUSR2 3 times.
|
|
|
|
for (const _ of Array(3)) {
|
|
|
|
await delay(20);
|
|
|
|
Deno.kill(Deno.pid, "SIGUSR2");
|
|
|
|
}
|
|
|
|
await delay(20);
|
|
|
|
Deno.removeSignalListener("SIGUSR2", listener1);
|
|
|
|
// Sends SIGUSR2 3 times.
|
|
|
|
for (const _ of Array(3)) {
|
|
|
|
await delay(20);
|
|
|
|
Deno.kill(Deno.pid, "SIGUSR2");
|
|
|
|
}
|
|
|
|
await delay(20);
|
|
|
|
// Sends SIGUSR1 (irrelevant signal) 3 times.
|
|
|
|
for (const _ of Array(3)) {
|
|
|
|
await delay(20);
|
|
|
|
Deno.kill(Deno.pid, "SIGUSR1");
|
|
|
|
}
|
|
|
|
await delay(20);
|
|
|
|
Deno.removeSignalListener("SIGUSR2", listener0);
|
|
|
|
resolvable.resolve();
|
|
|
|
});
|
2020-01-24 08:15:31 -05:00
|
|
|
|
2020-03-03 12:22:53 -05:00
|
|
|
await resolvable;
|
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",
|
|
|
|
"--unstable",
|
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",
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|