Skip to content

Commit

Permalink
Make sure to API Server listens on 127.0.0.1 (#7041)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rm3l authored Aug 28, 2023
1 parent 319adfa commit a949230
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions pkg/apiserver-impl/starterserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit a949230

Please sign in to comment.