Merge pull request #6321 from hdm/bug/various-race-conditions

Address race conditions in http.Request and MemGuardian
This commit is contained in:
Mzack9999 2025-07-15 15:19:02 +02:00 committed by GitHub
commit 3e9bee7400
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 12 additions and 5 deletions

View File

@ -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() {

View File

@ -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()
}

View File

@ -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())
}