Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use random port when no port specified with -http #316

Merged
merged 2 commits into from
Feb 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 30 additions & 11 deletions internal/driver/webui.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,10 @@ type webArgs struct {
}

func serveWebInterface(hostport string, p *profile.Profile, o *plugin.Options, wantBrowser bool) error {
host, portStr, err := net.SplitHostPort(hostport)
if err != nil {
return fmt.Errorf("could not split http address: %v", err)
}
port, err := strconv.Atoi(portStr)
host, port, err := getHostAndPort(hostport)
if err != nil {
return fmt.Errorf("invalid port number: %v", err)
}
if host == "" {
host = "localhost"
return err
}

interactiveMode = true
ui := makeWebInterface(p, o)
for n, c := range pprofCommands {
Expand All @@ -112,7 +104,7 @@ func serveWebInterface(hostport string, p *profile.Profile, o *plugin.Options, w
server = defaultWebServer
}
args := &plugin.HTTPServerArgs{
Hostport: net.JoinHostPort(host, portStr),
Hostport: net.JoinHostPort(host, strconv.Itoa(port)),
Host: host,
Port: port,
Handlers: map[string]http.Handler{
Expand All @@ -131,6 +123,33 @@ func serveWebInterface(hostport string, p *profile.Profile, o *plugin.Options, w
return server(args)
}

func getHostAndPort(hostport string) (string, int, error) {
host, portStr, err := net.SplitHostPort(hostport)
if err != nil {
return "", 0, fmt.Errorf("could not split http address: %v", err)
}
if host == "" {
host = "localhost"
}
var port int
if portStr == "" {
ln, err := net.Listen("tcp", net.JoinHostPort(host, "0"))
if err != nil {
return "", 0, fmt.Errorf("could not generate random port: %v", err)
}
port = ln.Addr().(*net.TCPAddr).Port
err = ln.Close()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is racy in the sense that another program may grab the port after the socket is closed here and opened again by the HTTP server. Did you consider this possibility? Is this too unlikely to worry about?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's definitely possible -- I believe it will occur rarely and I could not find another way to generate an unused port.

To reduce this possibility, I could shift the functionality of getHostAndPort() into serveWebInterface(), and close the connection later. But that would make it harder to test random port selection and still not completely eliminate the race.

if err != nil {
return "", 0, fmt.Errorf("could not generate random port: %v", err)
}
} else {
port, err = strconv.Atoi(portStr)
if err != nil {
return "", 0, fmt.Errorf("invalid port number: %v", err)
}
}
return host, port, nil
}
func defaultWebServer(args *plugin.HTTPServerArgs) error {
ln, err := net.Listen("tcp", args.Hostport)
if err != nil {
Expand Down
35 changes: 35 additions & 0 deletions internal/driver/webui_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,41 @@ func makeFakeProfile() *profile.Profile {
}
}

func TestGetHostAndPort(t *testing.T) {
if runtime.GOOS == "nacl" {
t.Skip("test assumes tcp available")
}

type testCase struct {
hostport string
wantHost string
wantPort int
wantRandomPort bool
}

testCases := []testCase{
{":", "localhost", 0, true},
{":4681", "localhost", 4681, false},
{"localhost:4681", "localhost", 4681, false},
}
for _, tc := range testCases {
host, port, err := getHostAndPort(tc.hostport)
if err != nil {
t.Errorf("could not get host and port for %q: %v", tc.hostport, err)
}
if got, want := host, tc.wantHost; got != want {
t.Errorf("for %s, got host %s, want %s", tc.hostport, got, want)
continue
}
if !tc.wantRandomPort {
if got, want := port, tc.wantPort; got != want {
t.Errorf("for %s, got port %d, want %d", tc.hostport, got, want)
continue
}
}
}
}

func TestIsLocalHost(t *testing.T) {
for _, s := range []string{"localhost:10000", "[::1]:10000", "127.0.0.1:10000"} {
host, _, err := net.SplitHostPort(s)
Expand Down