-
Notifications
You must be signed in to change notification settings - Fork 612
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
Conversation
internal/driver/webui.go
Outdated
if err != nil { | ||
return "", 0, fmt.Errorf("could not generate random port: %v", err) | ||
} | ||
fmt.Printf("\n%v\n", port) |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 Report
@@ 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
Continue to review full report at Codecov.
|
* use random port when no port specified * remove debugging print statement
* use random port when no port specified * remove debugging print statement
This fixes #307.
When the port is not specified with the
-http
option, a random port is selected usingnet.Listen()
.