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

Destroy service beans after web server is down gracefully #19951

Closed
LychakGalina opened this issue Jan 27, 2020 · 6 comments
Closed

Destroy service beans after web server is down gracefully #19951

LychakGalina opened this issue Jan 27, 2020 · 6 comments
Labels
status: duplicate A duplicate of another issue

Comments

@LychakGalina
Copy link

We use spring boot with embedded netty server that has graceful shutdown (that seems has been fixed in spring-projects/spring-framework#23631) and do some logic using another beans autowired into controller. When sigterm is received server waits for all in-progress requests are finished, and we have case when service is destroyed before request is finished and requests are failed due to closed resources in destroy method. and as I can see reactorServerResourceFactory is always destroyed last. So would be great to have ability to set up ordering between reactorServerResourceFactory and custom beans. Or probably do we have a way already to customize this bean to have @DependsOn annotation with dependency on our beans?

SpringBoot version: 2.2.2.RELEASE

@wilkinsona
Copy link
Member

Duplicates #4657.

@wilkinsona wilkinsona added status: duplicate A duplicate of another issue and removed status: waiting-for-triage An issue we've not yet triaged labels Jan 27, 2020
@LychakGalina
Copy link
Author

@wilkinsona there are too many issues raised in #4657, and more about how to prevent accepting new requests/implement custom graceful shutdown with serverCustomizer interface for tomcat/jetty/completion all active requests, but currently waiting for active requests when sigterm is received works for Netty, doesn't it?
and do we really have no way to customize reactorServerResourceFactory to have something like @dependsOn?

@wilkinsona
Copy link
Member

more about how to prevent accepting new requests/implement custom graceful shutdown with serverCustomizer interface for tomcat/jetty/completion all active requests

The customizer approach is a way of someone implementing their own solution until Spring Boot supports it out-of-the box. #4657 is tracking the addition of that support.

but currently waiting for active requests when sigterm is received works for Netty, doesn't it?

I don't believe so, not out of the box anyway.

do we really have no way to customize reactorServerResourceFactory to have something like @dependsOn?

You can use a BeanFactoryPostProcessor to set up custom depends on configuration. Once #4657 is implemented it, hopefully, will not be necessary as the web server should stop accepting new requests, wait for any inflight requests to complete, and then allow closing of the context to proceed.

@LychakGalina
Copy link
Author

LychakGalina commented Jan 27, 2020

I don't believe so, not out of the box anyway.

it's strange, have tested this in a simple way - add Thread#sleep to endpoint, call endpoint, and send sigterm to application, it doesn't destroy the webServer bean until request is finished. and it didn't work before this issue is fixed
spring-projects/spring-framework#23631

@wilkinsona
Copy link
Member

wilkinsona commented Jan 27, 2020

There's more to it than that. As described in #4657 we currently close the application context and then, at the end of that, close the web server. This means that beans that an inflight request relies upon may be destroyed while the request is still being handled. We need to do three things:

  1. Stop any new requests from being accepted
  2. Wait for any existing requests to complete
  3. Close the application context

We're now going over things that have been discussed at length and are being tracked by #4657. Please subscribe to that issue for further updates.

@LychakGalina
Copy link
Author

LychakGalina commented Jan 28, 2020

We need to do three things:

it's clear, thanks
but my point was that one of these things (wait for any existing requests to complete) already works for us when we use EmbeddedNetty server, and it didn't until we upgraded springBoot/springFramework from 2.0.5/5.0.9 -> 2.2.2/5.2.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

3 participants