Skip to content

Commit

Permalink
Fix race when getting body - fixes #4596
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoykov committed Mar 6, 2025
1 parent dafd7f8 commit ff5aa4c
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions internal/js/modules/k6/browser/browser/response_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ func mapResponseEvent(vu moduleVU, event common.PageOnEvent) mapping {
}

// mapResponse to the JS module.
//
//nolint:funlen
func mapResponse(vu moduleVU, r *common.Response) mapping {
if r == nil {
return nil
Expand All @@ -23,14 +25,23 @@ func mapResponse(vu moduleVU, r *common.Response) mapping {
})
},
"body": func() *sobek.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
rt := vu.Runtime()
promise, res, rej := rt.NewPromise()
callback := vu.RegisterCallback()
go func() {
body, err := r.Body()
if err != nil {
return nil, err //nolint: wrapcheck
callback(func() error {
return rej(err)
})
return
}
buf := vu.Runtime().NewArrayBuffer(body)
return &buf, nil
})
callback(func() error {
buf := vu.Runtime().NewArrayBuffer(body)
return res(&buf)
})
}()
return promise
},
"frame": func() mapping {
return mapFrame(vu, r.Frame())
Expand Down

0 comments on commit ff5aa4c

Please sign in to comment.