What causes OSError: Bad file descriptor error in python's selenium package?

Viewed 557

I use selenium to run django tests and I've never encountered this error before. Out of nowhere, hundreds of my tests began failing (though strangely enough not all of them, and not consistently - if I run tests one at a time, they run fine.) I have no idea why this started happening as there have been no changes to my environment.

From the traceback below, it looks like the error is being thrown while setting up the webdriver. Has anybody ever encountered this selenium error before and does anyone know how to fix it?

I'm using selenium==3.141.0 and python 3.6.3

Here's the test Class that seems to be the source of the error when running the tests:

from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from selenium import webdriver
from django.test import Client
from rest_framework.test import APIClient


class ViewTestCase(StaticLiveServerTestCase):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.test_client = Client()
        self.api_client = APIClient()

    def setUp(self):
        super().setUp()
        self.selenium = webdriver.Chrome()

        self.selenium.set_window_size(1200, 678)
        self.selenium.implicitly_wait(5)
        self.open_page()

    def tearDown(self):
        self.selenium.quit()
        super().tearDown()

    def open_page(self):
        self.selenium.get('{}/{}'.format(self.live_server_url, self.page_url))

Error tracelogs:

Traceback (most recent call last):
  File "/Users/daniellong/Documents/Coding/GrubBase/GrubBase/tests/__init__.py", line 174, in setUp
    self.selenium = webdriver.Chrome()
  File "/Users/daniellong/Documents/Coding/GrubBase/venv/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
    self.service.start()
  File "/Users/daniellong/Documents/Coding/GrubBase/venv/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 76, in start
    stdin=PIPE)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 1344, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
OSError: [Errno 9] Bad file descriptor
1 Answers

This error message...

Traceback (most recent call last):
  File "/Users/daniellong/Documents/Coding/GrubBase/GrubBase/tests/__init__.py", line 174, in setUp
    self.selenium = webdriver.Chrome()
  File "/Users/daniellong/Documents/Coding/GrubBase/venv/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
.
.
    raise child_exception_type(errno_num, err_msg, err_filename)
OSError: [Errno 9] Bad file descriptor

and your observations within your comments ...the error happens inconsistently. It only happens when I run the full test suite. If I run a test by itself, it runs fine....

...implies that the ChromeDriver was unable to initiate/spawn a new Browsing Context i.e. Chrome Browser session intermittently.

Presumably your observation suggests your code is flawless but the problem lies with quiting the Chrome browser and reinitializing it. In general, when OSError: [Errno 9] Bad file descriptor is encountered when the socket file descriptor you are trying to use to initiate the new session is not valid, which has multiple possible reasons:

  • The file descriptor is already closed somewhere.
  • The file descriptor is inconsistent.
  • Your system is out of memory.

The snapshot of memory usage would have helped us to analyze the issue in the better way. However a generic approach will be to:


References

You can find a couple of relevant detailed discussions in:

Related