Skip to content
Merged
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
29 changes: 15 additions & 14 deletions examples/browser_routing.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
"""Example: direct-to-VM browser routing for process exec and raw HTTP."""

from typing import Any, cast
"""Example: direct-to-VM browser routing for raw HTTP."""

import httpx

from kernel import Kernel


def main() -> None:
with Kernel() as client:
browsers = cast(Any, client.browsers)
browser = browsers.create(headless=True)
try:
response = cast(httpx.Response, browsers.request(browser.session_id, "GET", "https://example.com"))
print("status", response.status_code)

with browsers.stream(browser.session_id, "GET", "https://example.com") as streamed:
print("streamed-bytes", len(streamed.read()))
finally:
browsers.delete_by_id(browser.session_id)
client = Kernel()

browser = client.browsers.create()

# Raw browser curl: streams the response. Use for large responses, when you want to stream,
# or when you want httpx.Response semantics.
response: httpx.Response = client.browsers.request(browser.session_id, "GET", "https://example.com")
print("status", response.status_code)

# Buffered browser curl: returns the full response in a JSON envelope. Use for small responses.
buffered = client.browsers.curl(browser.session_id, url="https://example.com", method="GET")
print("body", buffered.body)

client.browsers.delete_by_id(browser.session_id)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Browser session leaks if request or curl fails

Low Severity

The previous code wrapped the browser usage in try/finally to ensure delete_by_id was called even on failure. The new code calls client.browsers.request() and client.browsers.curl() without any error handling, so if either raises an exception, delete_by_id is never reached and the remote browser session is leaked on the server. Since this is example code that users will copy, the missing cleanup pattern could lead to orphaned browser sessions in real applications.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 84cb2e3. Configure here.



if __name__ == "__main__":
Expand Down