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

Hold event loop during main() #841

Merged
merged 1 commit into from
Dec 7, 2023
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
6 changes: 6 additions & 0 deletions lib/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ export async function main(
// this function in the exit context so it can be called anywhere.
yield* ExitContext.set(resolve);

// this will hold the event loop and prevent runtimes such as
// Node and Deno from exiting prematurely.
let interval = setInterval(() => {}, Math.pow(2, 30));

try {
let interrupt = () => resolve({ status: 130, signal: "SIGINT" });
yield* withHost({
Expand Down Expand Up @@ -105,6 +109,8 @@ export async function main(
yield* exit(0);
} catch (error) {
resolve({ status: 1, error });
} finally {
clearInterval(interval);
}
})
);
Expand Down
19 changes: 19 additions & 0 deletions test/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,25 @@ describe("main", () => {
expect(status.code).toEqual(1);
});
});

it("works even if suspend is the only operation", async () => {
await run(function* () {
let process = yield* useCommand("deno", {
stdout: "piped",
args: ["run", "test/main/just.suspend.ts"],
});
let stdout = yield* buffer(process.stdout);
yield* detect(stdout, "started");

process.kill("SIGINT");

let status = yield* process.status;

expect(status.code).toBe(130);

yield* detect(stdout, "gracefully stopped");
});
});
});

import type { Operation } from "../lib/types.ts";
Expand Down
11 changes: 11 additions & 0 deletions test/main/just.suspend.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { suspend } from "../../lib/instructions.ts";
import { main } from "../../lib/main.ts";

await main(function* () {
console.log("started");
try {
yield* suspend();
} finally {
console.log("gracefully stopped");
}
});
Loading