From 833edaf63d3842987298179e93f5b5c362382c7a Mon Sep 17 00:00:00 2001 From: Adnan Maolood Date: Tue, 23 Feb 2021 16:06:57 -0500 Subject: [PATCH] server: Cancel context on IO errors --- server.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/server.go b/server.go index 94103bb..d577c97 100644 --- a/server.go +++ b/server.go @@ -344,9 +344,24 @@ func (srv *Server) ServeConn(ctx context.Context, conn net.Conn) error { } func (srv *Server) serveConn(ctx context.Context, conn net.Conn) error { - w := newResponseWriter(conn) + ctx, cancel := context.WithCancel(ctx) + done := ctx.Done() + cw := &contextWriter{ + ctx: ctx, + done: done, + cancel: cancel, + wc: conn, + } + r := &contextReader{ + ctx: ctx, + done: done, + cancel: cancel, + rc: conn, + } - req, err := ReadRequest(conn) + w := newResponseWriter(cw) + + req, err := ReadRequest(r) if err != nil { w.WriteHeader(StatusBadRequest, "Bad request") return w.Flush()