From 3a9f6c010bf1236679548d882f7934624e9a3245 Mon Sep 17 00:00:00 2001 From: Rohan Verma Date: Wed, 6 Nov 2024 12:05:54 +0530 Subject: [PATCH] fix: prevent buffer reuse in async mode for Put method If the mode is async, we need to copy request Blob to prevent fasthttp from reusing the buffer since we are going to use the buffer in a separate goroutine beyond the scope of the current request. --- stores/goredis/redis.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/stores/goredis/redis.go b/stores/goredis/redis.go index 614966f..81793bf 100644 --- a/stores/goredis/redis.go +++ b/stores/goredis/redis.go @@ -169,7 +169,14 @@ type putReq struct { // Put sets a value to given session but stored only on commit func (s *Store) Put(namespace, group, uri string, b fastcache.Item, ttl time.Duration) error { if s.config.Async { - s.putBuf <- putReq{namespace, group, uri, b, ttl} + // If the mode is async, we need to copy b.Blob to prevent + // fasthttp from reusing the buffer since we are going to + // use the buffer in a separate goroutine beyond the scope + // of the current request. + copiedItem := b + copiedItem.Blob = append([]byte(nil), b.Blob...) + + s.putBuf <- putReq{namespace, group, uri, copiedItem, ttl} return nil }