Skip to content

grpc_json: use a mutex to protext JsonTranscoderConfig::type_helper_#18894

Closed
qiwzhang wants to merge 1 commit intoenvoyproxy:mainfrom
qiwzhang:use-mutex
Closed

grpc_json: use a mutex to protext JsonTranscoderConfig::type_helper_#18894
qiwzhang wants to merge 1 commit intoenvoyproxy:mainfrom
qiwzhang:use-mutex

Conversation

@qiwzhang
Copy link
Copy Markdown
Contributor

@qiwzhang qiwzhang commented Nov 4, 2021

Signed-off-by: Wayne Zhang qiwzhang@google.com

To fix #18849

JsonTranscoderConfig::type_helper_ is not a thread safe object. Its used
TypeInfoForTypeResolver
uses three mutable maps to cache used data.

Most of places use type_helper_ are in the JsonTranscoderConfig constructor which is only called at main thread.
But there is a place inside JsonTranscoderConfig::createTranscoder and this function is called per-request processing in the worker thread. It may have race condition that multiple threads try to update the mutable maps.

Hence use a mutex to protect it.

Tried alternative approach: creating type_helper_ as thread local. It did not work

grpc_json_transcoder_integration_test failed at this
GOOGLE_CHECK as

GOOGLE_CHECK(file()->finished_building_ == true);

Did not spend too much time to debug it. Maybe descriptor should not be used in the worker threads.

As low risk, just add a mutex to protect it. This ResolveFieldPath should be fairly quick, so the contention should be low, it should not slow down much.

Risk Level: None
Testing: unit tests and integration tests
Docs Changes: None
Release Notes: None

Signed-off-by: Wayne Zhang <qiwzhang@google.com>
@qiwzhang qiwzhang requested a review from lizan as a code owner November 4, 2021 07:22
Copy link
Copy Markdown
Member

@mattklein123 mattklein123 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks I agree this is an OK fix. One small comment, thanks.

/wait

Protobuf::DescriptorPool descriptor_pool_;
google::grpc::transcoding::PathMatcherPtr<MethodInfoSharedPtr> path_matcher_;
mutable absl::Mutex type_helper_mutex_;
std::unique_ptr<google::grpc::transcoding::TypeHelper> type_helper_;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a locked by annotation on this object to make sure we hit all uses now and in the future?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is too complicate to protect all type_helper_. its resolver is already thread-safe. Only its type_info_ is not.

I changed code here to make type_info thread safe.

grpc-ecosystem/grpc-httpjson-transcoding#64

@qiwzhang
Copy link
Copy Markdown
Contributor Author

qiwzhang commented Nov 4, 2021

Abandon this changes. Instead of using this

grpc-ecosystem/grpc-httpjson-transcoding#64

@qiwzhang qiwzhang closed this Nov 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

grpc transcoder config may lead to crash

2 participants