From 587614d37057e0c0b11fc230a175af218435c2f4 Mon Sep 17 00:00:00 2001 From: Adam Bouqdib Date: Wed, 29 Jan 2025 15:06:18 +0000 Subject: [PATCH] fix: race condition when registering grpc types --- internal/js/modules/k6/grpc/client.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/internal/js/modules/k6/grpc/client.go b/internal/js/modules/k6/grpc/client.go index 20b507829ff..176cc72c22d 100644 --- a/internal/js/modules/k6/grpc/client.go +++ b/internal/js/modules/k6/grpc/client.go @@ -9,6 +9,7 @@ import ( "fmt" "io" "strings" + "sync" "time" "go.k6.io/k6/internal/lib/netext/grpcext" @@ -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 @@ -443,6 +447,7 @@ 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)) @@ -450,6 +455,7 @@ func (c *Client) convertToMethodInfo(fdset *descriptorpb.FileDescriptorSet) ([]M return false } } + globalMutex.Unlock() nested := message.Messages() for i := 0; i < nested.Len(); i++ {