Skip to content
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
1 change: 0 additions & 1 deletion templates/python/advanced-sample/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ async def test_captcha_solver(ctx: kernel.KernelContext) -> None:
kernel_browser = client.browsers.create(
invocation_id=ctx.invocation_id,
stealth=True,
persistence={"id": "captcha-solver"}
)

async with async_playwright() as playwright:
Expand Down
20 changes: 10 additions & 10 deletions templates/python/sample-app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,34 +62,34 @@ async def get_page_title(ctx: kernel.KernelContext, input_data: PageTitleInput)

return {"title": title}
finally:
await browser.close()
client.browsers.delete_by_id(kernel_browser.session_id)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Missing await on async browser deletion call

The client.browsers.delete_by_id() call is missing await in this async function. The TypeScript equivalent properly uses await kernel.browsers.deleteByID(...), indicating this is an async operation. Without await, the browser deletion may not complete before the function returns, errors will be silently lost, and browser sessions may not be properly cleaned up.

Fix in Cursor Fix in Web



"""
Example app that instantiates a persisted Kernel browser that can be reused across invocations
Example app that creates a long-running Kernel browser for manual testing
Invoke this action to test Kernel browsers manually with our browser live view
https://onkernel.com/docs/browsers/persistence
https://onkernel.com/docs/browsers/live-view
Args:
ctx: Kernel context containing invocation information
Returns:
A dictionary containing the browser live view url
Invoke this via CLI:
kernel login # or: export KERNEL_API_KEY=<your_api_key>
kernel deploy main.py # If you haven't already deployed this app
kernel invoke python-basic create-persisted-browser
kernel invoke python-basic create-browser-for-testing
kernel logs python-basic -f # Open in separate tab
"""
class CreatePersistedBrowserOutput(TypedDict):
class CreateBrowserForTestingOutput(TypedDict):
browser_live_view_url: str

@app.action("create-persisted-browser")
async def create_persisted_browser(ctx: kernel.KernelContext) -> CreatePersistedBrowserOutput:
@app.action("create-browser-for-testing")
async def create_browser_for_testing(ctx: kernel.KernelContext) -> CreateBrowserForTestingOutput:
kernel_browser = client.browsers.create(
invocation_id=ctx.invocation_id,
persistence={"id": "persisted-browser"},
stealth=True, # Turns on residential proxy & auto-CAPTCHA solver
stealth=True,
timeout_seconds=3600, # Keep browser alive for 1 hour
)

return {
"browser_live_view_url": kernel_browser.browser_live_view_url,
}
}
3 changes: 0 additions & 3 deletions templates/typescript/advanced-sample/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ app.action("test-captcha-solver", async (ctx: KernelContext): Promise<void> => {
const kernelBrowser = await kernel.browsers.create({
invocation_id: ctx.invocation_id,
stealth: true,
persistence: {
id: "captcha-solver",
},
});
const browser = await chromium.connectOverCDP(kernelBrowser.cdp_ws_url);

Expand Down
20 changes: 9 additions & 11 deletions templates/typescript/sample-app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,37 +70,35 @@ app.action<PageTitleInput, PageTitleOutput>(
const title = await page.title();
return { title };
} finally {
await browser.close();
await kernel.browsers.deleteByID(kernelBrowser.session_id);
}
}
);

/**
* Example app that instantiates a persisted Kernel browser that can be reused across invocations
* Example app that creates a long-running Kernel browser for manual testing
* Invoke this action to test Kernel browsers manually with our browser live view
* https://onkernel.com/docs/browsers/persistence
* https://onkernel.com/docs/browsers/live-view
* Args:
* ctx: Kernel context containing invocation information
* Returns:
* A dictionary containing the browser live view url
* Invoke this via CLI:
* kernel login # or: export KERNEL_API_KEY=<your_api_key>
* kernel deploy index.ts # If you haven't already deployed this app
* kernel invoke ts-basic create-persisted-browser
* kernel invoke ts-basic create-browser-for-testing
* kernel logs ts-basic -f # Open in separate tab
*/
interface CreatePersistedBrowserOutput {
interface CreateBrowserForTestingOutput {
browser_live_view_url: string;
}
app.action(
"create-persisted-browser",
async (ctx: KernelContext): Promise<CreatePersistedBrowserOutput> => {
"create-browser-for-testing",
async (ctx: KernelContext): Promise<CreateBrowserForTestingOutput> => {
const kernelBrowser = await kernel.browsers.create({
invocation_id: ctx.invocation_id,
persistence: {
id: "persisted-browser",
},
stealth: true, // Turns on residential proxy & auto-CAPTCHA solver
stealth: true,
timeout_seconds: 3600, // Keep browser alive for 1 hour
});

return {
Expand Down
Loading