Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.
Python ai-agentsai-toolsbrowser-agentbrowser-automationbrowser-usecdpcloud-browsercomputer-useheadless-browserllmmcpplaywrightpythonremote-browserstealth-browserweb-automationweb-scraping
fix(utils): add option to disable SignalHandler for host app signal control
BillionClaw1147daef92ed
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.pyindex 08e61d51e..25d34e103 100644--- a/browser_use/agent/service.py+++ b/browser_use/agent/service.py@@ -205,6 +205,7 @@ class Agent(Generic[Context, AgentStructuredOutput]):message_compaction: MessageCompactionSettings | bool | None = True,max_clickable_elements_length: int = 40000,_url_shortening_limit: int = 25,+ enable_signal_handler: bool = True,**kwargs,):# Validate llm_screenshot_size@@ -420,6 +421,9 @@ class Agent(Generic[Context, AgentStructuredOutput]):if self.settings.message_compaction and self.settings.message_compaction.compaction_llm:self.token_cost_service.register_llm(self.settings.message_compaction.compaction_llm)+ # Store signal handler setting (not part of AgentSettings as it's runtime behavior)+ self.enable_signal_handler = enable_signal_handler+# Initialize stateself.state = injected_agent_state or AgentState()@@ -2493,6 +2497,7 @@ class Agent(Generic[Context, AgentStructuredOutput]):resume_callback=self.resume,custom_exit_callback=on_force_exit_log_telemetry, # Pass the new telemetrycallbackexit_on_second_int=True,+ disabled=not self.enable_signal_handler,)signal_handler.register()diff --git a/browser_use/utils.py b/browser_use/utils.pyindex 1baac45d5..5661c9f34 100644--- a/browser_use/utils.py+++ b/browser_use/utils.py@@ -77,6 +77,7 @@ class SignalHandler:- Management of event loop state across signals- Standardized handling of first and second Ctrl+C presses- Cross-platform compatibility (with simplified behavior on Windows)+ - Option to disable signal handling for embedding in applications that manage their own signals"""def __init__(@@ -87,6 +88,7 @@ class SignalHandler:custom_exit_callback: Callable[[], None] | None = None,exit_on_second_int: bool = True,interruptible_task_patterns: list[str] | None = None,+ disabled: bool = False,):"""Initialize the signal handler.@@ -99,6 +101,8 @@ class SignalHandler:exit_on_second_int: Whether to exit on second SIGINT (Ctrl+C)interruptible_task_patterns: List of patterns to match task names that should becanceled on first Ctrl+C (default: ['step', 'multi_act', 'get_next_action'])+ disabled: If True, signal handling is disabled and register() is a no-op.+ Useful when embedding browser-use in applications that manage their own signals."""self.loop = loop or asyncio.get_event_loop()self.pause_callback = pause_callback@@ -107,6 +111,7 @@ class SignalHandler:self.exit_on_second_int = exit_on_second_intself.interruptible_task_patterns = interruptible_task_patterns or ['step', 'multi_act', 'get_next_action']self.is_windows = platform.system() == 'Windows'+ self.disabled = disabled# Initialize loop state attributesself._initialize_loop_state()@@ -121,7 +126,13 @@ class SignalHandler:setattr(self.loop, 'waiting_for_input', False)def register(self) -> None:- """Register signal handlers for SIGINT and SIGTERM."""+ """Register signal handlers for SIGINT and SIGTERM.++ If disabled=True was passed to __init__, this method does nothing.+ """+ if self.disabled:+ return+try:if self.is_windows:# On Windows, use simple signal handling with immediate exit on Ctrl+C@@ -146,7 +157,13 @@ class SignalHandler:passdef unregister(self) -> None:- """Unregister signal handlers and restore original handlers if possible."""+ """Unregister signal handlers and restore original handlers if possible.++ If disabled=True was passed to __init__, this method does nothing.+ """+ if self.disabled:+ return+try:if self.is_windows:# On Windows, just restore the original SIGINT handler