Skip to content

Commit

Permalink
Fix fetchBody with a retry
Browse files Browse the repository at this point in the history
There seems to be a race condition with chrome and CDP requests when
trying to retrieve the response body. If the call to retrieve the
response body is too "quick", then we get an error back.

However with retries and a sleep it seems to solve the issue with the
small tests i have performed. I suspect this will be a greater issue in
the wild.
  • Loading branch information
ankur22 authored and inancgumus committed Feb 24, 2025
1 parent 1601f55 commit 20d24fb
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion internal/js/modules/k6/browser/common/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,27 @@ func (r *Response) fetchBody() error {
return nil
}
action := network.GetResponseBody(r.request.requestID)
body, err := action.Do(cdp.WithExecutor(r.ctx, r.request.frame.manager.session))

// Try to fetch the response body. If the request to retrieve the response
// body is too "quick" then the response body is not available. After
// retrying we have a better chance of getting the response body.
var body []byte
var err error
maxRetries := 5
for i := 0; i <= maxRetries; i++ {
body, err = action.Do(cdp.WithExecutor(r.ctx, r.request.frame.manager.session))
if err == nil {
break
}
if strings.Contains(err.Error(), "No data found for resource with given identifier") {
if i == maxRetries {
break
}
time.Sleep(100 * time.Millisecond)
continue
}
break
}
if err != nil {
return fmt.Errorf("fetching response body: %w", err)
}
Expand Down

0 comments on commit 20d24fb

Please sign in to comment.