Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix data race in compiler when resolver provides unlinked descriptor proto #103

Merged
merged 2 commits into from
Mar 2, 2023

Conversation

jhump
Copy link
Member

@jhump jhump commented Mar 1, 2023

From looking at the data race errors in bug report #102, this can happen only if a *descriptorpb.FileDescriptorProto or parser.Result is provided as a search result and is shared across parallel compiler operations.

So, luckily, that means many (most?) usages of the compiler are safe, including the way that buf uses it.

This looks to have always been the case. But I bet the user described this as a new failure because their tests previously didn't all share a search result for "google/protobuf/descriptor.proto". But as of #97, all compile operations will implicitly use a search result for this file. So I suspect the reporter's tests use a resolver that returns a proto for this file and previously only one of the tests actually used it (i.e. only one test actually imports that file).

This is a non-trivial change mainly because I was worried about the performance impact of always cloning values, even if it's not necessary. So the strategy here is to only make a defensive clone of the descriptor proto (or the parser result, which contains a potentially mutable descriptor proto) when necessary. How do we know if it's needed? If it has any type names that are not fully-qualified (field extendees, field types, method input and output types), if it has uninterpreted options, or if it has no source code info but needs it. Under these conditions, the compiler will mutate the proto (to make type names fully-qualified, to interpret options, to add source code info).

Edit: The code now always clones descriptor protos and parser results (which may contain a descriptor proto). That way there's no worry of the mutation logic getting out of sync with the logic that decides whether or not to clone.

The most complicated bit here is actually cloning a parser result, and associated tests, because it has an index that maps elements in the descriptor proto to their corresponding AST nodes. So we have to rebuild that index so it matches the new (cloned) elements in the descriptor proto.

The test added to compiler_test.go successfully reproduces the data race that was reported and is fixed with all of the other changes in this branch.

Fixes #102.

@jhump jhump changed the title Fix data race in compiler Fix data race in compiler when resolver provides unlinked descriptor proto Mar 1, 2023
@jhump jhump requested a review from pkwarren March 1, 2023 23:16
@jhump jhump mentioned this pull request Mar 2, 2023
Copy link
Member

@pkwarren pkwarren left a comment

Choose a reason for hiding this comment

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

A bit concerned with the complexity around clone and also with any possible missed mutations in the checks to determine if we need to clone the parse result or descriptor proto.

.golangci.yml Outdated Show resolved Hide resolved
compiler.go Outdated Show resolved Hide resolved
compiler.go Outdated Show resolved Hide resolved
compiler_test.go Show resolved Hide resolved
compiler.go Outdated Show resolved Hide resolved
@jhump jhump merged commit 2c27603 into main Mar 2, 2023
@jhump jhump deleted the jh/fix-data-race branch March 2, 2023 17:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Data race in v0.3.0
2 participants