Skip to content

OSError: [Errno 9] Bad file descriptor with playwright #666

Open
@Zackoswald

Description

@Zackoswald

Issues

GitHub issues are for bugs. If you have questions, please ask them on the mailing list.

Checklist

  • Does your title concisely summarize the problem?
  • Did you include a minimal, reproducible example?
  • What OS are you using?
  • What version of Dramatiq are you using?
  • What did you do?
  • What did you expect would happen?
  • What happened?

What OS are you using?

Windows10

What version of Dramatiq are you using?

Dramatiq 1.17.1

What did you do?

I try to set up a task that get cookies from chrome opened by playwright, but it turn out to be failed that showing error: OSError: [Errno 9] Bad file descriptor, how can i solve this problem

What did you expect would happen?

expect to open up a chrome browser(not headless mode) but it failed

What happened?

Traceback (most recent call last):
  File "C:\Users\administrator\anaconda3\envs\python3.11\Lib\site-packages\dramatiq\worker.py", line 487, in process_message
    res = actor(*message.args, **message.kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\administrator\anaconda3\envs\python3.11\Lib\site-packages\dramatiq\actor.py", line 185, in __call__
    return self.fn(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\administrator\Desktop\j\study\huey\config.py", line 35, in start_get_cookies
  File "C:\Users\administrator\Desktop\j\study\huey\config.py", line 38, in start_get_cookies2
    def start_get_cookies():
        ^^^^^^^^^^^^^
  File "C:\Users\administrator\Desktop\j\study\huey\config.py", line 42, in get_cookies
    def start_get_cookies2():
        ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\administrator\anaconda3\envs\python3.11\Lib\site-packages\playwright\sync_api\_context_manager.py", line 77, in __enter__
    dispatcher_fiber.switch()
  File "C:\Users\administrator\anaconda3\envs\python3.11\Lib\site-packages\playwright\sync_api\_context_manager.py", line 56, in greenlet_main
    self._loop.run_until_complete(self._connection.run_as_sync())
  File "C:\Users\administrator\anaconda3\envs\python3.11\Lib\asyncio\base_events.py", line 654, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "C:\Users\administrator\anaconda3\envs\python3.11\Lib\site-packages\playwright\_impl\_connection.py", line 263, in run_as_sync
    await self.run()
  File "C:\Users\administrator\anaconda3\envs\python3.11\Lib\site-packages\playwright\_impl\_connection.py", line 272, in run
    await self._transport.connect()
  File "C:\Users\administrator\anaconda3\envs\python3.11\Lib\site-packages\playwright\_impl\_transport.py", line 133, in connect
    raise exc
  File "C:\Users\administrator\anaconda3\envs\python3.11\Lib\site-packages\playwright\_impl\_transport.py", line 120, in connect
    self._proc = await asyncio.create_subprocess_exec(
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\administrator\anaconda3\envs\python3.11\Lib\asyncio\subprocess.py", line 223, in create_subprocess_exec
    transport, protocol = await loop.subprocess_exec(
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\administrator\anaconda3\envs\python3.11\Lib\asyncio\base_events.py", line 1708, in subprocess_exec
    transport = await self._make_subprocess_transport(
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\administrator\anaconda3\envs\python3.11\Lib\asyncio\windows_events.py", line 399, in _make_subprocess_transport
    transp = _WindowsSubprocessTransport(self, protocol, args, shell,
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\administrator\anaconda3\envs\python3.11\Lib\asyncio\base_subprocess.py", line 36, in __init__
    self._start(args=args, shell=shell, stdin=stdin, stdout=stdout,
  File "C:\Users\administrator\anaconda3\envs\python3.11\Lib\asyncio\windows_events.py", line 929, in _start
    self._proc = windows_utils.Popen(
                 ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\administrator\anaconda3\envs\python3.11\Lib\asyncio\windows_utils.py", line 153, in __init__
    super().__init__(args, stdin=stdin_rfd, stdout=stdout_wfd,
  File "C:\Users\administrator\anaconda3\envs\python3.11\Lib\subprocess.py", line 992, in __init__
    errread, errwrite) = self._get_handles(stdin, stdout, stderr)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\administrator\anaconda3\envs\python3.11\Lib\subprocess.py", line 1403, in _get_handles
    errwrite = msvcrt.get_osfhandle(stderr)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
OSError: [Errno 9] Bad file descriptor

there is a demo i created to replay the error

from playwright.sync_api import sync_playwright

import dramatiq
from dramatiq.brokers.redis import RedisBroker
from dramatiq.results import Results
from dramatiq.results.backends import RedisBackend

CONF_REDIS = {
    'host': '127.0.0.1',
    'port': 6379,
    'db': 5,
    'decode_responses': False,
    'encoding': 'utf-8',
    'max_connections': 10,
}

backend = RedisBackend()
redis_broker = RedisBroker(**CONF_REDIS)
redis_broker.add_middleware(Results(backend=backend))
dramatiq.set_broker(redis_broker)


class HTTPTimeout(Exception):
    pass


def should_retry(retries_so_far, exception):
    return retries_so_far < 3 and isinstance(exception, HTTPTimeout)


@dramatiq.actor(retry_when=should_retry, store_results=True)
def send_message(message):
    print(f'received message: {message}')
    return f"backend received {message}"


@dramatiq.actor
def start_get_cookies():
    start_get_cookies2()


def start_get_cookies2():
    get_cookies()


def get_cookies():
    with sync_playwright() as p:
        browser = p.chromium.launch(headless=False)
        context = browser.new_context()
        context.clear_cookies()
        page = context.new_page()
        page.goto('https://www.google.com')


if __name__ == "__main__":
    # res_msg = send_message.send('hello world').get_result(block=True)
    # print(res_msg)

    res = start_get_cookies.send().get_result(block=True)
    print(res)

it cannot work both with playwright.sync_playwright or playwright.async_playwright

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions