Skip to content

File descriptor leak due to unclosed WebSocket connections #3705

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix for FD leak on CPD
  • Loading branch information
felipehertzer committed Apr 24, 2025
commit 9e4d3a9b449802ac0d1485bec9ed65aa3ff5834c
1 change: 1 addition & 0 deletions seleniumbase/core/browser_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,7 @@ def uc_open_with_cdp_mode(driver, url=None):
loop.run_until_complete(page_tab.activate())

loop.run_until_complete(driver.cdp_base.update_targets())
loop.run_until_complete(driver.cdp_base.connection.aclose())
page = loop.run_until_complete(driver.cdp_base.get(url))
with gui_lock:
with suppress(Exception):
Expand Down
1 change: 1 addition & 0 deletions seleniumbase/plugins/sb_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -1373,6 +1373,7 @@ def SB(
loop = asyncio.get_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(sb_config._cdp_aclose())
loop.close()
gc.collect()
if test and test_name and not test_passed and raise_test_failure:
raise exception
Expand Down
24 changes: 11 additions & 13 deletions seleniumbase/undetected/cdp_driver/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ async def wait(self, time: Union[float, int] = 1) -> Browser:

sleep = wait
"""Alias for wait"""
def _handle_target_update(
async def _handle_target_update(
self,
event: Union[
cdp.target.TargetInfoChanged,
Expand Down Expand Up @@ -226,19 +226,17 @@ def _handle_target_update(
target_info: cdp.target.TargetInfo = event.target_info
from .tab import Tab

new_target = Tab(
(
f"ws://{self.config.host}:{self.config.port}"
f"/devtools/{target_info.type_ or 'page'}"
f"/{target_info.target_id}"
),
target=target_info,
browser=self,
)
self.targets.append(new_target)
logger.debug(
"Target #%d created => %s", len(self.targets), new_target
websocket_url = (
f"ws://{self.config.host}:{self.config.port}"
f"/devtools/{target_info.type_ or 'page'}"
f"/{target_info.target_id}"
)

async with Tab(websocket_url=websocket_url, target=target_info, browser=self) as new_target:
self.targets.append(new_target)
logger.debug(
"Target #%d created => %s", len(self.targets), new_target
)
elif isinstance(event, cdp.target.TargetDestroyed):
current_tab = next(
filter(
Expand Down
10 changes: 10 additions & 0 deletions seleniumbase/undetected/cdp_driver/tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,16 @@ def __init__(
self._dom = None
self._window_id = None


async def __aenter__(self):
return self


async def __aexit__(self, exc_type, exc_val, exc_tb):
await self.aclose()
if exc_type and exc_val:
raise exc_type(exc_val)

@property
def inspector_url(self):
"""
Expand Down