Skip to content

Commit

Permalink
fix: race condition when registering grpc types
Browse files Browse the repository at this point in the history
  • Loading branch information
abemedia committed Jan 29, 2025
1 parent 04fba5c commit 587614d
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions internal/js/modules/k6/grpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"io"
"strings"
"sync"
"time"

"go.k6.io/k6/internal/lib/netext/grpcext"
Expand All @@ -30,6 +31,9 @@ import (
"google.golang.org/protobuf/types/dynamicpb"
)

// globalMutex is used to prevent race conditions during concurrent access to [protoregistry.GlobalTypes].
var globalMutex sync.Mutex //nolint:gochecknoglobals

// Client represents a gRPC client that can be used to make RPC requests
type Client struct {
mds map[string]protoreflect.MethodDescriptor
Expand Down Expand Up @@ -443,13 +447,15 @@ func (c *Client) convertToMethodInfo(fdset *descriptorpb.FileDescriptorSet) ([]M
message := stack[len(stack)-1]
stack = stack[:len(stack)-1]

globalMutex.Lock()
_, errFind := protoregistry.GlobalTypes.FindMessageByName(message.FullName())
if errors.Is(errFind, protoregistry.NotFound) {
err = protoregistry.GlobalTypes.RegisterMessage(dynamicpb.NewMessageType(message))
if err != nil {
return false
}
}
globalMutex.Unlock()

nested := message.Messages()
for i := 0; i < nested.Len(); i++ {
Expand Down

0 comments on commit 587614d

Please sign in to comment.