Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix possible race condition on request ctx done #1662 #1806

Merged
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2731,7 +2731,15 @@ func (ctx *RequestCtx) Deadline() (deadline time.Time, ok bool) {
// Note: Because creating a new channel for every request is just too expensive, so
// RequestCtx.s.done is only closed when the server is shutting down
func (ctx *RequestCtx) Done() <-chan struct{} {
return ctx.s.done
// fix use new variables to prevent panic caused by modifying the original done chan to nil.
done := ctx.s.done

if done == nil {
done = make(chan struct{}, 1)
done <- struct{}{}
return done
}
return done
}

// Err returns a non-nil error value after Done is closed,
Expand All @@ -2745,7 +2753,7 @@ func (ctx *RequestCtx) Done() <-chan struct{} {
// RequestCtx.s.done is only closed when the server is shutting down
func (ctx *RequestCtx) Err() error {
select {
case <-ctx.s.done:
case <-ctx.Done(): // fix Use unified functions instead of reference variables to converge fetching into one place
byte0o marked this conversation as resolved.
Show resolved Hide resolved
return context.Canceled
default:
return nil
Expand Down