From 7d97879b2278655b5137584593d0dc5ff73f8a83 Mon Sep 17 00:00:00 2001 From: Knative Prow Robot Date: Wed, 22 May 2024 22:54:51 +0100 Subject: [PATCH] [release-1.14] Copy message before passing it to eventtype autocreate handler (#7937) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Copy message before passing it to eventtype autocreate handler Co-authored-by: Christoph Stäbler --- pkg/kncloudevents/event_dispatcher.go | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/pkg/kncloudevents/event_dispatcher.go b/pkg/kncloudevents/event_dispatcher.go index 54b12276703..a80a17f6e41 100644 --- a/pkg/kncloudevents/event_dispatcher.go +++ b/pkg/kncloudevents/event_dispatcher.go @@ -26,9 +26,10 @@ import ( "net/http" "time" + "github.com/cloudevents/sdk-go/v2/binding/buffering" + cloudevents "github.com/cloudevents/sdk-go/v2" "github.com/cloudevents/sdk-go/v2/binding" - "github.com/cloudevents/sdk-go/v2/binding/buffering" "github.com/cloudevents/sdk-go/v2/event" cehttp "github.com/cloudevents/sdk-go/v2/protocol/http" "github.com/hashicorp/go-retryablehttp" @@ -248,7 +249,11 @@ func (d *Dispatcher) send(ctx context.Context, message binding.Message, destinat messagesToFinish = append(messagesToFinish, responseMessage) if config.eventTypeAutoHandler != nil { - d.handleAutocreate(ctx, responseMessage, config) + // messages can only be read once, so we need to make a copy of it + responseMessage, err = buffering.CopyMessage(ctx, responseMessage) + if err == nil { + d.handleAutocreate(ctx, responseMessage, config) + } } if config.reply == nil { @@ -354,15 +359,8 @@ func (d *Dispatcher) executeRequest(ctx context.Context, target duckv1.Addressab return ctx, responseMessage, &dispatchInfo, nil } -func (d *Dispatcher) handleAutocreate(ctx context.Context, responseMessage binding.Message, config *senderConfig) { - // messages can only be read once, so we need to make a copy of it - messageCopy, err := buffering.CopyMessage(ctx, responseMessage) - if err != nil { - return - } - defer responseMessage.Finish(nil) - - responseEvent, err := binding.ToEvent(ctx, messageCopy) +func (d *Dispatcher) handleAutocreate(ctx context.Context, msg binding.Message, config *senderConfig) { + responseEvent, err := binding.ToEvent(ctx, msg) if err != nil { return }