Skip to content

Commit a7d488a

Browse files
authored
Fix possible race condition on request ctx done #1662 (#1806)
* Fix possible race condition on request ctx done #1662 * Fix possible race condition on request ctx done #1662 * Fix Comment * fix remove the use of lock * fix remove Comment
1 parent 86c7e84 commit a7d488a

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

server.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2743,7 +2743,15 @@ func (ctx *RequestCtx) Deadline() (deadline time.Time, ok bool) {
27432743
// Note: Because creating a new channel for every request is just too expensive, so
27442744
// RequestCtx.s.done is only closed when the server is shutting down.
27452745
func (ctx *RequestCtx) Done() <-chan struct{} {
2746-
return ctx.s.done
2746+
// fix use new variables to prevent panic caused by modifying the original done chan to nil.
2747+
done := ctx.s.done
2748+
2749+
if done == nil {
2750+
done = make(chan struct{}, 1)
2751+
done <- struct{}{}
2752+
return done
2753+
}
2754+
return done
27472755
}
27482756

27492757
// Err returns a non-nil error value after Done is closed,
@@ -2757,7 +2765,7 @@ func (ctx *RequestCtx) Done() <-chan struct{} {
27572765
// RequestCtx.s.done is only closed when the server is shutting down.
27582766
func (ctx *RequestCtx) Err() error {
27592767
select {
2760-
case <-ctx.s.done:
2768+
case <-ctx.Done():
27612769
return context.Canceled
27622770
default:
27632771
return nil

0 commit comments

Comments
 (0)