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

TomcatWebServer stop doesn't close sockets for additional connectors #38564

Closed
soer84 opened this issue Nov 27, 2023 · 9 comments
Closed

TomcatWebServer stop doesn't close sockets for additional connectors #38564

soer84 opened this issue Nov 27, 2023 · 9 comments
Assignees
Labels
type: regression A regression from a previous release
Milestone

Comments

@soer84
Copy link

soer84 commented Nov 27, 2023

Greetings!

As stated in this seemingly unrelated ticket #38554 we are currently facing an issue when running multiple Spring boot tests one after another.

i took the liberty and created a reproducer for this case. The tests in the attached zip file work with spring boot 3.1.5 but fail on 3.2.0.

In my example I add a new http connector with a fixed port. When running each test on its own they work. But if they're executed in a row i.e. by a mvn verify the second tests fails on initialization stating that the configured connector failed to start.

Maven doesn't print the reason too clearly unfortunately but a little closer investigation inside a IDE reveals the java.net.BindException: Address already in use: bind exception.

SpringTestReproducer.zip

In this example also the "@DirtiesContext" doesn't help. It seems as if the Spring / ApplicationContext is cleaned up and the new server instance is going to boot but the old instance of the server hasn't shut down yet or isn't shutting down at all.

Thanks in advance!

@soer84 soer84 changed the title Address already in use: bind on SpringBootTest - tests when using a fixed port number Address already in use: bind on SpringBootTest - tests when using a fixed port number on Spring boot 3.2.0 Nov 27, 2023
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Nov 27, 2023
@mhalbritter
Copy link
Contributor

mhalbritter commented Nov 27, 2023

For some reason, after this.webServer.stop(); has been called in org.springframework.boot.web.servlet.context.WebServerStartStopLifecycle#stop, the process is still listening on port 8010:

ss -l -p -n | grep "pid=83209,"
tcp   LISTEN 0      100                                                      *:8010                   *:*    users:(("java",pid=83209,fd=58))

Interestingly, the connector for port 8080 has closed the listening socket.

In org.apache.tomcat.util.net.AbstractEndpoint#stop, for port 8010, the bindState is BOUND_ON_INIT, thus is skips the unbind in the stop method. This method is the one which closes the server socket. For port 8080, the state is BOUND_ON_START, thus closing the socket on stop.

@mhalbritter
Copy link
Contributor

In org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory#customizeConnector, we have this:

// Don't bind to the socket prematurely if ApplicationContext is slow to start
connector.setProperty("bindOnInit", "false");

When adding this property to the 8010 connector, all tests pass.

@wilkinsona
Copy link
Member

That points towards dbb2428 as being the cause. A BOUND_ON_INIT endpoint isn't unbound until destroy is called.

@mhalbritter mhalbritter changed the title Address already in use: bind on SpringBootTest - tests when using a fixed port number on Spring boot 3.2.0 WebServer stop doesn't close all sockets Nov 28, 2023
@mhalbritter mhalbritter added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Nov 28, 2023
@mhalbritter mhalbritter added this to the 3.2.x milestone Nov 28, 2023
@wilkinsona wilkinsona changed the title WebServer stop doesn't close all sockets TomcatWebServer stop doesn't close sockets for additional connectors Nov 29, 2023
@wilkinsona wilkinsona self-assigned this Nov 29, 2023
@wilkinsona wilkinsona modified the milestones: 3.2.x, 3.2.1 Nov 29, 2023
@wilkinsona wilkinsona added type: regression A regression from a previous release and removed type: bug A general bug labels Nov 29, 2023
@mr-hamburg
Copy link

Hi, without wanting to put pressure, this is absolutely not my intention, but just because I want to understand: so we wait until release 3.2.1 until this is fixed, is that correct? Best regards from Hamburg!

@mhalbritter
Copy link
Contributor

mhalbritter commented Dec 5, 2023

Yes. It will be released on 21th of December, see the calendar or the milestone.

@mr-hamburg
Copy link

Thanks for the link, I'll save it for future reference. Best wishes and have a nice day!

@jamfor352
Copy link

I'm still seeing this in 3.2.1 & 3.2.2.

@wilkinsona
Copy link
Member

wilkinsona commented Jan 30, 2024

@jamfor352 Without anything more to go on, we can only assume that you have a different problem as we believe that this one has been resolved. Please open a new issue that describes your situation in more detail and that provides a minimal example that reproduces it.

@soer84
Copy link
Author

soer84 commented Jan 31, 2024

Sorry for the lack of feedback - but just to let you know - we have been upgrading our repository to spring boot 3.2.1 and the tests were executing as expected. So this issue seems fixed for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: regression A regression from a previous release
Projects
None yet
Development

No branches or pull requests

6 participants