This is a tricky issue found while debugging failure of "flaky" watcher test (
ZOOKEEPER-2686). When closing a Netty connection, depend on timing the connection bean registered when the connection was provisioned might not get unregistered, leading to leaked Java beans.
The race happens at the time when the client is in the process of finalizing the session. As part of session finalization, a connection bean will be registered . But right before the registering bean, the connection might gets closed, in cases for example the server that the client is connecting to is shutdown. As part of connection close, the bean will be un-registered, as expected , however the problem is when we execute at , the connection bean might not finish registering at , so the unregister of bean is a NOP. What's worse, as part of connection close, we remove this connection from connection factory , so future connection close call will get short circuited and directly return; in other words the bean unregister code in connection close call will only get executed once. Depends on luck, the bean might not get unregistered, as previously illustrated.