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

Conversation

nolanmar511
Copy link
Contributor

@nolanmar511 nolanmar511 commented Feb 7, 2018

This fixes #307.
When the port is not specified with the -http option, a random port is selected using net.Listen().

if err != nil {
return "", 0, fmt.Errorf("could not generate random port: %v", err)
}
fmt.Printf("\n%v\n", port)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Leftover debug output? (certainly doesn't look like meaningful user-visible output, so assuming this is from debug)

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.

@codecov-io
Copy link

codecov-io commented Feb 7, 2018

Codecov Report

Merging #316 into master will increase coverage by 0.01%.
The diff coverage is 60.86%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #316      +/-   ##
==========================================
+ Coverage   65.86%   65.87%   +0.01%     
==========================================
  Files          36       36              
  Lines        7402     7417      +15     
==========================================
+ Hits         4875     4886      +11     
- Misses       2132     2134       +2     
- Partials      395      397       +2
Impacted Files Coverage Δ
internal/driver/webui.go 59.4% <60.86%> (+0.95%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a8f279b...cb24360. Read the comment docs.

@nolanmar511 nolanmar511 merged commit 7a927a9 into google:master Feb 7, 2018
lannadorai pushed a commit to lannadorai/pprof that referenced this pull request Feb 13, 2018
* use random port when no port specified

* remove debugging print statement
gmarin13 pushed a commit to gmarin13/pprof that referenced this pull request Dec 17, 2020
* use random port when no port specified

* remove debugging print statement
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Random port doesn't work
4 participants