-
-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: move playwright to thread to avoid event loop conflicts
Resolves #351 by moving Playwright to a separate thread to isolate its event loop from prompt_toolkit. This prevents the asyncio.run() error that occurred when trying to use prompt_toolkit after browser operations. Changes: - Created thread-based browser manager - Updated all browser operations to use the thread - Added proper timeout and error handling Co-authored-by: Bob <[email protected]>
- Loading branch information
1 parent
0ecf045
commit 5673f4d
Showing
2 changed files
with
163 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import logging | ||
import time | ||
from collections.abc import Callable | ||
from dataclasses import dataclass | ||
from queue import Empty, Queue | ||
from threading import Event, Lock, Thread | ||
from typing import Any, Literal, TypeVar | ||
|
||
from playwright.sync_api import sync_playwright | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
T = TypeVar("T") | ||
|
||
TIMEOUT = 30 # seconds | ||
|
||
|
||
@dataclass | ||
class Command: | ||
func: Callable | ||
args: tuple | ||
kwargs: dict | ||
|
||
|
||
Action = Literal["stop"] | ||
|
||
|
||
class BrowserThread: | ||
def __init__(self): | ||
self.queue: Queue[tuple[Command | Action, object]] = Queue() | ||
self.results: dict[object, tuple[Any, Exception | None]] = {} | ||
self.lock = Lock() | ||
self.ready = Event() | ||
self.thread = Thread(target=self._run, daemon=True) | ||
self.thread.start() | ||
# Wait for browser to be ready | ||
if not self.ready.wait(timeout=TIMEOUT): | ||
raise TimeoutError("Browser failed to start") | ||
logger.info("Browser thread started") | ||
|
||
def _run(self): | ||
try: | ||
playwright = sync_playwright().start() | ||
browser = playwright.chromium.launch() | ||
logger.info("Browser launched") | ||
self.ready.set() | ||
|
||
while True: | ||
try: | ||
cmd, cmd_id = self.queue.get(timeout=1.0) | ||
if cmd == "stop": | ||
break | ||
|
||
try: | ||
result = cmd.func(browser, *cmd.args, **cmd.kwargs) | ||
with self.lock: | ||
self.results[cmd_id] = (result, None) | ||
except Exception as e: | ||
logger.exception("Error in browser thread") | ||
with self.lock: | ||
self.results[cmd_id] = (None, e) | ||
except Empty: | ||
# Timeout on queue.get, continue waiting | ||
continue | ||
except Exception: | ||
logger.exception("Fatal error in browser thread") | ||
self.ready.set() # Prevent hanging in __init__ | ||
raise | ||
finally: | ||
try: | ||
browser.close() | ||
playwright.stop() | ||
except Exception: | ||
logger.exception("Error stopping browser") | ||
logger.info("Browser stopped") | ||
|
||
def execute(self, func: Callable[..., T], *args, **kwargs) -> T: | ||
if not self.thread.is_alive(): | ||
raise RuntimeError("Browser thread died") | ||
|
||
cmd_id = object() # unique id | ||
self.queue.put((Command(func, args, kwargs), cmd_id)) | ||
|
||
deadline = time.monotonic() + TIMEOUT | ||
while time.monotonic() < deadline: | ||
with self.lock: | ||
if cmd_id in self.results: | ||
result, error = self.results.pop(cmd_id) | ||
if error: | ||
raise error | ||
return result | ||
time.sleep(0.1) # Prevent busy-waiting | ||
|
||
raise TimeoutError(f"Browser operation timed out after {TIMEOUT}s") | ||
|
||
def stop(self): | ||
"""Stop the browser thread""" | ||
try: | ||
self.queue.put(("stop", object())) | ||
self.thread.join(timeout=TIMEOUT) | ||
except Exception: | ||
logger.exception("Error stopping browser thread") |