From f6e270ae9c9fdb7b04fbd0115ecc347b5380c3d3 Mon Sep 17 00:00:00 2001 From: diamondburned Date: Sat, 14 Nov 2020 14:18:50 -0800 Subject: [PATCH] json: Fixed a race condition with erroneous JSON streams --- utils/httputil/options.go | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/utils/httputil/options.go b/utils/httputil/options.go index cb5d45e..7e19626 100644 --- a/utils/httputil/options.go +++ b/utils/httputil/options.go @@ -78,25 +78,14 @@ func WithBody(body io.ReadCloser) RequestOption { // WithJSONBody inserts a JSON body into the request. This ignores JSON errors. func WithJSONBody(v interface{}) RequestOption { if v == nil { - return func(httpdriver.Request) error { - return nil - } + return func(httpdriver.Request) error { return nil } } var rp, wp = io.Pipe() - var err error - go func() { - err = json.EncodeStream(wp, v) - wp.Close() - }() + go func() { wp.CloseWithError(json.EncodeStream(wp, v)) }() return func(r httpdriver.Request) error { - // TODO: maybe do something to this? - if err != nil { - return err - } - r.AddHeader(http.Header{ "Content-Type": {"application/json"}, })