Skip to content

Improve CDP memory usage #3711

New issue

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

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

Already on GitHub? # to your account

Merged
merged 2 commits into from
Apr 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion seleniumbase/__version__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# seleniumbase package
__version__ = "4.37.9"
__version__ = "4.37.10"
13 changes: 8 additions & 5 deletions seleniumbase/plugins/sb_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -1369,12 +1369,15 @@ def SB(
"%s%s%s%s%s"
% (c1, left_space, end_text, right_space, cr)
)
if hasattr(sb_config, "_cdp_aclose"):
if undetectable and hasattr(sb, "_drivers_browser_map"):
import asyncio
with suppress(Exception):
loop = asyncio.get_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(sb_config._cdp_aclose())
for driver in sb._drivers_browser_map.keys():
if hasattr(driver, "cdp") and driver.cdp:
asyncio.set_event_loop(driver.cdp.loop)
tasks = [tab.aclose() for tab in driver.cdp.get_tabs()]
tasks.append(driver.cdp.driver.connection.aclose())
driver.cdp.loop.run_until_complete(asyncio.gather(*tasks))
driver.cdp.loop.close()
gc.collect()
if test and test_name and not test_passed and raise_test_failure:
raise exception
Expand Down
3 changes: 0 additions & 3 deletions seleniumbase/undetected/cdp_driver/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
)
import websockets
from websockets.protocol import State
from seleniumbase import config as sb_config
from . import cdp_util as util
import mycdp as cdp
import mycdp.network
Expand Down Expand Up @@ -271,7 +270,6 @@ async def aopen(self, **kw):
max_size=MAX_SIZE,
)
self.listener = Listener(self)
sb_config._cdp_aclose = self.aclose
except (Exception,) as e:
logger.debug("Exception during opening of websocket: %s", e)
if self.listener:
Expand Down Expand Up @@ -446,7 +444,6 @@ async def send(
if not _is_update:
await self._register_handlers()
await self.websocket.send(tx.message)
sb_config._cdp_aclose = self.aclose
try:
return await tx
except ProtocolException as e:
Expand Down
1 change: 1 addition & 0 deletions seleniumbase/undetected/cdp_driver/tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,7 @@ async def close(self):
await self.send(
cdp.target.close_target(target_id=self.target.target_id)
)
await self.aclose()
await asyncio.sleep(0.1)

async def get_window(self) -> Tuple[
Expand Down