spring-boot icon indicating copy to clipboard operation
spring-boot copied to clipboard

ApplicationContext close() is blocked by embedded Tomcat stop()

Open lubos-bistak opened this issue 10 years ago • 4 comments

I am trying to close Spring Boot application by calling "applicationContext.close()" method, the whole Java process is blocked and can not terminated event by calling "System.exit();".

I already tried to use "SpringApplication.exit()", but same behaviour.

Stack trace of BLOCKED thread as displayed in "Java Mission Control":
localhost-startStop-2 [55] (BLOCKED)
   org.springframework.context.support.AbstractApplicationContext.close line: 956
   org.springframework.web.servlet.FrameworkServlet.destroy line: 829
   org.apache.catalina.core.StandardWrapper.unload line: 1359
   org.apache.catalina.core.StandardWrapper.stopInternal line: 1690
   org.apache.catalina.util.LifecycleBase.stop line: 226
   org.apache.catalina.core.StandardContext.stopInternal line: 5399
   org.apache.catalina.util.LifecycleBase.stop line: 226
   org.apache.catalina.core.ContainerBase$StopChild.call line: 1419
   org.apache.catalina.core.ContainerBase$StopChild.call line: 1408
   java.util.concurrent.FutureTask.run line: not available
   java.util.concurrent.ThreadPoolExecutor.runWorker line: not available
   java.util.concurrent.ThreadPoolExecutor$Worker.run line: not available
   java.lang.Thread.run line: not available

I think the problem might be: "ApplicationContext.close()-> ... ->FrameworkServlet.destroy() -> ... -> ApplicationContext.close()" and "AbstractApplicationContext.close()" contains synchronized block.

After replacing "Tomcat Embeded" with "Undertow", it works fine. Because of some issues we can't use "Undertow".

I am using following versions:

[INFO] +- org.springframework:spring-webmvc:jar:4.3.1.RELEASE:compile
[INFO] |  +- org.springframework:spring-aop:jar:4.3.1.RELEASE:compile
[INFO] |  +- org.springframework:spring-beans:jar:4.3.1.RELEASE:compile
[INFO] |  +- org.springframework:spring-context:jar:4.3.1.RELEASE:compile
[INFO] |  +- org.springframework:spring-core:jar:4.3.1.RELEASE:compile
[INFO] |  |  \- commons-logging:commons-logging:jar:1.2:compile
[INFO] |  +- org.springframework:spring-expression:jar:4.3.3.RELEASE:compile
[INFO] |  \- org.springframework:spring-web:jar:4.3.1.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter:jar:1.4.1.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot:jar:1.4.1.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-autoconfigure:jar:1.4.1.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-logging:jar:1.4.1.RELEASE:compile
[INFO] |  \- org.yaml:snakeyaml:jar:1.17:compile
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:1.4.1.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-tomcat:jar:1.4.1.RELEASE:compile
[INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-core:jar:8.5.5:compile
[INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-el:jar:8.5.5:compile
[INFO] |  |  \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:8.5.5:compile
[INFO] |  \- org.hibernate:hibernate-validator:jar:5.1.2.Final:compile
[INFO] |     \- javax.validation:validation-api:jar:1.1.0.Final:compile
[INFO] +- org.springframework.boot:spring-boot-starter-data-jpa:jar:1.4.1.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-aop:jar:1.4.1.RELEASE:compile
[INFO] |  |  \- org.aspectj:aspectjweaver:jar:1.8.9:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-jdbc:jar:1.4.1.RELEASE:compile
[INFO] |  |  \- org.apache.tomcat:tomcat-jdbc:jar:8.5.5:compile
[INFO] |  |     \- org.apache.tomcat:tomcat-juli:jar:8.5.5:compile
[INFO] |  +- org.hibernate:hibernate-core:jar:4.3.7.Final:compile
[INFO] |  |  +- org.jboss.logging:jboss-logging:jar:3.3.0.Final:compile
[INFO] |  |  +- org.jboss.logging:jboss-logging-annotations:jar:1.2.0.Beta1:compile
[INFO] |  |  +- org.jboss.spec.javax.transaction:jboss-transaction-api_1.2_spec:jar:1.0.0.Final:compile
[INFO] |  |  +- dom4j:dom4j:jar:1.6.1:compile
[INFO] |  |  |  \- xml-apis:xml-apis:jar:1.4.01:compile
[INFO] |  |  +- org.hibernate.common:hibernate-commons-annotations:jar:4.0.5.Final:compile
[INFO] |  |  +- org.hibernate.javax.persistence:hibernate-jpa-2.1-api:jar:1.0.0.Final:compile
[INFO] |  |  +- org.javassist:javassist:jar:3.18.2-GA:compile
[INFO] |  |  +- antlr:antlr:jar:2.7.7:compile
[INFO] |  |  \- org.jboss:jandex:jar:1.1.0.Final:compile
[INFO] |  +- org.hibernate:hibernate-entitymanager:jar:4.3.7.Final:compile
[INFO] |  +- javax.transaction:javax.transaction-api:jar:1.2:compile
[INFO] |  +- org.springframework.data:spring-data-jpa:jar:1.10.2.RELEASE:compile
[INFO] |  |  +- org.springframework.data:spring-data-commons:jar:1.12.3.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-tx:jar:4.3.1.RELEASE:compile
[INFO] |  \- org.springframework:spring-aspects:jar:4.3.1.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-actuator:jar:1.4.1.RELEASE:compile
[INFO] |  \- org.springframework.boot:spring-boot-actuator:jar:1.4.1.RELEASE:compile
[INFO] +- org.modelmapper:modelmapper:jar:0.7.5:compile
[INFO] +- com.fasterxml.jackson.core:jackson-core:jar:2.7.1:compile
[INFO] +- com.fasterxml.jackson.core:jackson-databind:jar:2.7.1:compile
[INFO] +- com.fasterxml.jackson.core:jackson-annotations:jar:2.7.1:compile
[INFO] +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.7.1:compile
[INFO] +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.7.1:compile
[INFO] +- com.fasterxml.jackson.datatype:jackson-datatype-guava:jar:2.7.1:compile
[INFO] +- com.fasterxml.jackson.module:jackson-module-afterburner:jar:2.7.1:compile
[INFO] +- com.fasterxml.jackson.module:jackson-module-mrbean:jar:2.7.1:compile
[INFO] |  \- org.ow2.asm:asm:jar:5.0.4:compile
[INFO] +- com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:jar:2.7.1:compile

Java: 1.8.0_101 OS: Win XP 32bit

lubos-bistak avatar Sep 28 '16 16:09 lubos-bistak

I don't think it'll fix the problem, but it looks like you're overriding the Spring Framework version and downgrading it. Can you retry with 4.3.3.RELEASE please?

If the problem remains, can you please provide a sample that reproduces the problem?

wilkinsona avatar Sep 28 '16 16:09 wilkinsona

I changed Spring Framework to 4.3.3.RELEASE, but the problem is same.

I'll try to make small app / example to reproduce the problem.

But has anybody already tried to call ApplicationContext.close() in Sprint Boot with Tomcat embedded?

lubos-bistak avatar Sep 28 '16 16:09 lubos-bistak

I think i found a solution / workaround for my problem:

final DispatcherServlet dispatcherServlet = new DispatcherServlet(applicationContext); vs. final DispatcherServlet dispatcherServlet = new DispatcherServlet(); dispatcherServlet.setApplicationContext(applicationContext);

Field "webApplicationContextInjected" is checked in "FrameworkServlet.destroy()" method. And "webApplicationContextInjected" is only set by calling explicitly "DispatcherServlet.setApplicationContext()".

I would suggest to change code of constructor DispatcherServlet(applicationContext) to call setApplicationContext() instead of assigning this field directly.

lubos-bistak avatar Sep 28 '16 17:09 lubos-bistak

Now that I know you're creating the DispatcherServlet yourself this is a duplicate of #1314.

I would suggest to change code of constructor DispatcherServlet(applicationContext) to call setApplicationContext() instead of assigning this field directly.

It's intentionally the way it is at the moment so that the behaviour is different when the application context is passed into the constructor versus being injected via setApplicationContext. Your proposed change would remove that distinction.

wilkinsona avatar Sep 28 '16 19:09 wilkinsona