Skip to content

Commit 26f5e33

Browse files
authored
Bug Fix: Update the port scanning logic to properly skip ports in use (#6560)
## Motivation for features / changes #6552 ## Technical description of changes The port scanning logic originally added in #1851 does not seem to work anymore. WerkzeugServer no longer raises an Error when attempting to bind to a port which is in used and instead handles the issue silently. The current Werkzeug version is 2.3.7 but I had 2.3.4 installed when I first verified this. I tried installing 2.2.0 and the issues was not present so I assume it was introduced in 2.3.0. https://pypi.org/project/Werkzeug ## Detailed steps to verify changes work correctly (as executed by you) 1) Start TensorBoard, note the port it is bound to (it should be 6006) 2) In a separate terminal start TensorBoard again. 3) TensorBoard should successfully bind to a different port (this time 6007)
1 parent bc3d387 commit 26f5e33

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

tensorboard/program.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ def configure(self, argv=("",), **kwargs):
205205
# any positional arguments to `serve`.
206206
serve_parser = serve_subparser
207207

208-
for (name, subcommand) in self.subcommands.items():
208+
for name, subcommand in self.subcommands.items():
209209
subparser = subparsers.add_parser(
210210
name,
211211
help=subcommand.help(),
@@ -698,7 +698,17 @@ def __init__(self, wsgi_app, flags):
698698
self._url = None # Will be set by get_url() below
699699

700700
self._fix_werkzeug_logging()
701+
702+
def is_port_in_use(port):
703+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
704+
return s.connect_ex(("localhost", port)) == 0
705+
701706
try:
707+
if is_port_in_use(port):
708+
raise TensorBoardPortInUseError(
709+
"TensorBoard could not bind to port %d, it was already in use"
710+
% port
711+
)
702712
super().__init__(host, port, wsgi_app, _WSGIRequestHandler)
703713
except socket.error as e:
704714
if hasattr(errno, "EACCES") and e.errno == errno.EACCES:

0 commit comments

Comments
 (0)