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

net/http: add context cancellation reason for server handlers #70850

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all 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
26 changes: 20 additions & 6 deletions src/net/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ var (
// anything in the net/http package. Callers should not
// compare errors against this variable.
ErrWriteAfterFlush = errors.New("unused")

// TODO: expose once proposal is accepted
// errConnectionClosed is used as a context Cause for contexts
// cancelled because the client closed their connection while
// a request was being handled.
errConnectionClosed = errors.New("connection closed by client")

// TODO: expose once proposal is accepted
// errRequestHandlerReturned is used as a context Cause for contexts
// cancelled because the request handler already returned.
errRequestHandlerReturned = errors.New("request handler returned")
)

// A Handler responds to an HTTP request.
Expand Down Expand Up @@ -257,7 +268,7 @@ type conn struct {
server *Server

// cancelCtx cancels the connection-level context.
cancelCtx context.CancelFunc
cancelCtx context.CancelCauseFunc

// rwc is the underlying network connection.
// This is never wrapped by other types and is the value given out
Expand Down Expand Up @@ -754,8 +765,11 @@ func (cr *connReader) hitReadLimit() bool { return cr.remain <= 0 }
// down its context.
//
// It may be called from multiple goroutines.
func (cr *connReader) handleReadError(_ error) {
cr.conn.cancelCtx()
func (cr *connReader) handleReadError(err error) {
if errors.Is(err, io.EOF) {
err = errConnectionClosed
}
cr.conn.cancelCtx(err)
cr.closeNotify()
}

Expand Down Expand Up @@ -2005,9 +2019,9 @@ func (c *conn) serve(ctx context.Context) {

// HTTP/1.x from here on.

ctx, cancelCtx := context.WithCancel(ctx)
ctx, cancelCtx := context.WithCancelCause(ctx)
c.cancelCtx = cancelCtx
defer cancelCtx()
defer cancelCtx(errRequestHandlerReturned)

c.r = &connReader{conn: c}
c.bufr = newBufioReader(c.r)
Expand Down Expand Up @@ -4021,7 +4035,7 @@ func (w checkConnErrorWriter) Write(p []byte) (n int, err error) {
n, err = w.c.rwc.Write(p)
if err != nil && w.c.werr == nil {
w.c.werr = err
w.c.cancelCtx()
w.c.cancelCtx(err)
}
return
}
Expand Down