Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TensorBoard in Jupyter "localhost refused to connect" issue on Windows #2481

Closed
JimmyMcWeb opened this issue Jul 31, 2019 · 8 comments
Closed

Comments

@JimmyMcWeb
Copy link

JimmyMcWeb commented Jul 31, 2019

ENVIRONMENT:
About Jupyter Notebook

Server Information:
You are using Jupyter notebook.

The version of the notebook server is: 5.7.8
The server is running on this version of Python:
Python 3.7.3 (default, Mar 27 2019, 17:13:21) [MSC v.1915 64 bit (AMD64)]

Current Kernel Information:
Python 3.7.3 (default, Mar 27 2019, 17:13:21) [MSC v.1915 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 7.4.0 -- An enhanced Interactive Python. Type '?' for help.

I'm not sure where to start here other than to relay the issue that I can't fix!

I am learning neural networks and I am trying to run code within Jupyter notebooks.

Tensorboard is a useful visualisation tool and I thought that I had got it to work embedded into a Jupyter notebook. It worked once but then stopped connecting to the localhost.

I've tried to follow through solutions to this issue from StackOverflow and GitHub postings but they either reference commands without giving the context (i.e. environment) in which those commands should be executed or they just get closed off because nobody has responded or redirected to here, the Tensorboard hub.

I'm stuck and I am struggling because I don't fully understand how the various pieces of technology hang together.

So my understanding is this:

  1. Jupyter is effectively a server running under my OS (Windows 10)
  2. Processes within Jupyter run under that server/kernel
  3. If I have installed Tensorflow from a Jupyter notebook then all elements should be available within that 'virtualenv'
  4. Tensorboard is bundled with Tensorflow but need to be explicitly loaded from a notebook

What I don't really understand is how the port numbers are working.

  1. Port 8888 is reserved on localhost to run Jupyter
  2. Tensorboard wants to use port 6006
  3. What effect Tensorboard running under Jupyter has on port allocations, I don't know.

My code is this:

import os
import tensorflow as tf
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
%load_ext tensorboard
logs_base_dir = "./logs"
os.makedirs(logs_base_dir, exist_ok=True)
%tensorboard --logdir {logs_base_dir}

There are log files in the /logs directory created under another notebook.

The error message is this:

Reusing TensorBoard on port 6006 (pid 17596), started 1 day, 23:56:21 ago. (Use '!kill 17596' to kill it.)

I have shutdown the PC and restarted but this process seems to persist?
I can't find anything on port 6006 when I've run:
netstat -abno
from Windows cmd (as admin)

I've tried to guess how to use !kill 17596 but I am not guessing correctly!

In a nutshell I want to clear out the system memory and just run Tensorboard again, but it won't work! (but it did work once!)

I would be grateful for any help, but please assume that I don't know from which terminal / notebook commands need to be run from. I'd be grateful if you could make that explicit in any answers / suggestions.

@JimmyMcWeb
Copy link
Author

JimmyMcWeb commented Jul 31, 2019

OK, guess work ... but it seems to be responding again.

A quick scan of the docs on this site mentioned network security issue on Macs:
https://github.com/tensorflow/tensorboard#i-get-a-network-security-popup-every-time-i-run-tensorboard-on-a-mac

Under that heading it mentioned specifying "localhost" instead of the default "0.0.0.0"

I amended my code above to:

%tensorboard --logdir {logs_base_dir}  --host localhost

I ran the code again from Jupyter notebook. 1st attempt timed out, 2nd attempt brought up the Tensorboard dashboard, which I will claim is it working now.

Still not sure why it seems to have worked but I'll accept that right now it is!

@wchargin
Copy link
Contributor

@JimmyMcWeb: Thanks for the report! Responses below.

So my understanding is this: […]

That’s all correct. Running TensorBoard under Jupyter doesn’t affect the
port allocations; ports are a system-global resource.

The error message is this:

The “Reusing TensorBoard” message isn’t an error; it’s just an
informational message. Reusing a previously started TensorBoard instance
should usually be fine, but we let you know anyway just in case the
instance has somehow gotten into a bad state.

I've tried to guess how to use !kill 17596 but I am not guessing
correctly!

On Linux or macOS, you just write !kill 17596 in any IPython notebook
cell, or kill 17596 (no bang) in any terminal window—but it sounds
like you’re on Windows, so the situation is different.

I’ve just looked into the details, and it looks like there’s no simple
way to achieve the desired behavior on Windows. (You can use taskkill
to force-kill an arbitrary process given its ID, but as far as I can
tell, you can’t gracefully shut down any process unless it’s part of
your process group, and even then the situation is a bit complicated.)
So we’ll need to make a few structural changes to the notebook
integration to make it behave the same as on Linux and macOS.

I’ll update the messaging on Windows accordingly—thanks for bringing
this to our attention!

In a nutshell I want to clear out the system memory and just run
Tensorboard again

For a quick workaround, you can run the following commands in any
command prompt (cmd.exe):

taskkill /im tensorboard.exe /f
del /q %TMP%\.tensorboard-info\*

If either of those gives an error (probably “process "tensorboard.exe"
not found” or “the system cannot find the file specified”), that’s okay:
you can ignore it.

@wchargin
Copy link
Contributor

I ran the code again from Jupyter notebook. 1st attempt timed out, 2nd
attempt brought up the Tensorboard dashboard, which I will claim is it
working now.

Still not sure why it seems to have worked but I'll accept that right
now it is!

I’ve opened #2483 to track a better fix, so given the workaround above,
I think that we can close this issue—if something still seems awry,
please feel free to comment here and we can re-open it.

@JimmyMcWeb
Copy link
Author

@wchargin

Thanks William

Restarting work today (Th 1/8/19) I found that the "localhost refuses to connect" message was back when I asked Tensorboard to graph the log files created yesterday.

I ran

taskkill /im tensorboard.exe /f
del /q %TMP%\.tensorboard-info\*

from cmd.exe as you suggested.

1st command threw an error saying tensorboard wasn't running.
2nd command completed with no comment.

Back in Jupyter I reran the

logs_base_dir = "./logs"
os.makedirs(logs_base_dir, exist_ok=True)
%tensorboard --logdir {logs_base_dir}  --host localhost

commands.

1st attempt timed out again.
2nd attempt brought up the Tensorboard dashboard.

Thanks for your help. Glad it wasn't just me! I can progress again!

Best regards
James

@wchargin wchargin changed the title Tensorboard in Jupyter "localhost refused to connect" issue TensorBoard in Jupyter "localhost refused to connect" issue on Windows Aug 1, 2019
@wchargin
Copy link
Contributor

wchargin commented Aug 1, 2019

Restarting work today (Th 1/8/19) I found that the "localhost refuses
to connect" message was back when I asked Tensorboard to graph the log
files created yesterday.

Yes; unfortunately, I suspected that this might be the case, because
Windows does not clean up the temporary directory. :-( More details in
#2483, if you’re curious.

1st attempt timed out again.
2nd attempt brought up the Tensorboard dashboard.

This is the expected behavior when TensorBoard takes more than 10
seconds to start up—which is fairly rare in my experience (takes about 1
second on Linux), but things may be slower on Windows, or with more
background tasks or antivirus software running, etc.

Thanks for your help. Glad it wasn't just me! I can progress again!

Glad to hear it! And you’re quite welcome.

@konstantin-frolov
Copy link

del /q %TMP%.tensorboard-info*

It's works for me with connection refused error. Thanks

@ghost
Copy link

ghost commented Jun 23, 2021

For a quick workaround, you can run the following commands in any
command prompt (cmd.exe):

taskkill /im tensorboard.exe /f
del /q %TMP%\.tensorboard-info\*

The above process worked for me thank you so much

@scotgopal
Copy link

scotgopal commented Mar 28, 2022

For a quick workaround, you can run the following commands in any
command prompt (cmd.exe):

> taskkill /im tensorboard.exe /f
> del /q %TMP%\.tensorboard-info\*

An equivalent Powershell command would look like this:-

> taskkill /im tensorboard.exe /f
> del $env:TEMP\.tensorboard-info\*

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants