MaxRetryError: HTTPConnectionPool: Max retries exceeded (Caused by ProtocolError(‘Connection aborted.’, error(111, ‘Connection refused’)))

This error message…

MaxRetryError: HTTPConnectionPool(host="127.0.0.1", port=51379): Max retries exceeded with url: /session/2e64d2a1-3c7f-4221-96fe-9d0b1c102195/window (Caused by ProtocolError('Connection aborted.', error(111, 'Connection refused')))

…implies that the call to self.driver.close() method failed raising MaxRetryError.


A couple of things:

  • First and foremost as per the discussion max-retries-exceeded exceptions are confusing the traceback is somewhat misleading. Requests wraps the exception for the users convenience. The original exception is part of the message displayed.

  • Requests never retries (it sets the retries=0 for urllib3’s HTTPConnectionPool), so the error would have been much more canonical without the MaxRetryError and HTTPConnectionPool keywords. So an ideal Traceback would have been:

      ConnectionError(<class 'socket.error'>: [Errno 1111] Connection refused)
    
  • But again @sigmavirus24 in his comment mentioned …wrapping these exceptions make for a great API but a poor debugging experience…

  • Moving forward the plan was to traverse as far downwards as possible to the lowest level exception and use that instead.

  • Finally this issue was fixed by rewording some exceptions which has nothing to do with the actual connection refused error.


Solution

Even before self.driver.close() within tearDown(self) is invoked, the try{} block within test_select_in_sina(self) includes finally{} where you have invoked driver.quit()which is used to call the /shutdown endpoint and subsequently the web driver & the client instances are destroyed completely closing all the pages/tabs/windows. Hence no more connection exists.

You can find a couple of relevant detailed discussion in:

In such a situation when you invoke self.driver.close() the client is unable to locate any active connection to initiate a clousure. Hence you see the error.

So a simple solution would be to remove the line driver.quit() i.e. remove the finally block.


tl; dr

As per the Release Notes of Selenium 3.14.1:

* Fix ability to set timeout for urllib3 (#6286)

The Merge is: repair urllib3 can’t set timeout!


Conclusion

Once you upgrade to Selenium 3.14.1 you will be able to set the timeout and see canonical Tracebacks and would be able to take required action.


References

A couple of relevent references:

Leave a Comment