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

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


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 sysfrom contextlib import contextmanager@contextmanagerdef 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 processwith suppress_stderr():    self.browser.quit()


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:


if you are using run_subprocess() make detach_mode=True and try it