From 6bf3f147985c499113c74ab64e66d2fa6e351741 Mon Sep 17 00:00:00 2001 From: HD Moore Date: Tue, 15 Jul 2025 02:34:29 -0500 Subject: [PATCH 1/2] avoid data races by using request clones --- pkg/protocols/http/request.go | 6 ++++-- pkg/protocols/http/utils.go | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/pkg/protocols/http/request.go b/pkg/protocols/http/request.go index d9b30ab72..381f4d385 100644 --- a/pkg/protocols/http/request.go +++ b/pkg/protocols/http/request.go @@ -864,8 +864,10 @@ func (request *Request) executeRequest(input *contextargs.Context, generatedRequ var curlCommand string if !request.Unsafe && resp != nil && generatedRequest.request != nil && resp.Request != nil && !request.Race { bodyBytes, _ := generatedRequest.request.BodyBytes() - resp.Request.Body = io.NopCloser(bytes.NewReader(bodyBytes)) - command, err := http2curl.GetCurlCommand(generatedRequest.request.Request) + // Use a clone to avoid a race condition with the http transport + req := resp.Request.Clone(resp.Request.Context()) + req.Body = io.NopCloser(bytes.NewReader(bodyBytes)) + command, err := http2curl.GetCurlCommand(req) if err == nil && command != nil { curlCommand = command.String() } diff --git a/pkg/protocols/http/utils.go b/pkg/protocols/http/utils.go index 92aa870dc..90b2e8446 100644 --- a/pkg/protocols/http/utils.go +++ b/pkg/protocols/http/utils.go @@ -12,7 +12,8 @@ import ( // dump creates a dump of the http request in form of a byte slice func dump(req *generatedRequest, reqURL string) ([]byte, error) { if req.request != nil { - bin, err := req.request.Dump() + // Use a clone to avoid a race condition with the http transport + bin, err := req.request.Clone(req.request.Context()).Dump() if err != nil { return nil, errorutil.NewWithErr(err).WithTag("http").Msgf("could not dump request: %v", req.request.String()) } From 875941ce8d227ba6fed61dd053e7cc468b53d278 Mon Sep 17 00:00:00 2001 From: HD Moore Date: Tue, 15 Jul 2025 02:34:47 -0500 Subject: [PATCH 2/2] avoid data races using mutex for memguardian --- pkg/protocols/common/protocolstate/memguardian.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pkg/protocols/common/protocolstate/memguardian.go b/pkg/protocols/common/protocolstate/memguardian.go index dac57cb7e..8a0d9699a 100644 --- a/pkg/protocols/common/protocolstate/memguardian.go +++ b/pkg/protocols/common/protocolstate/memguardian.go @@ -16,9 +16,12 @@ var ( MaxBytesBufferAllocOnLowMemory = env.GetEnvOrDefault("MEMGUARDIAN_ALLOC", 0) memTimer *time.Ticker cancelFunc context.CancelFunc + muGlobalChange sync.Mutex ) func StartActiveMemGuardian(ctx context.Context) { + muGlobalChange.Lock() + defer muGlobalChange.Unlock() if memguardian.DefaultMemGuardian == nil || memTimer != nil { return } @@ -42,6 +45,9 @@ func StartActiveMemGuardian(ctx context.Context) { } func StopActiveMemGuardian() { + muGlobalChange.Lock() + defer muGlobalChange.Unlock() + if memguardian.DefaultMemGuardian == nil { return } @@ -73,8 +79,6 @@ func GuardThreadsOrDefault(current int) int { return 1 } -var muGlobalChange sync.Mutex - // Global setting func GlobalGuardBytesBufferAlloc() error { if !muGlobalChange.TryLock() {