diff --git a/tensorboard/program.py b/tensorboard/program.py index 762adc32b4d..02cd660980b 100644 --- a/tensorboard/program.py +++ b/tensorboard/program.py @@ -205,7 +205,7 @@ def configure(self, argv=("",), **kwargs): # any positional arguments to `serve`. serve_parser = serve_subparser - for (name, subcommand) in self.subcommands.items(): + for name, subcommand in self.subcommands.items(): subparser = subparsers.add_parser( name, help=subcommand.help(), @@ -698,7 +698,17 @@ def __init__(self, wsgi_app, flags): self._url = None # Will be set by get_url() below self._fix_werkzeug_logging() + + def is_port_in_use(port): + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + return s.connect_ex(("localhost", port)) == 0 + try: + if is_port_in_use(port): + raise TensorBoardPortInUseError( + "TensorBoard could not bind to port %d, it was already in use" + % port + ) super().__init__(host, port, wsgi_app, _WSGIRequestHandler) except socket.error as e: if hasattr(errno, "EACCES") and e.errno == errno.EACCES: