Fix mutex lock error in logging utils #478
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.
What
Right now when we run C++ unit tests we are getting this error in logs:
libc++abi: terminating due to uncaught exception of type std::__1::system_error: mutex lock failed: Invalid argument
, even though the test still passes.This is coming from
std::lock_guard<std::mutex> lock(mtx_);
inmemory_data
The root cause is because of the
_count_data.insert_entry(msg_ + " num_ss_comps", num_comps_);
call in the destructor oflogging_sum_of_squares_distance
object.I believe the root cause is because of the static initialization order fiasco. Though I'm not exactly sure of the reasoning, I believe the
count_data
object is being initialized before themtx_
mutex object.One particularly strange thing is that when I log out all the calls, I don't see
[logging_sum_of_squares_distance@dtor]
or[count_data@insert_entry]
being printed. But if I comment out_count_data.insert_entry(msg_ + " num_ss_comps", num_comps_);
orstd::lock_guard<std::mutex> lock(mtx_);
then the error goes away.Regardless, the fix we make in this PR is to use another approach to initialize the
get_instance()
function. Before we used the Meyers Singleton pattern, but now we change to a new thread-safe lazy initialization pattern, which fixes this error and still passes tests.Testing
libc++abi: terminating due to uncaught exception of type std::__1::system_error: mutex lock failed: Invalid argument
anymore.