fix: remove page.on() event listeners on environment close to prevent memory leak by rahulrao85 · Pull Request #59 · microsoft/Webwright · GitHub
Skip to content

fix: remove page.on() event listeners on environment close to prevent memory leak - #59

Open
Rahul Rao (rahulrao85) wants to merge 1 commit into
microsoft:mainfrom
rahulrao85:fix-listener-leak-page-on
Open

fix: remove page.on() event listeners on environment close to prevent memory leak#59
Rahul Rao (rahulrao85) wants to merge 1 commit into
microsoft:mainfrom
rahulrao85:fix-listener-leak-page-on

Conversation

@rahulrao85

Copy link
Copy Markdown

Summary

_attach_page_listeners()\ in \local_browser.py\ registers \page.on('console', ...)\ and \page.on('pageerror', ...)\ handlers but never removes them. The Playwright pyee event emitter retains references to the bound methods, causing a memory leak that grows linearly with agent steps.

Changes

  • Store listener removal callbacks in \self._page_cleanup\
  • Add _detach_page_listeners()\ method to clean up all listeners
  • Call _detach_page_listeners()\ at the start of _close_async()\ before nullifying the page reference
  • Import \Callable\ from typing

Testing

  • Existing tests continue to pass
  • The cleanup is defensive (catches all exceptions per listener)
  • No change to public API or behavior

Fixes: #57

… memory leak

The _attach_page_listeners method registered 'console' and 'pageerror' handlers via page.on() but these were never removed in _close_async(). Over long agent runs, orphaned listeners accumulate in the Playwright pyee event emitter, causing linear memory growth.

Fixes: microsoft#57
@microsoft-github-policy-service

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Memory leak: page.on() event listeners never removed on close

1 participant