From f437061d6c02e31d788edaa3e3e9a239d14c4279 Mon Sep 17 00:00:00 2001 From: Armel Soro Date: Fri, 25 Aug 2023 14:49:49 +0200 Subject: [PATCH] Make sure to API Server listens on 127.0.0.1 This ensures the same local address is used for listening and checking if a given port is free. Otherwise, `net.listen("tcp", ":$port")` would listen on `0.0.0.0:$port`, and, on some operating systems like Windows 11, `127.0.0.1:$port` is surprisingly considered as free (see output below). This, as a consequence, made it impossible to run multiple Dev Sessions on Windows. ``` PS C:\Users\asoro> netstat -aon | grep 2000 TCP 0.0.0.0:20000 0.0.0.0:0 LISTENING 11044 TCP 127.0.0.1:20001 0.0.0.0:0 LISTENING 11044 TCP [::]:20000 [::]:0 LISTENING 11044 TCP [::1]:20000 [::1]:53656 ESTABLISHED 11044 TCP [::1]:53656 [::1]:20000 ESTABLISHED 9984 ``` Using the same local address for listening and checking if the port is free would be safer. If we decide to support passing a custom address, we would use that address instead. --- pkg/apiserver-impl/starterserver.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pkg/apiserver-impl/starterserver.go b/pkg/apiserver-impl/starterserver.go index f94c09df43..7b7b396681 100644 --- a/pkg/apiserver-impl/starterserver.go +++ b/pkg/apiserver-impl/starterserver.go @@ -96,15 +96,16 @@ func StartServer( router.PathPrefix("/").Handler(staticServer) } + addr := "127.0.0.1" if port == 0 && !randomPort { - port, err = util.NextFreePort(20000, 30001, nil, "") + port, err = util.NextFreePort(20000, 30001, nil, addr) if err != nil { klog.V(0).Infof("Unable to start the API server; encountered error: %v", err) cancelFunc() } } - listener, err := net.Listen("tcp", fmt.Sprintf(":%d", port)) + listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", addr, port)) if err != nil { return ApiServer{}, fmt.Errorf("unable to start API Server listener on port %d: %w", port, err) }