Skip to content

Commit ff5aa4c

Browse files
committed
Fix race when getting body - fixes #4596
1 parent dafd7f8 commit ff5aa4c

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

internal/js/modules/k6/browser/browser/response_mapping.go

+16-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ func mapResponseEvent(vu moduleVU, event common.PageOnEvent) mapping {
1212
}
1313

1414
// mapResponse to the JS module.
15+
//
16+
//nolint:funlen
1517
func mapResponse(vu moduleVU, r *common.Response) mapping {
1618
if r == nil {
1719
return nil
@@ -23,14 +25,23 @@ func mapResponse(vu moduleVU, r *common.Response) mapping {
2325
})
2426
},
2527
"body": func() *sobek.Promise {
26-
return k6ext.Promise(vu.Context(), func() (any, error) {
28+
rt := vu.Runtime()
29+
promise, res, rej := rt.NewPromise()
30+
callback := vu.RegisterCallback()
31+
go func() {
2732
body, err := r.Body()
2833
if err != nil {
29-
return nil, err //nolint: wrapcheck
34+
callback(func() error {
35+
return rej(err)
36+
})
37+
return
3038
}
31-
buf := vu.Runtime().NewArrayBuffer(body)
32-
return &buf, nil
33-
})
39+
callback(func() error {
40+
buf := vu.Runtime().NewArrayBuffer(body)
41+
return res(&buf)
42+
})
43+
}()
44+
return promise
3445
},
3546
"frame": func() mapping {
3647
return mapFrame(vu, r.Frame())

0 commit comments

Comments
 (0)