Ah I get it now: it's due to application starting to send a response before the request has been completely received, as per the response to your ticket which links to https://github.com/golang/go/issues/15527
On Saturday, 19 June 2021 at 11:15:34 UTC+1 Brian Candler wrote: > Here is a variation on your program, removing json.Encoder from the > equation: > https://play.golang.org/p/ACS_n1yNJfQ > > It works as shown, but if you uncomment the commented-out fmt.Fprintf, it > fails. (macOS, go1.16.5) > > This is indeed weird. As far as I can see, the only way MaxBytesReader > <https://golang.org/src/net/http/request.go?s=36939:37016#L1115> > interacts with its Writer (w) argument is to call w.requestTooLarge() if > the limit is exceeded *and* w has that method. But I've checked and it > doesn't. > > On Saturday, 19 June 2021 at 06:19:22 UTC+1 jerome....@gmail.com wrote: > >> I submit this issue https://github.com/golang/go/issues/46826 >> >> Le vendredi 18 juin 2021 à 21:15:54 UTC+2, Jérôme LAFORGE a écrit : >> >>> Sorry, I wasn't clear enough. >>> I know that panic comes from my code. It throws it. >>> But I wanted to show that error on Decode must not occur because the >>> body is smaller than 8kB. >>> The 2nd example works like this. >>> The only difference between the two examples is just the response is >>> discarded. >>> >>> Le vendredi 18 juin 2021 à 20:37:11 UTC+2, seank...@gmail.com a écrit : >>> >>>> I think it's working as expected? >>>> The panic originates in your code: >>>> >>>> ``` >>>> err := decoder.Decode(&el) >>>> if err != nil { >>>> panic(err) >>>> } >>>> ``` >>>> >>>> What you should be doing is to break out of the loop and handle the >>>> error (client sending more than you want to read) gracefully >>>> On Friday, June 18, 2021 at 4:32:11 PM UTC+2 jerome....@gmail.com >>>> wrote: >>>> >>>>> Hello, >>>>> I am facing to unexpected behavior when I want to limit the maximum >>>>> body request and managing the request/response as streaming way. >>>>> >>>>> With this code https://play.golang.org/p/eN2XEpiPdhn, when I send the >>>>> request, I have this unexpected error (whatever the max size): >>>>> >>>>> ``` >>>>> 2021/06/18 15:48:49 http: panic serving 127.0.0.1:39024: http: >>>>> invalid Read on closed Body >>>>> goroutine 6 [running]: >>>>> net/http.(*conn).serve.func1(0xc000110960) >>>>> net/http/server.go:1824 +0x153 >>>>> panic(0x671b60, 0xc000012af0) >>>>> runtime/panic.go:971 +0x499 >>>>> main.serve(0x713cd0, 0xc0001480e0, 0xc000164000) >>>>> autodebit/service/order/cmd/main.go:32 +0x3b7 >>>>> net/http.HandlerFunc.ServeHTTP(0x6d2608, 0x713cd0, 0xc0001480e0, >>>>> 0xc000164000) >>>>> net/http/server.go:2069 +0x44 >>>>> net/http.serverHandler.ServeHTTP(0xc000148000, 0x713cd0, 0xc0001480e0, >>>>> 0xc000164000) >>>>> net/http/server.go:2887 +0xa3 >>>>> net/http.(*conn).serve(0xc000110960, 0x714180, 0xc000028280) >>>>> net/http/server.go:1952 +0x8cd >>>>> created by net/http.(*Server).Serve >>>>> net/http/server.go:3013 +0x39b >>>>> ``` >>>>> >>>>> But if I discard the response (via io.Discard) that works as expected >>>>> https://play.golang.org/p/ZOJQz6EC--V >>>>> >>>>> I don't understand what it is going wrong. Can I use >>>>> http.MaxBytesReader when I send response as streaming way? >>>>> >>>>> Thanks for your help >>>>> Jérôme >>>>> >>>>> the body file seems too big to be in attach, I put it on >>>>> https://sharetext.me/iveuebzrfq >>>>> the curl cmd: >>>>> curl POST 'http://127.0.0.1:8081' --data-binary '@body.jsonl' >>>>> >>>>> -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/300c2261-c478-48b5-ba6c-eeede6a31dfdn%40googlegroups.com.