[C++] Make some clean up methods thread safe #11762
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #11760
Motivation
BasicEndToEndTest.testLookupThrottlingis flaky, from the logs of #11760 we can see the test crashed inClientImpl::shutdown. TheConnection closedlog shows thatConnectionPoolhas been closed, so the problem is related to the close of threeExecutorServiceProviders ofClientImpl.ExecutorServiceProvider::closeis not thread safe becauseExecutorServiceProvider::getmight access theexecutors_field in different threads andgetuses a lock to guarantee thread-safety whileclosedoesn't. In addition, some clean up methods includingClientImpl#shutdownandExecutorService#closemight be called twice or more, which is not necessary and has potential bugs.Modifications
The main changes are:
ExecutorServiceProvider::close.ExecutorService#closeis called only once.ConnectionPool#getConnectionAsyncbeing called afterclosemethod is closed. In this case, return a future that is completed withResultAlreadyClosed.In addition, this PR involves other changes. First, the master branch's source code cannot be compiled in my local env (macOS + Clang 12.0.0 + Boost 1.74), the error is:
I followed boostorg/proto#30 to fix this compilation error.
Another change is to fix the logs format, we can see the filename is always the same from logs in #11760. The reason is #11668 has changed
logger()method fromstatictoinline. It will make all C++'s translation units share the same and random chosen definition oflogger(), but we need different definitions with different__FILE__in different translation units. So this PR modifiedstaticback toinlineand fix related compilation errors.Verifying this change
This change is a trivial rework / code cleanup without any test coverage.