Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host error with ChromeDriver Chrome Selenium Django

I am learning TDD by the "Obey the testing goat book", but I am trying to do it with Django 3.

If anyone knows it, I am at Chapter 6.

My code is:

class VisitorTest(LiveServerTestCase):

    def setUp(self):
        self.browser = webdriver.Chrome()
        self.browser.implicitly_wait(2)

    def tearDown(self):
        self.browser.quit()

    def test_starting(self):
        print(self.live_server_url) 

        self.browser.get(self.live_server_url)

and in console I am getting


Creating test database for alias 'default'...
System check identified no issues (0 silenced).

DevTools listening on ws://127.0.0.1:52187/devtools/browser/e9a03a04-819e-40a3-a0e4-bd4133d8f6cb
http://localhost:52180
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 52204)
----------------------------------------
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 52202)
Exception happened during processing of request from ('127.0.0.1', 52203)
Traceback (most recent call last):
Traceback (most recent call last):
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python36\lib\socketserver.py", line 654, in process_request_thread
    self.finish_request(request, client_address)
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python36\lib\socketserver.py", line 654, in process_request_thread
    self.finish_request(request, client_address)
Traceback (most recent call last):
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python36\lib\socketserver.py", line 364, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python36\lib\socketserver.py", line 364, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python36\lib\socketserver.py", line 724, in __init__
    self.handle()
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python36\lib\socketserver.py", line 724, in __init__
    self.handle()
  File "C:\Users\Alex\PycharmProjects\goat\lib\site-packages\django\core\servers\basehttp.py", line 172, in handle
    self.handle_one_request()
  File "C:\Users\Alex\PycharmProjects\goat\lib\site-packages\django\core\servers\basehttp.py", line 172, in handle
    self.handle_one_request()
  File "C:\Users\Alex\PycharmProjects\goat\lib\site-packages\django\core\servers\basehttp.py", line 182, in handle_one_request
    self.raw_requestline = self.rfile.readline(65537)
  File "C:\Users\Alex\PycharmProjects\goat\lib\site-packages\django\core\servers\basehttp.py", line 182, in handle_one_request
    self.raw_requestline = self.rfile.readline(65537)
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python36\lib\socket.py", line 586, in readinto
    return self._sock.recv_into(b)
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python36\lib\socket.py", line 586, in readinto
    return self._sock.recv_into(b)
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host
----------------------------------------
----------------------------------------
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python36\lib\socketserver.py", line 654, in process_request_thread
    self.finish_request(request, client_address)
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python36\lib\socketserver.py", line 364, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python36\lib\socketserver.py", line 724, in __init__
    self.handle()
  File "C:\Users\Alex\PycharmProjects\goat\lib\site-packages\django\core\servers\basehttp.py", line 174, in handle
    self.handle_one_request()
  File "C:\Users\Alex\PycharmProjects\goat\lib\site-packages\django\core\servers\basehttp.py", line 182, in handle_one_request
    self.raw_requestline = self.rfile.readline(65537)
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python36\lib\socket.py", line 586, in readinto
    return self._sock.recv_into(b)
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host
----------------------------------------
.
----------------------------------------------------------------------
Ran 1 test in 8.779s

OK
Destroying test database for alias 'default'...

However tests are running well (I didn`t displayed them here).

I made a research but didn`t found anything conclusive

Any idea why is displaying those and how to fix them?

like image 356
AAALLL3XXX Avatar asked Jan 07 '20 17:01

AAALLL3XXX


2 Answers

I've tried different suggested solutions unsuccessfully:

  • changing test server address from "localhost" to "127.0.0.1"
  • updating old ChromeDriver, 'cause my Chrome 83 is not officially supported by Anaconda's ChromeDriver 2.38
  • adding self.browser.refresh() before calling quit() (as the book itself suggest)
  • using Edge browser (well... IE driver doesn't have this problem and I haven't tried FF)

If we look deeper this kind of errors is handled by LiveServerTestCase.server_thread.httpd.handle_error function which default implementation in socketserver.BaseServer just prints error messages to stderr. As these messages are harmless I've decided to suppress them.

import sys
from contextlib import contextmanager

@contextmanager
def suppress_stderr():
    "Temporarly suppress writes to stderr"
    class Null:
        write = lambda *args: None
    err, sys.stderr = sys.stderr, Null
    try:
        yield
    finally:
        sys.stderr = err

# Suppress stderr messages during quit process
with suppress_stderr():
    self.browser.quit()
like image 93
Winand Avatar answered Sep 22 '22 07:09

Winand


This error message...

ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

...implies that the connection between the ChromeDriver and the Browsing Context i.e. Chrome Browser session was closed intermittemtly.

This issue is observed when incompatibility between the version of the binaries you are using.


Solution

Ensure that:

  • Selenium is upgraded to current levels Version 3.141.59.
  • ChromeDriver is updated to current ChromeDriver v79.0.3945.36 level.
  • Chrome is updated to current Chrome Version 79.0 level. (as per ChromeDriver v79.0 release notes)
  • Clean your Project Workspace through your IDE and Rebuild your project with required dependencies only.
  • (WindowsOS only) Use CCleaner tool to wipe off all the OS chores before and after the execution of your Test Suite.
  • (LinuxOS only) Free Up and Release the Unused/Cached Memory in Ubuntu/Linux Mint before and after the execution of your Test Suite.
  • If your base Web Client version is too old, then uninstall it and install a recent GA and released version of Web Client.
  • Take a System Reboot.
  • Execute your @Test as non-root user.
  • Always invoke driver.quit() within tearDown(){} method to close & destroy the WebDriver and Web Client instances gracefully.

Reference

You can finda couple of relevant discussions in:

  • urllib3.exceptions.ProtocolError: ('Connection aborted.', error(10054, 'An existing connection was forcibly closed by the remote host'))
like image 44
undetected Selenium Avatar answered Sep 24 '22 07:09

undetected Selenium