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

get-free-port: prevent duplicate ports on Linux #564

Merged
merged 8 commits into from
May 17, 2024

Conversation

mjameswh
Copy link
Contributor

@mjameswh mjameswh commented May 15, 2024

What changed

  • When assigning ephemeral ports for use by the Temporal Server, on all platforms except MacOS and Windows, immediately open a connection to that port, and force the connection into TIME_WAIT state. This avoids various race conditions that could happen due to the port being reallocated between the moment the port is obtained and the moment the server actually binds to that port. Resolves [Feature Request] Fix free-port assignment to actually try to connect #550.

  • Add checks to assert that ports provided by the user are indeed free; this would previously cause a SIGSEGV. Resolves SIGSEGV when using --http-port option for dev server and port is already in use #543.

    • Note that these checks are subject to race conditions (that's essentially a check-before-use sequence), though such race occurrences are very unlikely for ports numbers that have been specifically requested by the user.
  • Added tests to confirm that it is possible to start multiple dev server concurrently.

@mjameswh mjameswh changed the title [WIP] get-free-port: prevent duplicate ports on Linux get-free-port: prevent duplicate ports on Linux May 16, 2024
@mjameswh mjameswh marked this pull request as ready for review May 16, 2024 22:21
@mjameswh mjameswh requested a review from cretz May 16, 2024 22:22
Copy link
Member

@cretz cretz left a comment

Choose a reason for hiding this comment

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

LGTM. Only needed comment is fixing Godoc format.

Copy link
Member

Choose a reason for hiding this comment

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

Do we need a new package instead of just putting this file back where the file that was deleted was (and add the test file next to it)? I don't mind that much though, but if you do move, calling devserver.CheckPortFree from the command is ok. Your choice.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, I can move it back. I originally moved it to a different package when I introduced freeport_windows.go vs freeport_darwin.go vs …, but since I reduced all of that to a single file, there no longer a justification for this.

"runtime"
)

/**
Copy link
Member

Choose a reason for hiding this comment

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

This is not the common format of Godoc, it's just // -prefixed lines

@mjameswh mjameswh requested a review from cretz May 17, 2024 12:19
// in this regard; on that platform, `SO_REUSEADDR` has a different meaning and
// should not be set (setting it may have unpredictable consequences).
func GetFreePort(host string) (int, error) {
l, err := net.Listen("tcp", "127.0.0.1:0")
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
l, err := net.Listen("tcp", "127.0.0.1:0")
l, err := net.Listen("tcp", host+":0")

I wonder if this makes more sense. Not that it matters much.

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 does. Good catch.

Co-authored-by: Chad Retz <chad.retz@gmail.com>
@mjameswh mjameswh merged commit 350cb2f into temporalio:main May 17, 2024
7 checks passed
@mjameswh mjameswh deleted the free-ports branch May 17, 2024 13:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants