Make sure to API Server listens on 127.0.0.1 (#7041)

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.
This commit is contained in:
Armel Soro
2023-08-28 15:49:56 +02:00
committed by GitHub
parent 319adfa724
commit a9492307d7

View File

@@ -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)
}