Skip to content
Draft
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
28 changes: 28 additions & 0 deletions packages/runtime/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,4 +329,32 @@ describe("BrowserRuntime", () => {
message: "other failure",
});
});

it("rejects createSession after shutdown without relaunching the browser", async () => {
const { browser } = createMockBrowserTree();
mockChromiumLaunch.mockResolvedValueOnce(browser);

const rt = new BrowserRuntime({ headless: true });
await rt.createSession();
await rt.shutdown();

mockChromiumLaunch.mockClear();
await expect(rt.createSession()).rejects.toMatchObject({
code: "COMMAND_FAILED",
message: "Runtime is shutting down; cannot create session",
});
expect(mockChromiumLaunch).not.toHaveBeenCalled();
});

it("shutdown is idempotent", async () => {
const { browser, browserClose } = createMockBrowserTree();
mockChromiumLaunch.mockResolvedValueOnce(browser);

const rt = new BrowserRuntime({ headless: true });
await rt.createSession();
await rt.shutdown();
await rt.shutdown();

expect(browserClose).toHaveBeenCalledTimes(1);
});
});
21 changes: 21 additions & 0 deletions packages/runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,19 @@ type SessionRecord = {
export class BrowserRuntime {
private browserPromise: Promise<Browser> | null = null;
private sessions = new Map<string, SessionRecord>();
private shuttingDown = false;

constructor(private readonly options: BrowserRuntimeOptions = {}) {}

private assertActive(operation: string) {
if (this.shuttingDown) {
throw new WebchainRuntimeError(
"COMMAND_FAILED",
`Runtime is shutting down; cannot ${operation}`,
);
}
}

private async getBrowser(): Promise<Browser> {
if (!this.browserPromise) {
this.browserPromise = this.launchBrowser();
Expand Down Expand Up @@ -70,6 +80,7 @@ export class BrowserRuntime {
}

async createSession(): Promise<RuntimeSession> {
this.assertActive("create session");
const browser = await this.getBrowser();
const context = await browser.newContext();

Expand All @@ -96,6 +107,7 @@ export class BrowserRuntime {
}

async navigate(command: NavigateCommand): Promise<ActionResult> {
this.assertActive("navigate");
const session = this.getSession(command.sessionId);
try {
await session.page.goto(command.url, { waitUntil: "domcontentloaded" });
Expand All @@ -111,6 +123,7 @@ export class BrowserRuntime {
}

async snapshot(command: SnapshotCommand): Promise<SnapshotResult> {
this.assertActive("snapshot");
const session = this.getSession(command.sessionId);
try {
const page = session.page;
Expand Down Expand Up @@ -164,6 +177,7 @@ export class BrowserRuntime {
}

async click(command: ClickCommand): Promise<ActionResult> {
this.assertActive("click");
const session = this.getSession(command.sessionId);
try {
await session.page.locator(command.selector).first().click();
Expand All @@ -179,6 +193,7 @@ export class BrowserRuntime {
}

async type(command: TypeCommand): Promise<ActionResult> {
this.assertActive("type");
const session = this.getSession(command.sessionId);
try {
await session.page.locator(command.selector).first().fill(command.text);
Expand All @@ -196,6 +211,7 @@ export class BrowserRuntime {
async closeSession(
command: CloseSessionCommand,
): Promise<CloseSessionResult> {
this.assertActive("close session");
const session = this.getSession(command.sessionId);
try {
await session.context.close();
Expand All @@ -211,6 +227,11 @@ export class BrowserRuntime {
}

async shutdown() {
if (this.shuttingDown) {
return;
}
this.shuttingDown = true;

await Promise.all(
[...this.sessions.values()].map(async (session) => {
await session.context.close();
Expand Down
9 changes: 8 additions & 1 deletion services/companion/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,16 @@ const runtime = new BrowserRuntime({

const { app } = await createCompanionApp({ runtime });

let closing = false;

const closeGracefully = async () => {
await runtime.shutdown();
if (closing) {
return;
}
closing = true;

await app.close();
await runtime.shutdown();
process.exit(0);
};

Expand Down
2 changes: 1 addition & 1 deletion services/mcp/src/test-support/bootstrap-mcp-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ export async function bootstrapMcpStack(
shutdown: async () => {
await client.close();
await stdioTransport.close();
await runtime.shutdown();
await app.close();
await runtime.shutdown();
},
};
}
Loading