diff --git a/context.go b/context.go index 582d00f2..ef21b7bc 100644 --- a/context.go +++ b/context.go @@ -117,8 +117,11 @@ func (c *Context) reset() { *c.skippedNodes = (*c.skippedNodes)[:0] } -// Copy returns a copy of the current context that can be safely used outside the request's scope. +// Copy returns a copy of the current context for use outside the request's scope. // This has to be used when the context has to be passed to a goroutine. +// +// The copy's underlying http.ResponseWriter is nil, so it cannot write the HTTP response. +// Do not call response-writing methods such as JSON, String, or Stream on the copy. func (c *Context) Copy() *Context { cp := Context{ writermem: c.writermem, diff --git a/docs/doc.md b/docs/doc.md index 01edf52c..83edcbec 100644 --- a/docs/doc.md +++ b/docs/doc.md @@ -617,6 +617,8 @@ func main() { When starting new Goroutines inside a middleware or handler, you **SHOULD NOT** use the original context inside it, you have to use a read-only copy. +The copy's underlying `http.ResponseWriter` is `nil`, so it cannot write the HTTP response. Do not call response-writing methods such as `JSON`, `String`, or `Stream` on the copy. Write the response from the original handler before it returns. + ```go func main() { r := gin.Default()