Skip to content
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

[BUG] Unable to exit the main thread properly when the browser closes unexpectedly #70

Open
Nongzhsh opened this issue Nov 14, 2024 · 0 comments

Comments

@Nongzhsh
Copy link

Hi,

The program cannot exit normally when the browser is closed unexpectedly during script execution.
Maybe this issue isn't necessarily a bug, so any suggestions on how to exit the program gracefully when an error occurs?

Code:

    session = hrequests.Session()
    resp = session.get('https://www.bing.com/')
    with resp.render(headless=False, mock_human=True) as page:
        time.sleep(15)  # Manually close the browser within this time to simulate abnormal browser closing
        page.type('//textarea[@name="q"]', 'Hi!')

Outout:

...
    with resp.render(headless=False, mock_human=True) as page:
  File "/home/hi/.virtualenvs/projectA/lib/python3.10/site-packages/hrequests/browser/browser.py", line 157, in __exit__
    self.close()
  File "/home/hi/.virtualenvs/projectA/lib/python3.10/site-packages/hrequests/browser/browser.py", line 621, in close
    self.resp.raw = self.page.content()
  File "/home/hi/.virtualenvs/projectA/lib/python3.10/site-packages/hrequests/browser/engine.py", line 193, in method
    result = attr(*args, **kwargs)
  File "/home/hi/.virtualenvs/projectA/lib/python3.10/site-packages/hrequests/browser/engine.py", line 200, in method
    return _engine.execute(task)
  File "/home/hi/.virtualenvs/projectA/lib/python3.10/site-packages/hrequests/browser/engine.py", line 163, in execute
    return self.__handle_future(future)
  File "/home/hi/.virtualenvs/projectA/lib/python3.10/site-packages/hrequests/browser/engine.py", line 154, in __handle_future
    return future.result()
  File "/usr/lib/python3.10/concurrent/futures/_base.py", line 458, in result
    return self.__get_result()
  File "/usr/lib/python3.10/concurrent/futures/_base.py", line 403, in __get_result
    raise self._exception
  File "/home/hi/.virtualenvs/projectA/lib/python3.10/site-packages/hrequests/browser/engine.py", line 197, in task
    res = await result
  File "/home/hi/.virtualenvs/projectA/lib/python3.10/site-packages/playwright/async_api/_generated.py", line 8867, in content
    return mapping.from_maybe_impl(await self._impl_obj.content())
  File "/home/hi/.virtualenvs/projectA/lib/python3.10/site-packages/playwright/_impl/_page.py", line 536, in content
    return await self._main_frame.content()
  File "/home/hi/.virtualenvs/projectA/lib/python3.10/site-packages/playwright/_impl/_frame.py", line 416, in content
    return await self._channel.send("content")
  File "/home/hi/.virtualenvs/projectA/lib/python3.10/site-packages/playwright/_impl/_connection.py", line 59, in send
    return await self._connection.wrap_api_call(
  File "/home/hi/.virtualenvs/projectA/lib/python3.10/site-packages/playwright/_impl/_connection.py", line 520, in wrap_api_call
    raise rewrite_error(error, f"{parsed_st['apiName']}: {error}") from None
playwright._impl._errors.TargetClosedError: Page.content: Target page, context or browser has been closed

... # Thread hangs

Env:

hrequests[all]==0.9.1
Python 3.10.12
WSL2 (Ubuntu 22.04.4 LTS)

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

No branches or pull requests

1 participant