2

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
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Daniel Long
  • 1,162
  • 14
  • 30
  • 1
    Could you show the code you trying to run? – Dmitry Belaventsev Sep 18 '20 at 15:26
  • In general, "OSError: [Errno 9] Bad file descriptor" is raised when a python file was closed by something which is not a "close()" method of current file-object. So I can tell you more with listing of your code. – Dmitry Belaventsev Sep 18 '20 at 15:30
  • Thanks Dmitry. Added the source code for the test class where the webdriver is being initialized – Daniel Long Sep 18 '20 at 15:42
  • Please check this page https://pypi.org/project/selenium/ "Driver" section. Do you have chrome driver installed? – Dmitry Belaventsev Sep 18 '20 at 16:23
  • Yes, I have chrome driver installed. And I'm able to use the driver and selenium just fine most of the time. The strangest part about this is the error happens inconsistently. It only happens when I run the full test suite. If I run a test by itself, it runs fine. – Daniel Long Sep 18 '20 at 16:35
  • This might be the root of the problem. Maybe in another test selenium was used in a wrong way. Can you check other tests? Is there something related to "quit()" method. Or maybe you have some custom test runner. – Dmitry Belaventsev Sep 18 '20 at 16:38
  • All of the tests that are failing are flowing through this TestClass. And from the traceback it looks to me like the error is being fired in the setUp method, which should mean that the tearDown and the actual selenium.quit() command should never be reached correct? – Daniel Long Sep 18 '20 at 16:46
  • Yes, exactly, according to that traceback - the error is happening during instantiation of chrome webdriver. Can you share the full output of `python manage.py test -v 2`? – Dmitry Belaventsev Sep 18 '20 at 16:49
  • and btw what version of selenium do you use? I see some other code here https://github.com/SeleniumHQ/selenium/blob/3b5a62ca34a9715ee2f02ae92622503821193f4c/py/selenium/webdriver/common/service.py#L77 maybe update to latest? – Dmitry Belaventsev Sep 18 '20 at 17:38
  • That it hapens sort of randomly usually indicates a timing issue. Try adding a sleep in between your driver.quit and the next new driver instance.... so in your case I think after the quit... (Sometimes the browser needs time to cleanup on quit...) – pcalkins Sep 18 '20 at 21:12

1 Answers1

1

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:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352