Skip to content

[BUG] Playwright should cleanly shutdown driver in sync_api  #818

Closed

Description

Platform : Windows 10 x64

Minimal reproducer:

from playwright.sync_api import Playwright, sync_playwright


def run(playwright: Playwright) -> None:
    browser = playwright.chromium.launch(headless=False)
    context = browser.new_context()

    page = context.new_page()

    page.goto("https://github.com/")
    page.wait_for_timeout(10e5)
    page.close()

    context.close()
    browser.close()


with sync_playwright() as playwright:
    run(playwright)

Wait for the browser to open and then CTRL+C to force exit, playwright does not closes the driver properly and tries to write to non-existing pipes.

Stacktrace:

Traceback (most recent call last):
  File "D:\playwright-python\main.py", line 19, in <module>
    run(playwright)
  File "D:\playwright-python\main.py", line 11, in run
    page.wait_for_timeout(10e5)
  File "D:\playwright-python\playwright\sync_api\_generated.py", line 7600, in wait_for_timeout
    self._sync(
  File "D:\playwright-python\playwright\_impl\_sync_base.py", line 109, in _sync
    self._dispatcher_fiber.switch()
  File "D:\playwright-python\playwright\sync_api\_context_manager.py", line 48, in greenlet_main
    loop.run_until_complete(self._connection.run_as_sync())
  File "c:\users\kumar aditya\appdata\local\programs\python\python39\lib\asyncio\base_events.py", line 629, in run_until_complete
    self.run_forever()
  File "c:\users\kumar aditya\appdata\local\programs\python\python39\lib\asyncio\windows_events.py", line 316, in run_forever
    super().run_forever()
  File "c:\users\kumar aditya\appdata\local\programs\python\python39\lib\asyncio\base_events.py", line 596, in run_forever
    self._run_once()
  File "c:\users\kumar aditya\appdata\local\programs\python\python39\lib\asyncio\base_events.py", line 1854, in _run_once
    event_list = self._selector.select(timeout)
  File "c:\users\kumar aditya\appdata\local\programs\python\python39\lib\asyncio\windows_events.py", line 434, in select
    self._poll(timeout)
  File "c:\users\kumar aditya\appdata\local\programs\python\python39\lib\asyncio\windows_events.py", line 783, in _poll
    status = _overlapped.GetQueuedCompletionStatus(self._iocp, ms)
KeyboardInterrupt
Task was destroyed but it is pending!
task: <Task pending name='Task-7' coro=<Page.wait_for_timeout() done, defined at D:\playwright-python\playwright\_impl\_page.py:721> wait_for=<Task pending name='Task-8' coro=<sleep() running at c:\users\kumar aditya\appdata\local\programs\python\python39\lib\asyncio\tasks.py:654> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x000002308DC80190>()]> cb=[<TaskWakeupMethWrapper object at 0x000002308DC801C0>()]> cb=[SyncBase._sync.<locals>.callback() at D:\playwright-python\playwright\_impl\_sync_base.py:104]>
Task was destroyed but it is pending!
task: <Task pending name='Task-8' coro=<sleep() running at c:\users\kumar aditya\appdata\local\programs\python\python39\lib\asyncio\tasks.py:654> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x000002308DC80190>()]> cb=[<TaskWakeupMethWrapper object at 0x000002308DC801C0>()]>
Exception ignored in: <function BaseSubprocessTransport.__del__ at 0x000002308D3353A0>
Traceback (most recent call last):
  File "c:\users\kumar aditya\appdata\local\programs\python\python39\lib\asyncio\base_subprocess.py", line 125, in __del__
  File "c:\users\kumar aditya\appdata\local\programs\python\python39\lib\asyncio\base_subprocess.py", line 70, in __repr__
  File "c:\users\kumar aditya\appdata\local\programs\python\python39\lib\asyncio\proactor_events.py", line 79, in __repr__
  File "c:\users\kumar aditya\appdata\local\programs\python\python39\lib\asyncio\windows_utils.py", line 102, in fileno
ValueError: I/O operation on closed pipe
Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x000002308D336C10>
Traceback (most recent call last):
  File "c:\users\kumar aditya\appdata\local\programs\python\python39\lib\asyncio\proactor_events.py", line 115, in __del__
  File "c:\users\kumar aditya\appdata\local\programs\python\python39\lib\asyncio\proactor_events.py", line 79, in __repr__
  File "c:\users\kumar aditya\appdata\local\programs\python\python39\lib\asyncio\windows_utils.py", line 102, in fileno
ValueError: I/O operation on closed pipe
ERROR: The process with PID 15060 (child process of PID 4464) could not be terminated.
Reason: The operation attempted is not supported.

ERROR: The process with PID 4464 (child process of PID 13744) could not be terminated.
Reason: There is no running instance of the task.

The process tried to write to a nonexistent pipe.

Activity

  1. mxschmitt commented on Jul 22, 2021

    @mxschmitt
    Contributor

    What would be your recommended approach to fix this?

  2. kumaraditya303 commented on Jul 22, 2021

    @kumaraditya303
    ContributorAuthor

    What would be your recommended approach to fix this?

    To fix this issue we should clear all the pipes upon context manager exit and wait for the process to exit before interpreter shutdown. #819

  3. pavelfeldman commented on Aug 12, 2021

    @pavelfeldman
    Member

    I am not sure this is worth fixing - my read on keyboard interrupt is that it can happen at any moment. Here you are handling it in run_until_complete, so there are good chances you catch it, but it leaves two questions:

    1. What if we are not inside run_until_complete when program is interrupted
    2. What if program is interrupted due to another exception, do we still talk to the driver which is in the invalid state?

    I think your fix only raises more questions.

  4. kumaraditya303 commented on Aug 12, 2021

    @kumaraditya303
    ContributorAuthor

    What if we are not inside run_until_complete when program is interrupted

    Since there is one main greenlet where the dispatcher switches, whenever the process is reading from driver the current call is in the run_until_complete hence that is the place which needs to be fixed

    What if program is interrupted due to another exception, do we still talk to the driver which is in the invalid state?

    What exception you are talking about and also from which it inherits? If it is RuntimeError then we should not do anything.

    Also because of the current bug the terminal hangs at least on windows when CTRL-C is pressed and leaves the current shell unusable.

    You should also try the solution and see if it fixes yourself on Windows 10.

  5. kumaraditya303 commented on Dec 3, 2021

    @kumaraditya303
    ContributorAuthor

    Seems that it will not be fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions