jupyter-dash icon indicating copy to clipboard operation
jupyter-dash copied to clipboard

OSError: Address "" already in use.

Open peuleu opened this issue 4 years ago • 33 comments

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.

peuleu avatar Aug 14 '20 12:08 peuleu

Help needed, I got the same Error too, thanks~

ngloom avatar Aug 19 '20 11:08 ngloom

Unfortunately, the code always print that error regardless of the true reason of the failure. I also got that error but the true cause was that jupyter-dash was trying to test for success (access the _alive url) in a hostname that is not available in my enviroinment (https://github.com/plotly/jupyter-dash/issues/32).

You can easily fix it inside the code of your module, and that's probably the workaround for now because the authors of this library don't seem to be very responsive.

ccdavid avatar Aug 21 '20 18:08 ccdavid

ccdavid, could i trouble you to detail your fix/workaround with sample code? thanks

kwtneo avatar Nov 27 '20 00:11 kwtneo

I have the same issue. No matter what I do I get the "Address already in use" error. I am running jupyterlab on a server and am port-forwarding to local machine. More info:

conda list | grep jupyter
jupyter                   1.0.0                    py37_7  
jupyter-dash              0.3.1              pyhd8ed1ab_0    conda-forge
jupyter-server-proxy      1.0                        py_0    plotly
jupyter_client            6.1.7                      py_0  
jupyter_console           6.2.0                      py_0  
jupyter_core              4.7.0            py37h06a4308_0  
jupyterlab                2.2.6                      py_0  
jupyterlab_pygments       0.1.2                      py_0  
jupyterlab_server         1.2.0                      py_0  
conda list | grep dash
dash                      1.17.0             pyhd8ed1ab_0    conda-forge
dash-core-components      1.13.0             pyhd8ed1ab_1    conda-forge
dash-html-components      1.1.1              pyh9f0ad1d_0    conda-forge
dash-renderer             1.8.3              pyhd8ed1ab_0    conda-forge
dash-table                4.11.0             pyhd8ed1ab_0    conda-forge
jupyter-dash              0.3.1              pyhd8ed1ab_0    conda-forge
jupyter labextension list
jupyterlab-dash v0.3.0  enabled  OK

Jupyterlab has been rebuilt Running the following (in individual cells):

from jupyter_dash import JupyterDash
from jupyter_dash.comms import _send_jupyter_config_comm_request
_send_jupyter_config_comm_request()
JupyterDash.infer_jupyter_proxy_config()
import jupyter_server_proxy
JupyterDash()._server_proxy
from jupyter_dash.comms import _jupyter_config
_jupyter_config

produces no error. Output from last two lines is

True
{'type': 'base_url_response',
 'server_url': 'http://127.0.0.1:7001',
 'base_subpath': '/',
 'frontend': 'jupyterlab'}

However, app.run_server() always throws "OSError: Address 'http://0.0.0.0:xxxx' already in use", no matter the mode or port number.

drorcohengithub avatar Dec 07 '20 03:12 drorcohengithub

Hello,

I am having the same issue. Whatever port i put, it displays the error: "OSError: Address 'http://0.0.0.0:xxxx' already in use" @ccdavid could you please tell us what solution you've found?

@jonmmease , do you have any idea about this?

octavd avatar Jan 11 '21 13:01 octavd

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.

erikmajlath avatar Jan 21 '21 09:01 erikmajlath

Hello all,

had the same error no matter what mode was used. The link to the address that is said to be already in use leads to an error page of the Flask server. The page displays a message that no layout was set in the app. The following snippet led to success:

from jupyter_dash import JupyterDash
import dash_html_components as html

app = JupyterDash(__name__)
app.layout = html.Div([
    html.H3("Largest evaluation of all time")
])
app.run_server(mode='jupyterlab')

captain-proton avatar Feb 05 '21 11:02 captain-proton

Is the port already in use by another service at the OS level? See how 2000 is in use but 2194-2196 are blank? http://www.networksorcery.com/enp/protocol/ip/ports02000.htm

layne-sadler avatar Feb 05 '21 15:02 layne-sadler

For my case, whatever port i've used it gave me that error.

octavd avatar Feb 05 '21 15:02 octavd

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

dragoljub avatar Mar 05 '21 19:03 dragoljub

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.

mintaow avatar Mar 10 '21 13:03 mintaow

@mintaow

What versions of dash and Jupyter and python are you using?

I tested the same code in Linux Virtual environment with same results on Python 3.8 and latest versions of Dash and Jupyter.

dragoljub avatar Mar 10 '21 20:03 dragoljub

@mintaow

What versions of dash and Jupyter and python are you using?

I tested the same code in Linux Virtual environment with same results on Python 3.8 and latest versions of Dash and Jupyter.

I am just using the default Google Colab settings for Python and Jupyter. I think Colab is using Python 3.6.9. For Dash, I am !pip install jupyter-dash every time I reconnect to Colab so I guess it is the latest version.

mintaow avatar Mar 11 '21 10:03 mintaow

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.

nijatullahmansoor avatar Apr 24 '21 12:04 nijatullahmansoor

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...

dorianhoeffner avatar Jun 30 '21 07:06 dorianhoeffner

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.

Jonaddo avatar Aug 11 '21 10:08 Jonaddo

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.

brentkendrick avatar Nov 17 '21 00:11 brentkendrick

Hey all, I had the above error in Google Colab. Restarting the kernel worked for me.

import os

if not os.getenv("IS_TESTING"):
    import IPython

    app = IPython.Application.instance()
    app.kernel.do_shutdown(True)

Hope this helps you too!

atishaye avatar Dec 18 '21 09:12 atishaye

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

pai-data avatar Dec 27 '21 17:12 pai-data

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?

aiqc avatar Mar 12 '22 13:03 aiqc

Here is a great example: if you actually click on the localhost link it will show you the underlying error that is not forwarded to Jupyter.

image

what's scary is that after I deduplicated that ID and reran everything... I still got the same port taken/ duplicate ID error so it was reallllly hung somewhere.

and then my CPU started to spike big time (600%+ at one point)

image

so things are leaking and stacking big time


Other times, I just forgot to create a fresh app variable app = JupyterDash(__name__, external_stylesheets=sheets) after changing my layout/callbacks.

So this may be as simple as an error message not being caught with except

aiqc avatar Mar 15 '22 12:03 aiqc

There is a method to shutdown a server on a specific port:

from jupyter_dash import JupyterDash
JupyterDash._terminate_server_for_port('127.0.0.1', 9991)

UPDATE: this method was removed when i upgraded to 0.4.2

aiqc avatar Mar 30 '22 13:03 aiqc

Arggg. _terminate_server_for_port() only works when the app is running cleanly.

It does not tear down the port associated with OSError: [Errno 48] Address already in use

aiqc avatar Mar 31 '22 14:03 aiqc

Same issue. I've just installed a fresh Jupyter notebook on the new laptop and loaded my project. Now regardless of port/mode, I do see the same error. I've tried to add addtitional noproxy localhost variations, and I tried different versions of jupyter-dash - does not help. Apparently, I observe the same thing as its' been mentioned above - if I just copy and past local host address from the Error message, I can see the app running without a problem. Would be nice to understand what it is.

grcasey avatar Apr 08 '22 16:04 grcasey

Just upgraded to jupyter_dash==0.4.2, and now AttributeError: type object 'JupyterDash' has no attribute '_terminate_server_for_port' so it can't be used to stop ports anymore.

At this point, I am going to remove this library from my project.

aiqc avatar Jun 27 '22 13:06 aiqc

Hi all,

I discovered that the line in the getting started notebook:

"When running in JupyterHub or Binder, call the infer_jupyter_config function to detect the proxy configuration. JupyterDash.infer_jupyter_proxy_config()"

provided the solution to this. Forget the bit about JupyterHub or Binder, it's needed on localhost too. Hope that helps. Meantime I'm beginning to think this plugin is too not yet ready for primetime for the article I'm writing, so I'm getting off my horse in midstream and hopping on IPywidgets and jupyterlab-manager. Cheers!

JohnLockwood avatar Sep 02 '22 16:09 JohnLockwood

The only thing that fixed this for me was uninstalling and reinstalling jupyter_dash with pip :/

spookyuser avatar Sep 26 '22 16:09 spookyuser

Had the same problem today. In case someone Googles this, my solution was quite straightforward: restart the kernel / VS Code. Everything worked fine after that.

lanchuhuong avatar Mar 18 '23 21:03 lanchuhuong

@lanchuhuong that is a best case scenario. Does not always work

aiqc avatar Mar 18 '23 21:03 aiqc

i added a loop to go over a list of ports to find that which is not in use.

# generate a list of ports to try
ports = range(8051, 8061)

for port in ports:
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        if s.connect_ex(('localhost', port)) != 0:
            app = dash.Dash(__name__)
            app.run_server(host="0.0.0.0", port=port, mode="external")
            break

KKEVINRAYWAL avatar Mar 29 '23 12:03 KKEVINRAYWAL