gin-gonic / gin

Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance -- up to 40 times faster. If you need smashing performance, get yourself some Gin.
https://gin-gonic.com/
MIT License
78.83k stars 8.02k forks source link

Panic if using copied context for streaming #1763

Open ghost opened 5 years ago

ghost commented 5 years ago

Description

Hi, I get the following crash if I use a copied context (inside a goroutine) and I try to start a .Stream command:

panic: interface conversion: interface is nil, not http.CloseNotifier

goroutine 63 [running]: github.com/gin-gonic/gin.(responseWriter).CloseNotify(0xc0000ccbb0, 0x100010000) /home/pippus/gocode/pkg/mod/github.com/gin-gonic/gin@v1.3.0/response_writer.go:108 +0x43 github.com/gin-gonic/gin.(Context).Stream(0xc0000ccbb0, 0xc000144fa0) /home/pippus/gocode/pkg/mod/github.com/gin-gonic/gin@v1.3.0/context.go:823 +0x42

It looks like this is because the Copy function nullifies the ResponseWriter used by the stream.

How should this work? It is possible to use context.Stream inside a goroutine?

Thanks in advance for your time.

palvarezcordoba commented 5 years ago

Writing into a Writer from a goroutine does not seems safe. https://www.reddit.com/r/golang/comments/3xpzom/are_the_write_funcs_in_iowritecloser_goroutinesafe/

palvarezcordoba commented 5 years ago

Why do you need to write from a goroutine? Maybe you could do something like this: https://github.com/drone/drone/blob/master/server/stream.go