diff options
author | Lars-Dominik Braun <lars@6xq.net> | 2019-07-11 11:08:25 +0200 |
---|---|---|
committer | Lars-Dominik Braun <lars@6xq.net> | 2019-07-11 11:10:22 +0200 |
commit | 8761275f1f569b747cb26578e1c3411e108fb8dd (patch) | |
tree | 8d77813e13f76d33192b92bfd302044d4d415f70 | |
parent | 46d6c7f296e8b29db307cd180f3743e36b29ffe3 (diff) | |
download | crocoite-8761275f1f569b747cb26578e1c3411e108fb8dd.tar.gz crocoite-8761275f1f569b747cb26578e1c3411e108fb8dd.tar.bz2 crocoite-8761275f1f569b747cb26578e1c3411e108fb8dd.zip |
devtools: Add more crash error handling
In case the whole browser crashes (rare) we will neither be able to
close the tab on __aexit__, nor send SIGTERM to it. Make sure we still
terminate gracefully.
-rw-r--r-- | crocoite/devtools.py | 29 |
1 files changed, 23 insertions, 6 deletions
diff --git a/crocoite/devtools.py b/crocoite/devtools.py index b6311df..412ab08 100644 --- a/crocoite/devtools.py +++ b/crocoite/devtools.py @@ -67,16 +67,29 @@ class Browser: self.tab = await Tab.create (**resp) return self.tab - async def __aexit__ (self, *args): + async def __aexit__ (self, excType, excValue, traceback): assert self.tab is not None assert self.session is not None + await self.tab.close () - async with self.session.get (self.url.with_path (f'/json/close/{self.tab.id}')) as r: - resp = await r.text () - assert resp == 'Target is closing' + + try: + async with self.session.get (self.url.with_path (f'/json/close/{self.tab.id}')) as r: + resp = await r.text () + assert resp == 'Target is closing' + except aiohttp.client_exceptions.ClientConnectorError: + # oh boy, the whole browser crashed instead + if excType is Crashed: + # exception is reraised by `return False` + pass + else: + # this one is more important + raise + self.tab = None await self.session.close () self.session = None + return False class TabFunction: @@ -321,8 +334,12 @@ class Process: return URL.build(scheme='http', host='localhost', port=port) async def __aexit__ (self, *exc): - self.p.terminate () - await self.p.wait () + try: + self.p.terminate () + await self.p.wait () + except ProcessLookupError: + # ok, fine, dead already + pass # Try to delete the temporary directory multiple times. It looks like # Chrome will change files in there even after it exited (i.e. .wait() |