You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using crewai_tools version 0.36.0 (and possibly other versions above 0.32.0), the SeleniumScrapingTool class initializes a Chrome WebDriver instance immediately upon import/instantiation, even if the tool is not being used. This causes Chrome browser windows to automatically open during application startup when the module containing SeleniumScrapingTool is imported.
Additionally, these browser instances are not properly closed when the application reloads (especially in development mode with hot-reloading), leading to multiple Chrome processes accumulating in memory and causing severe memory issues over time.
Steps to Reproduce
Install crewai_tools version 0.36.0 (any version above 0.32.0)
Create a simple FastAPI application
Import and instantiate SeleniumScrapingTool at module level:
fromcrewai_toolsimportSeleniumScrapingTool# This line alone causes a Chrome browser to open during importselenium_scraper=SeleniumScrapingTool()
Run the application with uvicorn main:app --reload
Make a change to any file to trigger a reload
Observe that a new Chrome window opens with each reload, while old processes remain in memory
Expected behavior
The Chrome WebDriver should only be initialized when the tool is actually used (when its _run method is called), not when the tool is instantiated. Additionally, browser instances should be properly cleaned up when no longer needed.
Code snippets
The problematic code is in the __init__ method of the SeleniumScrapingTool class, where it creates a WebDriver instance immediately:
def__init__(
self,
website_url: Optional[str] =None,
cookie: Optional[dict] =None,
css_element: Optional[str] =None,
**kwargs,
):
super().__init__(**kwargs)
try:
fromseleniumimportwebdriverfromselenium.webdriver.chrome.optionsimportOptionsfromselenium.webdriver.common.byimportByexceptImportError:
# Import handling code...# This line creates a WebDriver instance immediately upon instantiationself.driver=webdriver.Chrome()
# ...
This is particularly problematic in development environments with hot-reloading, as each reload creates a new browser instance without properly cleaning up the previous ones.
Operating System
macOS
Python Version
3.11.4
crewAI Version
0.102.0
crewAI Tools Version
0.36.0
Evidence
When running a FastAPI application with hot-reloading enabled, you can observe:
Chrome browser windows automatically opening during application startup
New Chrome windows opening with each reload
Increasing memory usage over time as shown in Activity Monitor/Task Manager
Multiple chrome processes running in the background even after the application is stopped
The issue does not occur with crewai_tools version 0.32.0, confirming that the behavior was introduced in a more recent version.
Possible Solution
The solution is to implement lazy initialization of the WebDriver in the SeleniumScrapingTool class:
Store the WebDriver class instead of creating an instance in __init__
Add a _create_driver_instance method that creates the WebDriver only when needed
Modify _create_driver to use the lazy initialization
Improve the close method to ensure proper cleanup of resources
This approach ensures that:
No browser window opens until the tool is actually used
The tool still works exactly the same way when it is used
Proper cleanup happens to prevent memory leaks
I'm willing to submit a PR with this fix if desired.
The text was updated successfully, but these errors were encountered:
Description
When using crewai_tools version 0.36.0 (and possibly other versions above 0.32.0), the
SeleniumScrapingTool
class initializes a Chrome WebDriver instance immediately upon import/instantiation, even if the tool is not being used. This causes Chrome browser windows to automatically open during application startup when the module containingSeleniumScrapingTool
is imported.Additionally, these browser instances are not properly closed when the application reloads (especially in development mode with hot-reloading), leading to multiple Chrome processes accumulating in memory and causing severe memory issues over time.
Steps to Reproduce
SeleniumScrapingTool
at module level:uvicorn main:app --reload
Expected behavior
The Chrome WebDriver should only be initialized when the tool is actually used (when its
_run
method is called), not when the tool is instantiated. Additionally, browser instances should be properly cleaned up when no longer needed.Code snippets
The problematic code is in the
__init__
method of theSeleniumScrapingTool
class, where it creates a WebDriver instance immediately:This is particularly problematic in development environments with hot-reloading, as each reload creates a new browser instance without properly cleaning up the previous ones.
Operating System
macOS
Python Version
3.11.4
crewAI Version
0.102.0
crewAI Tools Version
0.36.0
Evidence
When running a FastAPI application with hot-reloading enabled, you can observe:
The issue does not occur with crewai_tools version 0.32.0, confirming that the behavior was introduced in a more recent version.
Possible Solution
The solution is to implement lazy initialization of the WebDriver in the SeleniumScrapingTool class:
__init__
_create_driver_instance
method that creates the WebDriver only when needed_create_driver
to use the lazy initializationclose
method to ensure proper cleanup of resourcesThis approach ensures that:
I'm willing to submit a PR with this fix if desired.
The text was updated successfully, but these errors were encountered: