joetong
joetong5w ago

playwright chromium could not be successfully closed

def run(playwright, url: str, doc: dict, proxy): browser = None try: browser = playwright.chromium.launch(proxy=proxy) context = browser.new_context() context.set_default_navigation_timeout(600000)
# 拦截请求 def block_images_and_css(route, request): if request.resourcetype in ["image", "stylesheet", "script"]: route.abort() else: route.continue()
context.route("*/", block_images_and_css) page = context.new_page() page.goto(url) page.evaluate("window.scrollTo(0, document.body.scrollHeight)") page.wait_for_load_state("domcontentloaded") except Exception as e: print(f"An error occurred: {e}") finally: if browser: browser.close()
3 Replies
Alper
Alper5w ago
can you confirm that you have a lot of chromium zombie processes? check it with:
# ps aux | grep defunct
root 3397781 0.0 0.0 0 0 ? Z Sep05 0:00 [chrome] <defunct>
root 3397782 0.0 0.0 0 0 ? Z Sep05 0:00 [chrome] <defunct>
root 3397893 0.0 0.0 0 0 ? Z Sep05 0:00 [chrome] <defunct>
...
# ps aux | grep defunct
root 3397781 0.0 0.0 0 0 ? Z Sep05 0:00 [chrome] <defunct>
root 3397782 0.0 0.0 0 0 ? Z Sep05 0:00 [chrome] <defunct>
root 3397893 0.0 0.0 0 0 ? Z Sep05 0:00 [chrome] <defunct>
...
if yes, you can set up a cronjob that runs daily and deletes all defunct processes. the only way to do that is to kill the parent process. which is windmill crontab could look like this:
0 0 * * * /usr/bin/killall -9 windmil
0 0 * * * /usr/bin/killall -9 windmil
since windmill runs in a docker container that automatically restarts on failure there is little to no downtime for the workers. this is the only way i found to solve this problem - at least for my case
joetong
joetong5w ago
Ok, thanks for your reply
Alper
Alper5w ago
also, you should probably also close the context before the browser:
context.close()
context.close()
https://playwright.dev/docs/api/class-browsercontext#browser-context-close
BrowserContext | Playwright
BrowserContexts provide a way to operate multiple independent browser sessions.