question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if youā€™re still stuck at the end, weā€™re happy to hop on a call to see how we can help out.

ResourceWarning: unclosed socket upon driver crashes

See original GitHub issue

šŸ› Bug Report

When running tests in parallel with pytest-xdist Chromium webdriver creation occasionally crashes or holds up a thread indefinitely for varying reasons:

E                 File ".../selenium-test/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
E                   raise exception_class(message, screen, stacktrace)
E               selenium.common.exceptions.SessionNotCreatedException: Message: session not created
E               from disconnected: Unable to receive message from renderer
E                 (Session info: headless chrome=88.0.4324.182)
E                 File ".../selenium-test/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
E                   raise exception_class(message, screen, stacktrace)
E               selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed.
E                 (unknown error: DevToolsActivePort file doesn't exist)
E                 (The process started from chrome location /snap/chromium/1497/usr/lib/chromium-browser/chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

Iā€™ve been trying to work around it by just re-trying the creation, however, Iā€™m also getting unclosed socket warnings every time there is a crash:

E               During handling of the above exception, another exception occurred:
E
E               Traceback (most recent call last):
E                 File "<frozen importlib._bootstrap_external>", line 580, in _compile_bytecode
E               ResourceWarning: unclosed <socket.socket fd=19, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('127.0.0.1', 35816), raddr=('127.0.0.1', 59245)>

To Reproduce

Create a pytest test case file called test_case.py:

from pytest import fixture, mark
from selenium import webdriver


@fixture
def browser():
    options = webdriver.ChromeOptions()
    options.add_argument('--headless')
    options.add_argument(f'--window-size=1000,500')
    driver = webdriver.Chrome(executable_path='/snap/bin/chromium.chromedriver', options=options)
    yield driver
    driver.close()


@mark.parametrize('index', list(range(50)))
def test_create(browser, index):
    print(browser)

Install the following requirements into a virtual environment:

pytest~=6.2.2
pytest-xdist[psutil]~=2.2.1
selenium~=3.141.0

Add the following to pyproject.toml:

[tool.pytest.ini_options]
filterwarnings = ['error']

Run pytest -n 8 a few times. Note that the errors appear only occasionally (in only a few percent of the cases), therefore you may need to change the number of parallel test runs (-n 8) or the number of tests (list(range(50))) to see it appear.

Expected behavior

A Chromium webdriver is created correctly 100% of the times, or, at least there is no unclosed socket warnings on failed webdriver creation.

Environment

OS: Ubuntu 20.04 Browser: Chromium Browser version: 88.0.4324.182 (installed via snap) Browser Driver version: chromium.chromedriver 88.0.4324.182 Language Bindings version: Python 3.141.0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
AutomatedTestercommented, Mar 1, 2021

Truthfully itā€™s hard to know if this is related or not.

If we can clean up the crashes and it still occurs please raise a bug and I will look into it.

Iā€™ve not seen this issue before so think itā€™s unrelated to the PythonĀ bindings or pytest-dist but there appears to be a lot of noise we need to clear before we can go into it more On Mar 1, 2021, 10:45 AM +0000, Gergely KalmĆ”r , wrote:

Hi @AutomatedTester, thank you very much for the insight! What about the unclosed socket issue though, is that also outside of Seleniumā€™s control? ā€” You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

0reactions
GergelyKalmarcommented, Mar 6, 2021

Iā€™ve been able to create a simpler test case which shows the issue more clearly.

Here is the test case:

from selenium import webdriver

for i in range(50):
    print(f'*** Run {i}')
    options = webdriver.ChromeOptions()
    options.add_argument('--headless')
    driver = webdriver.Chrome(options=options)
    print(driver)
    driver.quit()

Run it as python3 -Wd -X tracemalloc=25 test_case.py.

Note that the unclosed socket warning does appear in some runs (in a quite flaky way).

Also note that when using driver = webdriver.Chrome(options=options, keep_alive=False) the issue appears every time. The problem seems to be similar to what has been discussed in https://github.com/SeleniumHQ/selenium/issues/6878 and is related to how urllib3.PoolManager instances are managed in the RemoteConnection class (they are not cleaned up properly). When applying this patch it seems to solve the issue when keep_alive=False is set.

This issue appears to be fixed in the current version of https://github.com/SeleniumHQ/selenium/blob/trunk/py/selenium/webdriver/remote/remote_connection.py, so perhaps this will be resolved in 4.0.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ResourceWarning unclosed socket in Python 3 Unit Test
1) Expose a method to close the session, and call it in the test tearDown ... unittest 's tearDown method allows you to...
Read more >
Against ResourceWarnings in Python 3 - A. Jesse Jiryu Davis
There's a new annoyance in this function: it now junks up my terminal window with a ResourceWarning about an unclosed socket, just before...
Read more >
Chromecast Polyglot NodeServer - Page 2 - Universal Devices Forum
It looks like the nodeserver is connecting one by one to each of the chromecast devices then crashes. This is reflected in the...
Read more >
ResourceWarning: unclosed <socket.socket [closed] fd=3 ...
socket class based on the _socket.socket of the C module "_socket". The Python module has a private _closed attribute set to True as...
Read more >
raise runtimeerror('event loop is closed') - You.com | The Search ...
So, after the first job runs, you close the event loop. ... sys:1: ResourceWarning: unclosed <socket.socket fd=724, family=AddressFamily.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found