Add disabled parameter to SignalHandler class to allow opting out of
signal handling. This enables browser-use to be embedded in applications
like uvicorn/FastAPI that need to manage their own signal lifecycle.
Add enable_signal_handler parameter to Agent class (default True for
backward compatibility). When set to False, the Agent will not register
signal handlers, allowing the host application to control graceful
shutdown and signal handling.
Fixes#4385
- agent/views.py: Move RateLimitError import inside format_error() function
to avoid loading openai SDK (~800ms) at module level
- llm/messages.py: Replace openai.BaseModel with pydantic.BaseModel directly
to remove unnecessary openai dependency
- filesystem/file_system.py: Move reportlab imports inside sync_to_disk_sync()
to avoid ~40ms startup cost when PDF generation is not used
- utils.py: Convert OpenAIBadRequestError and GroqBadRequestError to lazy
loaders to avoid loading SDKs at module level
This improves import time for users who don't use OpenAI provider,
especially when using Anthropic, Google, or other providers.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Introduced a variable to hold exceptions for re-raising outside the try-except block, ensuring proper propagation to the event loop.
- Updated comments for clarity on the exception handling process in background tasks.
- Updated the logging behavior for exceptions in background tasks to log at warning level and re-raise exceptions for better propagation to the event loop's exception handler.
- Enhanced clarity in comments regarding exception retrieval and handling.
- Improved the `create_task_with_error_handling` function to allow for better exception logging and retrieval based on the `suppress_exceptions` parameter.
- Updated `SessionManager` to implement event-driven recovery for stale agent focus, replacing polling with efficient event handling.
- Refactored session retrieval methods to ensure focus validation and recovery are handled automatically.
- Enhanced logging for recovery processes and session management to provide clearer insights into state changes and errors.
- Adjusted various watchdogs to utilize the new session management methods, ensuring they correctly handle focus validation and session retrieval.
- Introduced a utility function `create_task_with_error_handling` to manage asyncio tasks with proper exception logging and suppression options.
- Updated multiple components, including `SessionManager`, `BrowserSession`, and various watchdogs, to utilize the new error handling mechanism for background tasks.
- Enhanced overall robustness of asynchronous operations by ensuring exceptions are logged and handled appropriately, improving maintainability and debugging capabilities.