-
Notifications
You must be signed in to change notification settings - Fork 978
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
[Feature] A way to prevent SIGINT (cmd+c) being passed to the browser processes so that they can be shutdown cleanly. #1170
Comments
Having the same issue where SIGINT closes the browser even with |
Not as handy as |
Look at this beautiful loop. I cancel it with Ctrl+C, then I handle the KeyboardInterrupt exception. |
@blksith0 as far as I can tell, it is a bug in Playwright, you cannot solve it on your side. If I may, I'd recommend you to use |
@Rafiot Yes, I see that. I'm just not smart enough to know how I can send a SIGTERM to my program and use that to stop the loop. |
It would be really great if I could terminate my loop and not have it kill the playwright browser. |
While I'm aware what I'm going to write is not an exact solution to the mentioned problem, I've got a slightly different perspective due to the "other" stuff going on with playwright. Exactly like yous I needed these feature, because I wanted to handle playwright error separately and do something except for ctrl-c etc (mainly SIGINTs and SIGTERMs). I would expect them to generate However, playwright has another bug (hopefully soon to be solved after 2.5 yrs of it being an open issue) - it mem-leaks. It's a big thread but I face most problems mentioned there for long running applications. The TL;DR from there is that the browser will unexpectedly close after certain num of context-creates and destroys and mem just keeps rising (so it sometimes closes and sometimes crashes with the app with OOM in dockers/fargates but amounts to the same thing - the browser's gone). Which means that the browser close not only happens on ctrl-c etc. but can happen due to other bugs like above. So instead of waiting for the solution from here (for ctrl-c to not raise playwright error due to browser close), I ended up doing the less desirable thing of catching playwright error, inspecting the string message and if it's anything like regex Ofc this may not be suitable for everyone as their use case might be different, but for someone who otherwise wants to do something separate for playwright-errors except browser close (e.g you might want to backoff and retry if doing a web-scrape) and interpret browser-close as an unexpected irrecoverable scenario, then this is perhaps the most straightforward solution (unless somebody knows a better way ofc). |
@ustulation that's also what I'm doing. In case you want a pretty complete list of possible errors, I have one there: https://github.com/Lookyloo/PlaywrightCapture/blob/main/playwrightcapture/capture.py#L837 |
following. |
Still not fixed! Can not stop playwright correctly after KeyboardInterrupt, and infinite awation produced. finally:
print('Closing...')
_BLOCK_IMAGES = _BLOCK_IMAGES_old
try:
if (context is not None):
await context.close()
context = None
if (playwright is not None):
await playwright.stop()
playwright = None
print(context, playwright)
except:
pass
print('Done.[Closing]') |
Hello folks, Here's an OS tip related to this issue. Have you heard about PGID (Process Group ID)? The OS kernel knows your processes are running at the foreground of the IF you type Ctrl+c in a terminal, the terminal driver of the OS kernel sends If your program imports playwright package, and it creates playwright In this setup, if you hit Ctrl+c, then all of these processes receive SIGINT So in this case, just send SIGTERM to your main process instead of hitting I hope this tip helps. |
Note that if you want to use the monkeypatch, the latest version of playwright has slightly different code for launching the driver:
Also, annoyingly, when you stop sending the signal to the browser, sometimes the event loop is unable to close itself in a timely manner (it takes 10s of seconds to actually close). |
Just keep myself in the loop for the issue here. I have the same issue when try:
browser = ...
nice_exit()
except KeyboardInterruption
harsh_exit()
finally:
browser.close() triggers long error messages when the program is terminated by |
I have been using Playwright with the Scrapy web scraping framework, this is the plugin: https://github.com/scrapy-plugins/scrapy-playwright
Scrapy is designed to cleanly shutdown on SIGINT, saving its crawl state so that it can be resumed. When used with Playwright the SIGINT immediately closes the browsers (with both
handle_sigint=False
orTrue
), this results in the currently processing pages crashing and the shutdown failing with the state not being saved. The ideal way to fix this is to prevent the SIGINT from being passed to the browsers so that scrapy-playwright can handle their clean shutdown as active pages finish being processed.I have successfully made this work on posix by monkey patching Playwright based on this from stackoverflow to include a
preexec_fn
:What would be ideal is either a flag that could be set to enable this (for both Posix and Windows) or a way to pass a custom
preexec_fn
when starting a browser.See the related bug on scrapy-playwright scrapy-plugins/scrapy-playwright#62
The text was updated successfully, but these errors were encountered: