jupyter-dash: OSError: Address "" already in use.

Hi all!

I’m running into an error when trying to run app.run_server(mode='external')

Even if I kill all ports, and even if I change the port to a random number (for instance: app.run_server(mode='external', port=2000)), the error still persists. I don’t know how to resolve this. Help would be appreciated! Thanks!

Full error:

---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-5-0a3a9f2580ba> in <module>
----> 1 app.run_server(mode='external', port=2000)

/opt/anaconda3/lib/python3.7/site-packages/jupyter_dash/jupyter_app.py in run_server(self, mode, width, height, inline_exceptions, **kwargs)
    317                 )
    318 
--> 319         wait_for_app()
    320 
    321         if JupyterDash._in_colab:

/opt/anaconda3/lib/python3.7/site-packages/retrying.py in wrapped_f(*args, **kw)
     47             @six.wraps(f)
     48             def wrapped_f(*args, **kw):
---> 49                 return Retrying(*dargs, **dkw).call(f, *args, **kw)
     50 
     51             return wrapped_f

/opt/anaconda3/lib/python3.7/site-packages/retrying.py in call(self, fn, *args, **kwargs)
    210                 if not self._wrap_exception and attempt.has_exception:
    211                     # get() on an attempt with an exception should cause it to be raised, but raise just in case
--> 212                     raise attempt.get()
    213                 else:
    214                     raise RetryError(attempt)

/opt/anaconda3/lib/python3.7/site-packages/retrying.py in get(self, wrap_exception)
    245                 raise RetryError(self)
    246             else:
--> 247                 six.reraise(self.value[0], self.value[1], self.value[2])
    248         else:
    249             return self.value

/opt/anaconda3/lib/python3.7/site-packages/six.py in reraise(tp, value, tb)
    701             if value.__traceback__ is not tb:
    702                 raise value.with_traceback(tb)
--> 703             raise value
    704         finally:
    705             value = None

/opt/anaconda3/lib/python3.7/site-packages/retrying.py in call(self, fn, *args, **kwargs)
    198         while True:
    199             try:
--> 200                 attempt = Attempt(fn(*args, **kwargs), attempt_number, False)
    201             except:
    202                 tb = sys.exc_info()

/opt/anaconda3/lib/python3.7/site-packages/jupyter_dash/jupyter_app.py in wait_for_app()
    313                     "Address '{url}' already in use.\n"
    314                     "    Try passing a different port to run_server.".format(
--> 315                         url=url
    316                     )
    317                 )

OSError: Address 'http://127.0.0.1:2000' already in use.
    Try passing a different port to run_server.

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Comments: 33 (6 by maintainers)

Most upvoted comments

app.run_server(mode=“inline”, host=“localhost”,port=8051)

so just pass different parameter port=? port number which is valid and not used I tried this it work for me OSError: Address ‘http://127.0.0.1:8050’ already in use. Try passing a different port to run_server.

The port busy error can also arise when you have two or more layout components that have the same id assigned. Usually the traceback will indicate a “DuplicateIdError” as well, but if you are importing layouts (e.g. from other python files) the only error that you might see is the “Address … already in use” error.

Hi guys,

So I after trying a lot of different ports I checked the rest of the code. And I noticed that I wrote:

app = JupyterDash(__name__, external_stylesheets='https://codepen.io/chriddyp/pen/bWLwgP.css')

instead of

app = JupyterDash(__name__, external_stylesheets=['https://codepen.io/chriddyp/pen/bWLwgP.css'])

Now it works perfectly in mode=‘inline’ and mode=‘external’ with all the ports 8050, 30000, 5000, etc…

So basically because I forgot to put the external stylesheet URL in a list I had the “OSError: Address ‘http://…’ already in use. Try passing a different port to run_server.”.

Conclusion, the error message is not necessarily due to wrong ip/port address.

I would prefer to have a python command to elegantly close the port as the code is shutdown, instead of leaving it open and giving an error the next time the program is run. However i find the following command is useful to close any blocking port (just replace 8050 with whatever port is causing a problem)

sudo lsof -t -i tcp:8050 | xargs kill -9

I’m running into the same issue. Its happening on Windows 10, Python 3.7 x64. Latest versions of JupyterLab Dash and JupyterDash. Once you run the cell in the notebook the dash server is started and I can navigate to it externally where everything works, but it does not show up inline and just gives the error regardless of the mode argument.

---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-1-c022ec548b22> in <module>
      6     html.H3("Largest evaluation of all time")
      7 ])
----> 8 app.run_server(mode='inline')

d:\python37\lib\site-packages\jupyter_dash\jupyter_app.py in run_server(self, mode, width, height, inline_exceptions, **kwargs)
    317                 )
    318 
--> 319         wait_for_app()
    320 
    321         if JupyterDash._in_colab:

d:\python37\lib\site-packages\retrying.py in wrapped_f(*args, **kw)
     47             @six.wraps(f)
     48             def wrapped_f(*args, **kw):
---> 49                 return Retrying(*dargs, **dkw).call(f, *args, **kw)
     50 
     51             return wrapped_f

d:\python37\lib\site-packages\retrying.py in call(self, fn, *args, **kwargs)
    210                 if not self._wrap_exception and attempt.has_exception:
    211                     # get() on an attempt with an exception should cause it to be raised, but raise just in case
--> 212                     raise attempt.get()
    213                 else:
    214                     raise RetryError(attempt)

d:\python37\lib\site-packages\retrying.py in get(self, wrap_exception)
    245                 raise RetryError(self)
    246             else:
--> 247                 six.reraise(self.value[0], self.value[1], self.value[2])
    248         else:
    249             return self.value

d:\python37\lib\site-packages\six.py in reraise(tp, value, tb)
    701             if value.__traceback__ is not tb:
    702                 raise value.with_traceback(tb)
--> 703             raise value
    704         finally:
    705             value = None

d:\python37\lib\site-packages\retrying.py in call(self, fn, *args, **kwargs)
    198         while True:
    199             try:
--> 200                 attempt = Attempt(fn(*args, **kwargs), attempt_number, False)
    201             except:
    202                 tb = sys.exc_info()

d:\python37\lib\site-packages\jupyter_dash\jupyter_app.py in wait_for_app()
    313                     "Address '{url}' already in use.\n"
    314                     "    Try passing a different port to run_server.".format(
--> 315                         url=url
    316                     )
    317                 )

OSError: Address 'http://127.0.0.1:8050' already in use.
    Try passing a different port to run_server.

jupyter-dash                       0.4.0

retrying                           1.3.3

dash                               1.19.0
dash-core-components               1.15.0
dash-html-components               1.1.2
dash-renderer                      1.9.0
dash-table                         4.11.2

jupyter --version
jupyter core     : 4.6.3
jupyter-notebook : 6.0.3
qtconsole        : 4.7.3
ipython          : 7.13.0
ipykernel        : 5.2.1
jupyter client   : 6.1.3
jupyter lab      : 3.0.9
nbconvert        : 5.6.1
ipywidgets       : 7.5.1
nbformat         : 5.0.6
traitlets        : 4.3.3

Hello,

had the same problem, just fixed it with

Run the application

if name == ‘main’: app.run_server(mode=‘external’, port=8052)

Changing the number port to 8052 (honestly, random), worked.

After spending a few days with jupyter-dash, it feels like the “port already in use” is really just the app hanging due to uncaught errors (e.g. bad stylesheet assignment) during initialization.

It’s like the webserver half of the program is waiting for the app half of the program to be ready… then either failing to release the port when the app building fails or just claiming that error because it doesn’t know what else to do. An error isn’t being caught on the app side.

https://github.com/plotly/jupyter-dash/blob/9599b0210728c0d218219209a06efb3e8de6342f/jupyter_dash/jupyter_app.py#L303

        # Wait for app to respond to _alive endpoint
        @retry(
            stop_max_attempt_number=15,
            wait_exponential_multiplier=10,
            wait_exponential_max=1000
        )
        def wait_for_app():
            res = requests.get(alive_url).content.decode()
            if res != "Alive":
                url = "http://{host}:{port}".format(
                    host=host, port=port, token=JupyterDash._token
                )
                raise OSError(
                    "Address '{url}' already in use.\n"
                    "    Try passing a different port to run_server.".format(
                        url=url
                    )
                )

        wait_for_app()

I can rerun an app on the same port repeatedly without a problem.

Could the url be pushed to the server rather than pulled?

If you go in your variable browser after executing the “faulty” code and you see your app (instance of dash.Dash), you might just type “del app” to delete this instance. Afterwards it worked for me…

Hey folks, not sure if my solution helps because my error is “NoLayout Exception” even though I did create them, but there is the same message "OSError: Address 'http://127.0.0.1:2000' already in use. Try passing a different port to run_server." at the bottom.

I am working on Google Colab. I just restarted the Colab, and things got solved.

Hello,

Error that we are encountering is as @ccdavid mentioned default error anytime python context cannot connect to the spawned dash server. As seen in the https://github.com/plotly/jupyter-dash/blob/master/jupyter_dash/jupyter_app.py#L306

def wait_for_app():
    res = requests.get(alive_url).content.decode()
    if res != "Alive":
        url = "http://{host}:{port}".format(
            host=host, port=port, token=JupyterDash._token
        )
        raise OSError(
            "Address '{url}' already in use.\n"
            "    Try passing a different port to run_server.".format(
                url=url
            )
        )

After you call app.run_server(mode='external', port=8050) (or other mode, it does not matter) it should spawn dash server. You can chceck by sudo netstat -nlp | grep 8050 if the server is running you should see something like this:

tcp        0      0 127.0.0.1:8050          0.0.0.0:*               LISTEN      2537/python

Second option is to curl localhost with correct port to see if it’s accessible curl http://localhost:8050 should return dash-like response for example:

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta charset="UTF-8">
        <title>Dash</title>
...

If dash server is running successfully and you are getting error mentioned above there is an issue with connecting from python to the localhost. Solution will depend on your setup. My problem was that container in which I run jupyter lab used proxy and was connecting to different localhost. Ignoring proxy for localhost solved my problem.