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

Race condition in func WithCustomAttributes(ctx context.Context, attrs map[string]string) #1032

Open
volkerhess2 opened this issue Apr 5, 2024 · 0 comments

Comments

@volkerhess2
Copy link

volkerhess2 commented Apr 5, 2024

The WithCustomAttributes function currently takes a map as input value. Since map is a pointer value, the pointer is passed to msg.Attributes of the pubsub message and then is used throughout the sdk. The map is modified in multiple places. If the pointer to the map is used outside of the event or if the map is reused for multiple events there will be race conditions.

https://github.com/cloudevents/sdk-go/blob/main/protocol/pubsub/v2/attributes.go

type withCustomAttributes struct{}

func AttributesFrom(ctx context.Context) map[string]string {
	return binding.GetOrDefaultFromCtx(ctx, withCustomAttributes{}, make(map[string]string)).(map[string]string)
}

// WithCustomAttributes sets Message Attributes without any CloudEvent logic.
// Note that this function is not intended for CloudEvent Extensions or any `ce-`-prefixed Attributes.
// For these please see `Event` and `Event.SetExtension`.
func WithCustomAttributes(ctx context.Context, attrs map[string]string) context.Context {
	return context.WithValue(ctx, withCustomAttributes{}, attrs)
}

https://github.com/cloudevents/sdk-go/blob/main/protocol/pubsub/v2/protocol.go

// Send implements Sender.Send
func (t *Protocol) Send(ctx context.Context, in binding.Message, transformers ...binding.Transformer) error {
	var err error
	defer func() { _ = in.Finish(err) }()

	topic := cecontext.TopicFrom(ctx)
	if topic == "" {
		topic = t.topicID
	}

	conn := t.getOrCreateConnection(ctx, topic, "", "")

	msg := &pubsub.Message{
		Attributes: AttributesFrom(ctx),
	}
...

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

No branches or pull requests

1 participant