Open Syuparn opened 1 year ago
concurrent writes occur when t.handler
writes response just after c.Writer
is changed for t.response()
.
https://github.com/gin-contrib/timeout/blob/master/timeout.go#L88
Since writer is changed to the original one, lock does not work on this line.
How about copying context before replacing Writer? In this case, t.handler
cannot write to copied context's writer.
+ cc := c.Copy()
+ cc.Writer = w
+ t.response(cc)
- c.Writer = w
- t.response(c)
- c.Writer = tw
When a handler passed to
timeout.WithHandler
completed and the middleware timed out at the same time, the context writes both response bodies or panics due to concurrent writes.Reproduction
Result
response bodies
Approximately 15% responses has duplicated response body.
panic
Also, server sometimes crashes due to concurrent map writes.
Panic stacktraces (click to open)
version