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

Stop using SocketUtils.findAvailableTcpPort() wherever possible #9382

Closed
wilkinsona opened this issue Jun 1, 2017 · 7 comments
Closed

Stop using SocketUtils.findAvailableTcpPort() wherever possible #9382

wilkinsona opened this issue Jun 1, 2017 · 7 comments
Assignees
Labels
type: task A general task
Milestone

Comments

@wilkinsona
Copy link
Member

SocketUtils.findAvailableTcpPort() is rather unreliable leading to intermittent test failures because a port is already in use. I'm not sure if this happens because the socket that findAvailableTcpPort opens to get a free port doesn't actually release the port, or if something else just happens to grab the same port. The end result is the same either way.

In the interests of making the build more stable, I'd like to stop using SocketUtils.findAvailableTcpPort() wherever possible. In its place, we should pass in a port of 0 to whatever's going to open a socket, and then use the port that the OS allocates. This will require some changes to some main code, TunnelClient for example, but I think it's worth it to improve the stability of the build.

@wilkinsona wilkinsona added for: team-attention An issue we'd like other members of the team to review priority: normal type: task A general task labels Jun 1, 2017
@wilkinsona wilkinsona removed the for: team-attention An issue we'd like other members of the team to review label Jun 8, 2017
@wilkinsona wilkinsona self-assigned this Jun 8, 2017
@wilkinsona wilkinsona added this to the 2.0.0.M2 milestone Jun 9, 2017
@wilkinsona wilkinsona marked this as a duplicate of #9230 Jul 29, 2017
@nitinsh99
Copy link

nitinsh99 commented Feb 14, 2022

@wilkinsona we are running into this while using findAvailableTcpPort.. Do you have more insight into why this is unreliable and any code example of providing 0 port? Looking at the SocketUtil implementation, this method tries to open up a server on random port and assumes its available if it doesn't throw IO exception because of conflict; which sounds a pretty good test. So I am curious why it's presenting a flaky behaviour? Also, should this be classified as a bug?

@wilkinsona
Copy link
Member Author

Do you have more insight into why this is unreliable

The opening comment of this issue describes our best guess at why it is unreliable.

Also, should this be classified as a bug?

Not from Spring Boot's perspective asSocketUtils is part of Spring Framework. You may want to open a Spring Framework issue describing the problem that you're seeing.

@sbrannen
Copy link
Member

Please note that SocketUtils has been deprecated in Spring Framework 5.3.16:

@ahoehma
Copy link

ahoehma commented Mar 14, 2022

What can I do if I'm using org.springframework.util.SocketUtils.findAvailableTcpPort()

@bclozel
Copy link
Member

bclozel commented Mar 15, 2022

@ahoehma this is explained in spring-projects/spring-framework#28052

@sbrannen
Copy link
Member

If you have been using SocketUtils solely in testing scenarios, please note that TestSocketUtils will be introduced in Spring Framework 5.3.18. See spring-projects/spring-framework#28210 for details.

@adrogon
Copy link

adrogon commented Nov 24, 2022

For those arriving here today, this is what you're looking for: spring-projects/spring-framework#29132

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: task A general task
Projects
None yet
Development

No branches or pull requests

6 participants