Stop using SocketUtils.findAvailableTcpPort() wherever possible

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
sbrannencommented, Feb 15, 2022

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

0reactions
adrogoncommented, Nov 24, 2022

For those arriving here today, this is what you’re looking for: https://github.com/spring-projects/spring-framework/pull/29132

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - How to find an available port? - Stack Overflow
Just for convenience purposes, I'll add an inline example using the Springframwework SocketUtils.findAvailableTcpPort() method: int randomAvailablePort = ...
Read more >
Finding a Free Port in Java | Baeldung
In this quick tutorial, we'll look at how to check a specific port status and how to use an automatically allocated one.
Read more >
91. Spring Cloud Contract Stub Runner
One of the issues that you might encounter while using Spring Cloud ... findAvailableTcpPort() log.info("Will set HTTPs port [" + httpsPort + "]...
Read more >
Micronaut Testing Best Practices - Object Computing, Inc.
Avoid artificial separation of tests because of slow start and memory consumption. - Micronaut is a test agnostic framework. - Spock, JUnit 4,...
Read more >
Index (spring-core 5.2.5.RELEASE API) - javadoc.io
Add a Comparator to the end of the chain using the provided sort order. ... findAvailableTcpPort() - Static method in class org.springframework.util.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found