Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

Merge pull request #1179 from browser-use/ctrl-c-improvements

Nick Sweeting1d0ff6617b99
diff --cc browser_use/agent/service.py
index 4575dc199,2b3e9ae9e..2002042d4
--- a/browser_use/agent/service.py
+++ b/browser_use/agent/service.py
@@@ -86,65 -77,8 +77,10 @@@ def log_response(response: AgentOutput
logger.info(f'๐Ÿ› ๏ธ Action {i + 1}/{len(response.action)}: {action.model_dump_json(exclude_unset=True)}')
- def sigint_handler(loop: asyncio.AbstractEventLoop, agent=None):
- """SIGINT (Ctrl+C) handler.
-
- First Ctrl+C: Cancel current step and pause the agent.
- Second Ctrl+C: Exit immediately.
- """
- global _exiting
-
- if _exiting:
- # Already exiting, force exit immediately
- os._exit(0)
-
- if getattr(loop, 'ctrl_c_pressed', False):
- # If we're in the waiting for input state, let the pause method handle it
- if getattr(loop, 'waiting_for_input', False):
- return
-
- # Second Ctrl+C - exit immediately
- _exiting = True
- logger.info('\n\n๐Ÿ›‘๐Ÿ›‘ Second Ctrl+C detected. Exiting immediately...\n\n')
-
- # Force exit - more reliable than sys.exit()
- os._exit(0)
-
- # Mark that Ctrl+C was pressed
- loop.ctrl_c_pressed = True
-
- # Cancel current tasks
- current_task = asyncio.current_task(loop)
- for task in asyncio.all_tasks(loop):
- if task != current_task and not task.done():
- task_name = task.get_name() if hasattr(task, 'get_name') else str(task)
- if 'step' in task_name or 'multi_act' in task_name or 'get_next_action' in task_name:
- task.cancel()
-
- # If we can get the agent reference, pause it
- if agent and hasattr(agent, 'pause'):
- agent.pause()
-
- logger.info('\n\n๐Ÿ›‘ Ctrl+C detected. Current step cancelled and agent paused.')
- logger.info('Press Enter to resume or Ctrl+C again to exit completely.\n\n')
-
-
- def sigterm_handler(loop: asyncio.AbstractEventLoop):
- """SIGTERM handler.
-
- Always exits the program completely.
- """
- global _exiting
- if not _exiting:
- _exiting = True
- logger.info('\n\n๐Ÿ›‘ SIGTERM received. Exiting immediately...\n\n')
- os._exit(0)
-
-
Context = TypeVar('Context')
+AgentHookFunc = Callable[['Agent'], None]
+
class Agent(Generic[Context]):
@time_execution_sync('--init (agent)')