[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.testLookupThrottling
is flaky, from the logs of #11760 we can see the test crashed inClientImpl::shutdown
. TheConnection closed
log shows thatConnectionPool
has been closed, so the problem is related to the close of threeExecutorServiceProvider
s ofClientImpl
.ExecutorServiceProvider::close
is not thread safe becauseExecutorServiceProvider::get
might access theexecutors_
field in different threads andget
uses a lock to guarantee thread-safety whileclose
doesn't. In addition, some clean up methods includingClientImpl#shutdown
andExecutorService#close
might be called twice or more, which is not necessary and has potential bugs.Modifications
The main changes are:
ExecutorServiceProvider::close
.ExecutorService#close
is called only once.ConnectionPool#getConnectionAsync
being called afterclose
method 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 fromstatic
toinline
. 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 modifiedstatic
back toinline
and fix related compilation errors.Verifying this change
This change is a trivial rework / code cleanup without any test coverage.