When spring-boot application context closed sentinel still has some not closed non-daemons thread

j2qf4p5b  于 23天前  发布在  Spring
关注(0)|答案(3)|浏览(31)

Issue Description

Type: bug report or feature request

Describe what happened (or what feature you want)

when I use spring-cloud-starter-alibaba-sentine:2.2.9.RELEASE with sentinel-transport-netty-http in my spring-boot application , the jvm can not exit when I call actuator/shutdown endpoint such as exec curl -X POST http://ip:port/actuator/shutdown .

This is because of com.alibaba.csp.sentinel.transport.command.netty.HttpServer thread has not closed when the application context closed.

Describe what you expected to happen

Process finished with exit code 0

How to reproduce it (as minimally and precisely as possible)

  1. run you local sentinel dashboard on port 8080
  2. clone a simple demo such as git clone git@github.com:tobrainto/sentinel-demo.git
  3. Run it in Intelllij IDEA with args
-Dcsp.sentinel.dashboard.server=localhost:8080
-Dcsp.sentinel.api.port=8729
-Dproject.name=sentinel-demo

  1. exec curl -X GET http://localhost:8090/hello to trigger some init for sentinel
  2. exec curl -X POST http://localhost:8090/actuator/shutdown to shutdown the application
  3. you cannot find the info which mean the process finished as blow.
Disconnected from the target VM, address: '127.0.0.1:59120', transport: 'socket'

Process finished with exit code 0

and you can see some non-daemon thread still alive

Tell us your environment

Java11 、spring-cloud-starter-alibaba-sentine:2.2.9.RELEASEE 、spring-boot:2.6.6

Try fixed

I try to fixed it like this in spring-cloud-starter-alibaba-sentinel

@PreDestroy
private void destroy() {
    // Shutdown CommandCenter
    CommandCenter commandCenter = CommandCenterProvider.getCommandCenter();
    if (commandCenter != null) {
	    try {
		    commandCenter.stop();
	    }
	    catch (Exception e) {
		    log.warn("Sentinel CommandCenter shutdown error :", e);
	    }
    }
}

it work well except for a exception trace print

java.lang.InterruptedException
	at java.base/java.lang.Object.wait(Native Method)
	at java.base/java.lang.Object.wait(Object.java:328)
	at io.netty.util.concurrent.DefaultPromise.await(DefaultPromise.java:253)
	at io.netty.channel.DefaultChannelPromise.await(DefaultChannelPromise.java:131)
	at io.netty.channel.DefaultChannelPromise.await(DefaultChannelPromise.java:30)
	at io.netty.util.concurrent.DefaultPromise.sync(DefaultPromise.java:404)
	at io.netty.channel.DefaultChannelPromise.sync(DefaultChannelPromise.java:119)
	at io.netty.channel.DefaultChannelPromise.sync(DefaultChannelPromise.java:30)
	at com.alibaba.csp.sentinel.transport.command.netty.HttpServer.start(HttpServer.java:86)
	at com.alibaba.csp.sentinel.transport.command.NettyHttpCommandCenter$1.run(NettyHttpCommandCenter.java:50)
	at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
	at java.base/java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:264)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at java.base/java.lang.Thread.run(Thread.java:834)

so at the some I think channel.closeFuture().sync(); may need a try catch to ingore the unnecessary exception trace print

I also report it to spring-cloud-alibaba

spring-cloud-alibaba/issues/2920

I first report it to spring-cloud-alibaba ,they reply me can report it here

thanks!

相关问题