Issue 1:
[ ✅] I have searched the issues of this repository and believe that this is not a duplicate.

[✅ ] I have checked the FAQ of this repository and believe that this is not a duplicate.

Environment

Dubbo version: 2.6.x, tag2.7.1

Operating System version: mac

Java version: jdk1.8

Steps to reproduce this issue

run unit test in the source code, ReferenceAnnotationBeanPostProcessorTest#test

then an exception occur

@reference proxy object needs connect to Service, but at this time Service is not ready.

this issue is like this one: [https://github.com//issues/1654]

I run this test on branch 3.x-dev and it is ok. i compare the code between 3.x-dev and 2.6.x

there are some differences on this method

branch 2.6.x

private String buildReferencedBeanName(Reference reference, Class<?> injectedType) {

AnnotationBeanNameBuilder builder = AnnotationBeanNameBuilder.create(reference, injectedType);
builder.environment(getEnvironment());
return getEnvironment().resolvePlaceholders(builder.build());
}

branch 3.x-dev

private String buildReferencedBeanName(Reference reference, Class<?> injectedType) {
ServiceBeanNameBuilder builder = ServiceBeanNameBuilder.create(reference, injectedType, getEnvironment());
return getEnvironment().resolvePlaceholders(builder.build());
}

Pls. provide [GitHub address] to reproduce this issue.

Expected Result

What do you expected from the above steps？

unit test run ok.

Actual Result

What actually happens?

If there is an exception, please attach the exception trace:

java.lang.IllegalStateException: Failed to load ApplicationContext

at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testBean': Injection of @com.alibaba.dubbo.config.annotation.Reference dependencies is failed; nested exception is com.alibaba.dubbo.rpc.RpcException: Fail to create remoting client for service(dubbo://127.0.0.1:12345/com.alibaba.dubbo.config.spring.api.DemoService?application=dubbo-annotation-provider&codec=dubbo&default.timeout=2000&dubbo=2.0.2&heartbeat=60000&interface=com.alibaba.dubbo.config.spring.api.DemoService&methods=sayName,getBox&owner=world&pid=919&register.ip=10.99.60.252&revision=2.5.7&side=consumer&timestamp=1555913871891&version=2.5.7): client(url: dubbo://127.0.0.1:12345/com.alibaba.dubbo.config.spring.api.DemoService?application=dubbo-annotation-provider&codec=dubbo&default.timeout=2000&dubbo=2.0.2&heartbeat=60000&interface=com.alibaba.dubbo.config.spring.api.DemoService&methods=sayName,getBox&owner=world&pid=919&register.ip=10.99.60.252&revision=2.5.7&side=consumer&timestamp=1555913871891&version=2.5.7) failed to connect to server /127.0.0.1:12345, error message is:Connection refused: /10.99.60.252:12345
at com.alibaba.dubbo.config.spring.beans.factory.annotation.AnnotationInjectedBeanPostProcessor.postProcessPropertyValues(AnnotationInjectedBeanPostProcessor.java:132)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1268)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:128)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:106)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:249)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
... 24 more
Caused by: com.alibaba.dubbo.rpc.RpcException: Fail to create remoting client for service(dubbo://127.0.0.1:12345/com.alibaba.dubbo.config.spring.api.DemoService?application=dubbo-annotation-provider&codec=dubbo&default.timeout=2000&dubbo=2.0.2&heartbeat=60000&interface=com.alibaba.dubbo.config.spring.api.DemoService&methods=sayName,getBox&owner=world&pid=919&register.ip=10.99.60.252&revision=2.5.7&side=consumer&timestamp=1555913871891&version=2.5.7): client(url: dubbo://127.0.0.1:12345/com.alibaba.dubbo.config.spring.api.DemoService?application=dubbo-annotation-provider&codec=dubbo&default.timeout=2000&dubbo=2.0.2&heartbeat=60000&interface=com.alibaba.dubbo.config.spring.api.DemoService&methods=sayName,getBox&owner=world&pid=919&register.ip=10.99.60.252&revision=2.5.7&side=consumer&timestamp=1555913871891&version=2.5.7) failed to connect to server /127.0.0.1:12345, error message is:Connection refused: /10.99.60.252:12345
at com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol.initClient(DubboProtocol.java:420)
at com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol.getSharedClient(DubboProtocol.java:384)
at com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol.getClients(DubboProtocol.java:355)
at com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol.refer(DubboProtocol.java:337)
at com.alibaba.dubbo.rpc.protocol.ProtocolListenerWrapper.refer(ProtocolListenerWrapper.java:67)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper.refer(ProtocolFilterWrapper.java:108)
at com.alibaba.dubbo.rpc.Protocol$Adaptive.refer(Protocol$Adaptive.java)
at com.alibaba.dubbo.config.ReferenceConfig.createProxy(ReferenceConfig.java:396)
at com.alibaba.dubbo.config.ReferenceConfig.init(ReferenceConfig.java:335)
at com.alibaba.dubbo.config.ReferenceConfig.get(ReferenceConfig.java:164)
at com.alibaba.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor$ReferenceBeanInvocationHandler.init(ReferenceAnnotationBeanPostProcessor.java:174)
at com.alibaba.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor$ReferenceBeanInvocationHandler.access$100(ReferenceAnnotationBeanPostProcessor.java:147)
at com.alibaba.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor.buildInvocationHandler(ReferenceAnnotationBeanPostProcessor.java:141)
at com.alibaba.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor.buildProxy(ReferenceAnnotationBeanPostProcessor.java:123)
at com.alibaba.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor.doGetInjectedBean(ReferenceAnnotationBeanPostProcessor.java:117)
at com.alibaba.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor.doGetInjectedBean(ReferenceAnnotationBeanPostProcessor.java:50)
at com.alibaba.dubbo.config.spring.beans.factory.annotation.AnnotationInjectedBeanPostProcessor.getInjectedObject(AnnotationInjectedBeanPostProcessor.java:340)
at com.alibaba.dubbo.config.spring.beans.factory.annotation.AnnotationInjectedBeanPostProcessor$AnnotatedFieldElement.inject(AnnotationInjectedBeanPostProcessor.java:520)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at com.alibaba.dubbo.config.spring.beans.factory.annotation.AnnotationInjectedBeanPostProcessor.postProcessPropertyValues(AnnotationInjectedBeanPostProcessor.java:128)
... 40 more
Caused by: com.alibaba.dubbo.remoting.RemotingException: client(url: dubbo://127.0.0.1:12345/com.alibaba.dubbo.config.spring.api.DemoService?application=dubbo-annotation-provider&codec=dubbo&default.timeout=2000&dubbo=2.0.2&heartbeat=60000&interface=com.alibaba.dubbo.config.spring.api.DemoService&methods=sayName,getBox&owner=world&pid=919&register.ip=10.99.60.252&revision=2.5.7&side=consumer&timestamp=1555913871891&version=2.5.7) failed to connect to server /127.0.0.1:12345, error message is:Connection refused: /10.99.60.252:12345
at com.alibaba.dubbo.remoting.transport.netty4.NettyClient.doConnect(NettyClient.java:127)
at com.alibaba.dubbo.remoting.transport.AbstractClient.connect(AbstractClient.java:275)
at com.alibaba.dubbo.remoting.transport.AbstractClient.<init>(AbstractClient.java:89)
at com.alibaba.dubbo.remoting.transport.netty4.NettyClient.<init>(NettyClient.java:55)
at com.alibaba.dubbo.remoting.transport.netty4.NettyTransporter.connect(NettyTransporter.java:37)
at com.alibaba.dubbo.remoting.Transporter$Adaptive.connect(Transporter$Adaptive.java)
at com.alibaba.dubbo.remoting.Transporters.connect(Transporters.java:75)
at com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchanger.connect(HeaderExchanger.java:39)
at com.alibaba.dubbo.remoting.exchange.Exchangers.connect(Exchangers.java:109)
at com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol.initClient(DubboProtocol.java:417)
... 59 more
Caused by: io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: /10.99.60.252:12345
at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717)
at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:325)
at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:340)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:635)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:582)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:499)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:461)
at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:884)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.ConnectException: Connection refused
... 11 more

Issue 2:
[ * ] I have searched the issues of this repository and believe that this is not a duplicate.

[ * ] I have checked the FAQ of this repository and believe that this is not a duplicate.

Environment

Dubbo version: 2.7.7

Operating System version: CentOS 7

Java version: 1.8.0_202

Steps to reproduce this issue

Code to reproduce: https://github.com/seraphain/dubbo-thread-leak

Start a zookeeper on 127.0.0.1:2181.

Run com.github.seraphain.demo.dubbo.Server. Before the server exits, run com.github.seraphain.demo.dubbo.Client.

Use jps to get client pid: jps | grep Client.

Use jstack to get dubbo threads number: jstack -l [pid] | grep Dubbo | wc -l. The result is 50 or 51.

After the server exits, run com.github.seraphain.demo.dubbo.Server again.

Use jstack to get dubbo thread number: jstack -l [pid] | grep Dubbo | wc -l. The result is 100 or 101.

Repeat step 5 & 6, dubbo thread number keep increasing everytime the server start.

Expected Result

Client dubbo thread number do not increase.

Actual Result

Client dubbo thread number keep increasing everytime the server start.