Skip to content

Commit 548790e

Browse files
dmitshurgopherbot
authored andcommitted
net/http: close req.Body only when it's non-nil on js
The main change here is fixing the panic where it was called even when req.Body was nil. It might also work better to keep the req.Body.Close calls closer after req.Body is read, so do that too. Calling readableStreamPull.Release on a js.Func with a zero value is currently a no-op, but it seems better to avoid it anyway. Also remove readableStreamStart, readableStreamCancel while here. They were used in the initial but not final patch set of CL 458395. Fixes #60809. Change-Id: I6ff2e3b6ec2cd4b0c9c67939903e32908312db8d Reviewed-on: https://go-review.googlesource.com/c/go/+/503676 Reviewed-by: Dmitri Shuralyov <[email protected]> Reviewed-by: Johan Brandhorst-Satzkorn <[email protected]> Auto-Submit: Dmitri Shuralyov <[email protected]> Reviewed-by: Bryan Mills <[email protected]> Run-TryBot: Dmitri Shuralyov <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 6dc2d2a commit 548790e

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

src/net/http/roundtrip_js.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,14 @@ func (t *Transport) RoundTrip(req *Request) (*Response, error) {
135135
}
136136
opt.Set("headers", headers)
137137

138-
var readableStreamStart, readableStreamPull, readableStreamCancel js.Func
139138
if req.Body != nil {
140139
if !supportsPostRequestStreams() {
141140
body, err := io.ReadAll(req.Body)
142141
if err != nil {
143142
req.Body.Close() // RoundTrip must always close the body, including on errors.
144143
return nil, err
145144
}
145+
req.Body.Close()
146146
if len(body) != 0 {
147147
buf := uint8Array.New(len(body))
148148
js.CopyBytesToJS(buf, body)
@@ -153,7 +153,7 @@ func (t *Transport) RoundTrip(req *Request) (*Response, error) {
153153
readableStreamCtorArg.Set("type", "bytes")
154154
readableStreamCtorArg.Set("autoAllocateChunkSize", t.writeBufferSize())
155155

156-
readableStreamPull = js.FuncOf(func(this js.Value, args []js.Value) any {
156+
readableStreamPull := js.FuncOf(func(this js.Value, args []js.Value) any {
157157
controller := args[0]
158158
byobRequest := controller.Get("byobRequest")
159159
if byobRequest.IsNull() {
@@ -181,6 +181,10 @@ func (t *Transport) RoundTrip(req *Request) (*Response, error) {
181181
// Note: This a return from the pull callback of the controller and *not* RoundTrip().
182182
return nil
183183
})
184+
defer func() {
185+
readableStreamPull.Release()
186+
req.Body.Close()
187+
}()
184188
readableStreamCtorArg.Set("pull", readableStreamPull)
185189

186190
opt.Set("body", js.Global().Get("ReadableStream").New(readableStreamCtorArg))
@@ -201,11 +205,6 @@ func (t *Transport) RoundTrip(req *Request) (*Response, error) {
201205
success = js.FuncOf(func(this js.Value, args []js.Value) any {
202206
success.Release()
203207
failure.Release()
204-
readableStreamCancel.Release()
205-
readableStreamPull.Release()
206-
readableStreamStart.Release()
207-
208-
req.Body.Close()
209208

210209
result := args[0]
211210
header := Header{}
@@ -270,11 +269,6 @@ func (t *Transport) RoundTrip(req *Request) (*Response, error) {
270269
failure = js.FuncOf(func(this js.Value, args []js.Value) any {
271270
success.Release()
272271
failure.Release()
273-
readableStreamCancel.Release()
274-
readableStreamPull.Release()
275-
readableStreamStart.Release()
276-
277-
req.Body.Close()
278272

279273
err := args[0]
280274
// The error is a JS Error type

0 commit comments

Comments
 (0)