Fix cleanup on exception in sync_api __enter__

If an exception is raised in the synchronous api during the setup, super().__enter__ has been called but super().__exit__ will not get called, leading to asyncio runloop exceptions further down the line if one tries to create a new context. This commit catches the exception in order to clean up the super().__exit__ calls before reraising.
This commit is contained in:
wv-opt-ai 2025-03-21 16:35:11 +01:00 committed by GitHub
parent 95cc4489d0
commit e008580521
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -26,7 +26,11 @@ class Camoufox(PlaywrightContextManager):
def __enter__(self) -> Union[Browser, BrowserContext]:
super().__enter__()
self.browser = NewBrowser(self._playwright, **self.launch_options)
try:
self.browser = NewBrowser(self._playwright, **self.launch_options)
except camoufox.exceptions.InvalidProxy as e:
super().__exit__(camoufox.exceptions.InvalidProxy, e, None)
raise
return self.browser
def __exit__(self, *args: Any):