Skip to content

Commit 1fb8085

Browse files
committed
fix: resolve context canceled when reading HTTP response body (#3141)
Signed-off-by: hadzija7 <[email protected]>
1 parent 76f17f9 commit 1fb8085

1 file changed

Lines changed: 12 additions & 9 deletions

File tree

pkg/components/http/http.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -494,14 +494,15 @@ func (e *HTTP) Execute(ctx core.ExecutionContext) error {
494494
func (e *HTTP) executeHTTPRequest(ctx core.ExecutionContext, spec Spec, retryMetadata RetryMetadata) error {
495495
currentTimeout := e.calculateTimeoutForAttempt(retryMetadata.TimeoutStrategy, retryMetadata.TimeoutSeconds, retryMetadata.Attempt)
496496

497-
resp, err := e.executeRequest(ctx.HTTP, spec, currentTimeout)
497+
resp, cancel, err := e.executeRequest(ctx.HTTP, spec, currentTimeout)
498498
if err != nil {
499499
if retryMetadata.Attempt < retryMetadata.MaxRetries {
500500
return e.scheduleRetry(ctx, err.Error(), retryMetadata)
501501
}
502502

503503
return e.handleRequestError(ctx, err, retryMetadata.Attempt+1)
504504
}
505+
defer cancel()
505506

506507
var isSuccess bool
507508
if spec.SuccessCodes != nil && *spec.SuccessCodes != "" {
@@ -511,7 +512,7 @@ func (e *HTTP) executeHTTPRequest(ctx core.ExecutionContext, spec Spec, retryMet
511512
}
512513

513514
if !isSuccess && retryMetadata.Attempt < retryMetadata.MaxRetries {
514-
515+
_ = resp.Body.Close()
515516
return e.scheduleRetry(ctx, fmt.Sprintf("HTTP status %d", resp.StatusCode), retryMetadata)
516517
}
517518

@@ -578,25 +579,25 @@ func (e *HTTP) calculateTimeoutForAttempt(strategy string, timeoutSeconds int, a
578579
return baseTimeout
579580
}
580581

581-
func (e *HTTP) executeRequest(httpCtx core.HTTPContext, spec Spec, timeout time.Duration) (*http.Response, error) {
582+
func (e *HTTP) executeRequest(httpCtx core.HTTPContext, spec Spec, timeout time.Duration) (*http.Response, context.CancelFunc, error) {
582583
var body io.Reader
583584
var contentType string
584585
var err error
585586
if spec.ContentType != nil && (spec.Method == "POST" || spec.Method == "PUT" || spec.Method == "PATCH") {
586587
body, contentType, err = e.serializePayload(spec)
587588
if err != nil {
588-
return nil, err
589+
return nil, nil, err
589590
}
590591
}
591592

592593
reqCtx, cancel := context.WithTimeout(context.Background(), timeout)
593-
defer cancel()
594594

595595
requestURL := spec.URL
596596
if spec.QueryParams != nil && len(*spec.QueryParams) > 0 {
597597
parsedURL, parseErr := url.Parse(spec.URL)
598598
if parseErr != nil {
599-
return nil, fmt.Errorf("failed to parse url: %w", parseErr)
599+
cancel()
600+
return nil, nil, fmt.Errorf("failed to parse url: %w", parseErr)
600601
}
601602

602603
query := parsedURL.Query()
@@ -610,7 +611,8 @@ func (e *HTTP) executeRequest(httpCtx core.HTTPContext, spec Spec, timeout time.
610611

611612
req, err := http.NewRequestWithContext(reqCtx, spec.Method, requestURL, body)
612613
if err != nil {
613-
return nil, fmt.Errorf("failed to create request: %w", err)
614+
cancel()
615+
return nil, nil, fmt.Errorf("failed to create request: %w", err)
614616
}
615617

616618
if contentType != "" {
@@ -625,10 +627,11 @@ func (e *HTTP) executeRequest(httpCtx core.HTTPContext, spec Spec, timeout time.
625627

626628
resp, err := httpCtx.Do(req)
627629
if err != nil {
628-
return nil, err
630+
cancel()
631+
return nil, nil, err
629632
}
630633

631-
return resp, nil
634+
return resp, cancel, nil
632635
}
633636

634637
func (e *HTTP) handleRequestError(ctx core.ExecutionContext, err error, totalAttempts int) error {

0 commit comments

Comments
 (0)