You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The operation of "timeout" should be removed because it may lead to multiple handlers using the same gin.Context simultaneously, thus causing incorrect response results.
#67
Open
skyterra opened this issue
May 8, 2024
· 1 comment
// New wraps a handler and aborts the process of the handler if the timeout is reachedfuncNew(opts...Option) gin.HandlerFunc {
t:=&Timeout{
timeout: defaultTimeout,
handler: nil,
response: defaultResponse,
}
// Loop through each optionfor_, opt:=rangeopts {
ifopt==nil {
panic("timeout Option not be nil")
}
// Call the option giving the instantiatedopt(t)
}
ift.timeout<=0 {
returnt.handler
}
bufPool=&BufferPool{}
returnfunc(c*gin.Context) {
finish:=make(chanstruct{}, 1)
panicChan:=make(chaninterface{}, 1)
w:=c.Writerbuffer:=bufPool.Get()
tw:=NewWriter(w, buffer)
c.Writer=twbuffer.Reset()
gofunc() {
deferfunc() {
ifp:=recover(); p!=nil {
panicChan<-p
}
}()
t.handler(c)
finish<-struct{}{}
}()
select {
casep:=<-panicChan:
tw.FreeBuffer()
c.Writer=wpanic(p)
case<-finish:
c.Next()
tw.mu.Lock()
defertw.mu.Unlock()
dst:=tw.ResponseWriter.Header()
fork, vv:=rangetw.Header() {
dst[k] =vv
}
if_, err:=tw.ResponseWriter.Write(buffer.Bytes()); err!=nil {
panic(err)
}
tw.FreeBuffer()
bufPool.Put(buffer)
case<-time.After(t.timeout):
c.Abort()
tw.mu.Lock()
defertw.mu.Unlock()
tw.timeout=truetw.FreeBuffer()
bufPool.Put(buffer)
c.Writer=wt.response(c)
c.Writer=tw
}
}
}
case <-time.After(t.timeout): c.Abort()
If a timeout occurs and c.Abort() is called here, but the handler function has not completed execution (for example, waiting for database data to be read), it will still continue to execute. In such a case, when c.PureJSON() is called in the handler function, c may have already been used by subsequent requests.
so, i suggest remove timeout operation.
The text was updated successfully, but these errors were encountered:
We are having a weird situation where this might be the issue.
We end with a request being responded with TWO json, where the first JSON seems to come from a previous request.
I can say thought the first request failed with a context cancel, and the second request when through successfully but the final response JSON seems to have, as mentioned, both message!
I don't see though how come the context could be shared between two request (I might not seeing in the library code..), even thought that seems exactly what it is happening!
case <-time.After(t.timeout): c.Abort()
If a timeout occurs and c.Abort() is called here, but the handler function has not completed execution (for example, waiting for database data to be read), it will still continue to execute. In such a case, when c.PureJSON() is called in the handler function, c may have already been used by subsequent requests.
so, i suggest remove timeout operation.
The text was updated successfully, but these errors were encountered: