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:
- Created 3 years ago
- Comments:7 (4 by maintainers)
Top GitHub Comments
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:
Iāve been able to create a simpler test case which shows the issue more clearly.
Here is the test case:
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 howurllib3.PoolManager
instances are managed in theRemoteConnection
class (they are not cleaned up properly). When applying this patch it seems to solve the issue whenkeep_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
.