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.

"Exception: Unable to find a free port" - SeGrid/Python/Chromedriver/Ubuntu+OSX

See original GitHub issue

Short Description: on attempting to execute parallel tests on a host using selenium grid, the server-standalone hub accepts the first session and rejects all subsequent requests to create a session until the first terminates.

Longer Description: I am not a java developer, so I can only dig so deeply, but it does look like something is not being correctly executed in the PortProber.java class. However, it may be something to do with the python requests to create a new session. I spent some time digging into the python code and wasn’t able to find a way to force a particular port be requested for the new session.

We’re trying to run multiple scripts on a headless virtual. This pattern has worked previously, but we needed to make updates to our chrome version, so we went ahead and updated the selenium-standalone version (I’ve tried 2.49-2.52), chromedriver (2.20-2.21), and Chrome (48.0.2564.82). In fact, in developing the python test script, I was able to run 4 sessions simultaneously on my OSX laptop. That is no longer the case (server standalone 2.50/2.52 tried - 2.50 previously worked)

I did try the selenium-server-standalone + a quick webdriver.io (nodeJS) script and it appears to work on my laptop.

Script to recreate: (start one instance, then start another on the same system)

#test.py
from selenium import webdriver
import time

baseURL='http://www.yahoo.com'
url2='http://www.bing.com'

options=webdriver.ChromeOptions()
options.add_argument('--disable-logging')
driver=webdriver.Remote(desired_capabilities=options.to_capabilities())

driver.get(baseURL)
time.sleep(2.5)
print(driver.title)

driver.get(url2)
time.sleep(2.5)
print(driver.title)

driver.get(baseURL)
time.sleep(2.5)
print(driver.title)

driver.get(url2)
time.sleep(2.5)
print(driver.title)

driver.close()
driver.quit()

Python Script Error:

>>>python test.py
Traceback (most recent call last):
  File "test.py", line 11, in <module>
    driver=webdriver.Remote(desired_capabilities=options.to_capabilities())
  File "/usr/local/lib/python2.7/dist-packages/selenium-2.51.1-py2.7.egg/selenium/webdriver/remote/webdriver.py", line 87, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "/usr/local/lib/python2.7/dist-packages/selenium-2.51.1-py2.7.egg/selenium/webdriver/remote/webdriver.py", line 141, in start_session
    'desiredCapabilities': desired_capabilities,
  File "/usr/local/lib/python2.7/dist-packages/selenium-2.51.1-py2.7.egg/selenium/webdriver/remote/webdriver.py", line 201, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python2.7/dist-packages/selenium-2.51.1-py2.7.egg/selenium/webdriver/remote/errorhandler.py", line 193, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Unable to find a free port

selenium-standalone-server error output:

10:59:41.649 INFO - Executing: [new session: Capabilities [{platform=ANY, javascriptEnabled=true, browserName=chrome, chromeOptions={args=[--disable-logging], extensions=[]}, version=}]])
10:59:41.650 INFO - Creating a new session for Capabilities [{platform=ANY, javascriptEnabled=true, browserName=chrome, chromeOptions={args=[--disable-logging], extensions=[]}, version=}]
10:59:41.652 WARN - Exception thrown
java.util.concurrent.ExecutionException: org.openqa.selenium.WebDriverException: java.lang.reflect.InvocationTargetException
Build info: version: '2.52.0', revision: '4c2593c', time: '2016-02-11 19:06:42'
System info: host: '**************', ip: '************', os.name: 'Linux', os.arch: 'amd64', os.version: '3.16.0-43-generic', java.version: '1.7.0_91'
Driver info: driver.version: unknown
  at java.util.concurrent.FutureTask.report(FutureTask.java:122)
  at java.util.concurrent.FutureTask.get(FutureTask.java:188)
  at org.openqa.selenium.remote.server.DefaultSession.execute(DefaultSession.java:183)
  at org.openqa.selenium.remote.server.DefaultSession.<init>(DefaultSession.java:119)
  at org.openqa.selenium.remote.server.DefaultSession.createSession(DefaultSession.java:95)
  at org.openqa.selenium.remote.server.DefaultDriverSessions.newSession(DefaultDriverSessions.java:124)
  at org.openqa.selenium.remote.server.handler.NewSession.handle(NewSession.java:59)
  at org.openqa.selenium.remote.server.handler.NewSession.handle(NewSession.java:1)
  at org.openqa.selenium.remote.server.rest.ResultConfig.handle(ResultConfig.java:111)
  at org.openqa.selenium.remote.server.JsonHttpCommandHandler.handleRequest(JsonHttpCommandHandler.java:79)
  at org.openqa.selenium.remote.server.DriverServlet.handleRequest(DriverServlet.java:202)
  at org.openqa.selenium.remote.server.DriverServlet.doPost(DriverServlet.java:164)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
  at org.openqa.selenium.remote.server.DriverServlet.service(DriverServlet.java:130)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
  at org.openqa.jetty.jetty.servlet.ServletHolder.handle(ServletHolder.java:428)
  at org.openqa.jetty.jetty.servlet.ServletHandler.dispatch(ServletHandler.java:680)
  at org.openqa.jetty.jetty.servlet.ServletHandler.handle(ServletHandler.java:571)
  at org.openqa.jetty.http.HttpContext.handle(HttpContext.java:1526)
  at org.openqa.jetty.http.HttpContext.handle(HttpContext.java:1479)
  at org.openqa.jetty.http.HttpServer.service(HttpServer.java:920)
  at org.openqa.jetty.http.HttpConnection.service(HttpConnection.java:820)
  at org.openqa.jetty.http.HttpConnection.handleNext(HttpConnection.java:986)
  at org.openqa.jetty.http.HttpConnection.handle(HttpConnection.java:837)
  at org.openqa.jetty.http.SocketListener.handleConnection(SocketListener.java:243)
  at org.openqa.jetty.util.ThreadedServer.handle(ThreadedServer.java:358)
  at org.openqa.jetty.util.ThreadPool$PoolThread.run(ThreadPool.java:537)
Caused by: org.openqa.selenium.WebDriverException: java.lang.reflect.InvocationTargetException
Build info: version: '2.52.0', revision: '4c2593c', time: '2016-02-11 19:06:42'
System info: host: '**********', ip: '**********', os.name: 'Linux', os.arch: 'amd64', os.version: '3.16.0-43-generic', java.version: '1.7.0_91'
Driver info: driver.version: unknown
  at org.openqa.selenium.remote.server.DefaultDriverProvider.callConstructor(DefaultDriverProvider.java:113)
  at org.openqa.selenium.remote.server.DefaultDriverProvider.newInstance(DefaultDriverProvider.java:97)
  at org.openqa.selenium.remote.server.DefaultDriverFactory.newInstance(DefaultDriverFactory.java:60)
  at org.openqa.selenium.remote.server.DefaultSession$BrowserCreator.call(DefaultSession.java:222)
  at org.openqa.selenium.remote.server.DefaultSession$BrowserCreator.call(DefaultSession.java:1)
  at java.util.concurrent.FutureTask.run(FutureTask.java:262)
  at org.openqa.selenium.remote.server.DefaultSession$1.run(DefaultSession.java:176)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
  at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
  at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
  at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
  at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
  at org.openqa.selenium.remote.server.DefaultDriverProvider.callConstructor(DefaultDriverProvider.java:103)
  ... 9 more
Caused by: java.lang.RuntimeException: Unable to find a free port
  at org.openqa.selenium.net.PortProber.findFreePort(PortProber.java:67)
  at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:292)
  at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:88)
  at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:138)
  ... 14 more
10:59:41.658 WARN - Exception: Unable to find a free port

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:5
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
visionscapercommented, Oct 28, 2016

I had exactly the same exception in the selenium-standalone-server error output. It turned out that I had statements in my /etc/hosts file that I had to remove (or comment out), e.g.

127.0.0.1 my-site.local

...

192.168.100.48 localhost   
1reaction
DavidGDDcommented, May 29, 2017

I have the same problem running on Linux. So after reading the responses and a week thinking that the problem was in my localhost definition or webdriver/driver/selenium versions, i’ve read the PortProber.java and LinuxEphemeralPortRangeDetector.java. There is a case in createAcceptablePort in which is always returning the same port, not random.

When you have defined in the “/proc/sys/net/ipv4/ip_local_port_range” the port range 1024-65535, the method returns always 1024. So if you change your range defined in the file to 1025-65535 (or whatever) it will work returning random ports.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to find a free port" - SeGrid/Python/Chromedriver ...
Short Description: on attempting to execute parallel tests on a host using selenium grid, the server-standalone hub accepts the first ...
Read more >
Is there any way to solve "java.lang.RuntimeException
This error message... java.lang.RuntimeException: Unable to find a free port ...is a RuntimeException which is the superclass of those ...
Read more >
Unable to find a free port - Google Groups
Exception in thread "main" java.lang.RuntimeException: Unable to find a free port. at org.openqa.selenium.net.PortProber.findFreePort(PortProber.java:67).
Read more >
ClamAV "can't find any free port" - Xeams
Hello,. I've got Xeams setup on the same CentOS host as ClamAV. ClamAV is configured to listen on TCP Port 3310 (the default), ......
Read more >
Finding a Free Port in Java | Baeldung
This will cause an input/output exception in our Java application. In this quick tutorial, we'll look at how to check a specific port...
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