Skip to content

Commit

Permalink
BREAKING(unstable): Remove Deno.Signals enum, Deno.signals.* (#11909)
Browse files Browse the repository at this point in the history
  • Loading branch information
ry authored Sep 6, 2021
1 parent b7c2902 commit c132c86
Show file tree
Hide file tree
Showing 12 changed files with 187 additions and 380 deletions.
3 changes: 0 additions & 3 deletions cli/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,14 @@ const UNSTABLE_DENO_PROPS: &[&str] = &[
"EmitOptions",
"EmitResult",
"HttpClient",
"LinuxSignal",
"Location",
"MXRecord",
"MacOSSignal",
"Metrics",
"OpMetrics",
"RecordType",
"ResolveDnsOptions",
"SRVRecord",
"SetRawOptions",
"Signal",
"SignalStream",
"StartTlsOptions",
"SystemMemoryInfo",
Expand Down
5 changes: 2 additions & 3 deletions cli/dts/lib.deno.ns.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1982,13 +1982,12 @@ declare namespace Deno {
stderrOutput(): Promise<Uint8Array>;
close(): void;

/** **UNSTABLE**: The `signo` argument may change to require the Deno.Signal
* enum.
/** **UNSTABLE**
*
* Send a signal to process. This functionality currently only works on
* Linux and Mac OS.
*/
kill(signo: number): void;
kill(signo: string): void; // TODO(ry): Use Signal type here once made stable.
}

export type ProcessStatus =
Expand Down
174 changes: 43 additions & 131 deletions cli/dts/lib.deno.unstable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -562,85 +562,48 @@ declare namespace Deno {
*/
export function applySourceMap(location: Location): Location;

enum LinuxSignal {
SIGHUP = 1,
SIGINT = 2,
SIGQUIT = 3,
SIGILL = 4,
SIGTRAP = 5,
SIGABRT = 6,
SIGBUS = 7,
SIGFPE = 8,
SIGKILL = 9,
SIGUSR1 = 10,
SIGSEGV = 11,
SIGUSR2 = 12,
SIGPIPE = 13,
SIGALRM = 14,
SIGTERM = 15,
SIGSTKFLT = 16,
SIGCHLD = 17,
SIGCONT = 18,
SIGSTOP = 19,
SIGTSTP = 20,
SIGTTIN = 21,
SIGTTOU = 22,
SIGURG = 23,
SIGXCPU = 24,
SIGXFSZ = 25,
SIGVTALRM = 26,
SIGPROF = 27,
SIGWINCH = 28,
SIGIO = 29,
SIGPWR = 30,
SIGSYS = 31,
}
enum MacOSSignal {
SIGHUP = 1,
SIGINT = 2,
SIGQUIT = 3,
SIGILL = 4,
SIGTRAP = 5,
SIGABRT = 6,
SIGEMT = 7,
SIGFPE = 8,
SIGKILL = 9,
SIGBUS = 10,
SIGSEGV = 11,
SIGSYS = 12,
SIGPIPE = 13,
SIGALRM = 14,
SIGTERM = 15,
SIGURG = 16,
SIGSTOP = 17,
SIGTSTP = 18,
SIGCONT = 19,
SIGCHLD = 20,
SIGTTIN = 21,
SIGTTOU = 22,
SIGIO = 23,
SIGXCPU = 24,
SIGXFSZ = 25,
SIGVTALRM = 26,
SIGPROF = 27,
SIGWINCH = 28,
SIGINFO = 29,
SIGUSR1 = 30,
SIGUSR2 = 31,
}

/** **UNSTABLE**: Further changes required to make platform independent.
*
* Signals numbers. This is platform dependent. */
export const Signal: typeof MacOSSignal | typeof LinuxSignal;
export type Signal =
| "SIGABRT"
| "SIGALRM"
| "SIGBUS"
| "SIGCHLD"
| "SIGCONT"
| "SIGEMT"
| "SIGFPE"
| "SIGHUP"
| "SIGILL"
| "SIGINFO"
| "SIGINT"
| "SIGIO"
| "SIGKILL"
| "SIGPIPE"
| "SIGPROF"
| "SIGPWR"
| "SIGQUIT"
| "SIGSEGV"
| "SIGSTKFLT"
| "SIGSTOP"
| "SIGSYS"
| "SIGTERM"
| "SIGTRAP"
| "SIGTSTP"
| "SIGTTIN"
| "SIGTTOU"
| "SIGURG"
| "SIGUSR1"
| "SIGUSR2"
| "SIGVTALRM"
| "SIGWINCH"
| "SIGXCPU"
| "SIGXFSZ";

/** **UNSTABLE**: new API, yet to be vetted.
*
* Represents the stream of signals, implements both `AsyncIterator` and
* `PromiseLike`. */
export class SignalStream
implements AsyncIterableIterator<void>, PromiseLike<void> {
constructor(signal: typeof Deno.Signal);
constructor(signal: Signal);
then<T, S>(
f: (v: void) => T | Promise<T>,
g?: (v: void) => S | Promise<S>,
Expand All @@ -656,7 +619,7 @@ declare namespace Deno {
* iterator.
*
* ```ts
* for await (const _ of Deno.signal(Deno.Signal.SIGTERM)) {
* for await (const _ of Deno.signal("SIGTERM")) {
* console.log("got SIGTERM!");
* }
* ```
Expand All @@ -665,15 +628,15 @@ declare namespace Deno {
* first one.
*
* ```ts
* await Deno.signal(Deno.Signal.SIGTERM);
* await Deno.signal("SIGTERM");
* console.log("SIGTERM received!")
* ```
*
* If you want to stop receiving the signals, you can use `.dispose()` method
* of the signal stream object.
*
* ```ts
* const sig = Deno.signal(Deno.Signal.SIGTERM);
* const sig = Deno.signal("SIGTERM");
* setTimeout(() => { sig.dispose(); }, 5000);
* for await (const _ of sig) {
* console.log("SIGTERM!")
Expand All @@ -685,55 +648,7 @@ declare namespace Deno {
*
* NOTE: This functionality is not yet implemented on Windows.
*/
export function signal(signo: number): SignalStream;

/** **UNSTABLE**: new API, yet to be vetted. */
export const signals: {
/** Returns the stream of SIGALRM signals.
*
* This method is the shorthand for `Deno.signal(Deno.Signal.SIGALRM)`. */
alarm: () => SignalStream;
/** Returns the stream of SIGCHLD signals.
*
* This method is the shorthand for `Deno.signal(Deno.Signal.SIGCHLD)`. */
child: () => SignalStream;
/** Returns the stream of SIGHUP signals.
*
* This method is the shorthand for `Deno.signal(Deno.Signal.SIGHUP)`. */
hungup: () => SignalStream;
/** Returns the stream of SIGINT signals.
*
* This method is the shorthand for `Deno.signal(Deno.Signal.SIGINT)`. */
interrupt: () => SignalStream;
/** Returns the stream of SIGIO signals.
*
* This method is the shorthand for `Deno.signal(Deno.Signal.SIGIO)`. */
io: () => SignalStream;
/** Returns the stream of SIGPIPE signals.
*
* This method is the shorthand for `Deno.signal(Deno.Signal.SIGPIPE)`. */
pipe: () => SignalStream;
/** Returns the stream of SIGQUIT signals.
*
* This method is the shorthand for `Deno.signal(Deno.Signal.SIGQUIT)`. */
quit: () => SignalStream;
/** Returns the stream of SIGTERM signals.
*
* This method is the shorthand for `Deno.signal(Deno.Signal.SIGTERM)`. */
terminate: () => SignalStream;
/** Returns the stream of SIGUSR1 signals.
*
* This method is the shorthand for `Deno.signal(Deno.Signal.SIGUSR1)`. */
userDefined1: () => SignalStream;
/** Returns the stream of SIGUSR2 signals.
*
* This method is the shorthand for `Deno.signal(Deno.Signal.SIGUSR2)`. */
userDefined2: () => SignalStream;
/** Returns the stream of SIGWINCH signals.
*
* This method is the shorthand for `Deno.signal(Deno.Signal.SIGWINCH)`. */
windowChange: () => SignalStream;
};
export function signal(sig: Signal): SignalStream;

export type SetRawOptions = {
cbreak: boolean;
Expand Down Expand Up @@ -803,11 +718,8 @@ declare namespace Deno {
},
>(opt: T): Process<T>;

/** **UNSTABLE**: The `signo` argument may change to require the Deno.Signal
* enum.
*
* Send a signal to process under given `pid`. This functionality currently
* only works on Linux and Mac OS.
/** **UNSTABLE**: Send a signal to process under given `pid`. This
* functionality only works on Linux and Mac OS.
*
* If `pid` is negative, the signal will be sent to the process group
* identified by `pid`.
Expand All @@ -816,10 +728,10 @@ declare namespace Deno {
* cmd: ["sleep", "10000"]
* });
*
* Deno.kill(p.pid, Deno.Signal.SIGINT);
* Deno.kill(p.pid, "SIGINT");
*
* Requires `allow-run` permission. */
export function kill(pid: number, signo: number): void;
export function kill(pid: number, signo: Signal): void;

/** **UNSTABLE**: New API, yet to be vetted. Additional consideration is still
* necessary around the permissions required.
Expand Down
2 changes: 1 addition & 1 deletion cli/tests/testdata/raw_mode_cbreak.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Deno.setRaw(0, true);
Deno.setRaw(0, true, { cbreak: true }); // Can be called multiple times

const signal = Deno.signals.interrupt();
const signal = Deno.signal("SIGINT");

Deno.stdout.writeSync(new TextEncoder().encode("S"));

Expand Down
32 changes: 14 additions & 18 deletions cli/tests/unit/process_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,12 @@ unitTest(
},
async function runCommandFailedWithSignal() {
const p = Deno.run({
cmd: [Deno.execPath(), "eval", "--unstable", "Deno.kill(Deno.pid, 9)"],
cmd: [
Deno.execPath(),
"eval",
"--unstable",
"Deno.kill(Deno.pid, 'SIGKILL')",
],
});
const status = await p.status();
assertEquals(status.success, false);
Expand Down Expand Up @@ -436,7 +441,7 @@ unitTest(

let error = null;
try {
p.kill(Deno.Signal.SIGTERM);
p.kill("SIGTERM");
} catch (e) {
error = e;
}
Expand All @@ -454,21 +459,13 @@ unitTest(
},
);

unitTest(function signalNumbers() {
if (Deno.build.os === "darwin") {
assertEquals(Deno.Signal.SIGSTOP, 17);
} else if (Deno.build.os === "linux") {
assertEquals(Deno.Signal.SIGSTOP, 19);
}
});

unitTest(function killPermissions() {
assertThrows(() => {
// Unlike the other test cases, we don't have permission to spawn a
// subprocess we can safely kill. Instead we send SIGCONT to the current
// process - assuming that Deno does not have a special handler set for it
// and will just continue even if a signal is erroneously sent.
Deno.kill(Deno.pid, Deno.Signal.SIGCONT);
Deno.kill(Deno.pid, "SIGCONT");
}, Deno.errors.PermissionDenied);
});

Expand All @@ -479,19 +476,17 @@ unitTest(
cmd: [Deno.execPath(), "eval", "setTimeout(() => {}, 10000)"],
});

assertEquals(Deno.Signal.SIGINT, 2);
Deno.kill(p.pid, Deno.Signal.SIGINT);
Deno.kill(p.pid, "SIGINT");
const status = await p.status();

assertEquals(status.success, false);
try {
assertEquals(status.code, 128 + Deno.Signal.SIGINT);
assertEquals(status.signal, Deno.Signal.SIGINT);
assertEquals(status.signal, "SIGINT");
} catch {
// TODO(nayeemrmn): On Windows sometimes the following values are given
// instead. Investigate and remove this catch when fixed.
assertEquals(status.code, 1);
assertEquals(status.signal, undefined);
assertEquals(status.code, 130);
assertEquals(status.signal, 2);
}
p.close();
},
Expand All @@ -505,7 +500,8 @@ unitTest({ perms: { run: true, read: true } }, function killFailed() {
assert(!p.stdout);

assertThrows(() => {
Deno.kill(p.pid, 12345);
// @ts-expect-error testing runtime error of bad signal
Deno.kill(p.pid, "foobar");
}, TypeError);

p.close();
Expand Down
Loading

0 comments on commit c132c86

Please sign in to comment.