X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/f1f74069850d8c5e987ef7d7fc246735ff94d58d..7af506a0e9712ca22096ebd56df8867a427dae96:/sdk/go/arvados/container_gateway.go diff --git a/sdk/go/arvados/container_gateway.go b/sdk/go/arvados/container_gateway.go index ce33fb3105..897ae434e1 100644 --- a/sdk/go/arvados/container_gateway.go +++ b/sdk/go/arvados/container_gateway.go @@ -15,6 +15,7 @@ import ( ) func (cresp ConnectionResponse) ServeHTTP(w http.ResponseWriter, req *http.Request) { + defer cresp.Conn.Close() hj, ok := w.(http.Hijacker) if !ok { http.Error(w, "ResponseWriter does not support connection upgrade", http.StatusInternalServerError) @@ -33,8 +34,8 @@ func (cresp ConnectionResponse) ServeHTTP(w http.ResponseWriter, req *http.Reque defer conn.Close() var bytesIn, bytesOut int64 - var wg sync.WaitGroup ctx, cancel := context.WithCancel(req.Context()) + var wg sync.WaitGroup wg.Add(1) go func() { defer wg.Done() @@ -63,11 +64,16 @@ func (cresp ConnectionResponse) ServeHTTP(w http.ResponseWriter, req *http.Reque ctxlog.FromContext(ctx).WithError(err).Error("error copying upstream") } }() - wg.Wait() - if cresp.Logger != nil { - cresp.Logger.WithFields(logrus.Fields{ - "bytesIn": bytesIn, - "bytesOut": bytesOut, - }).Info("closed connection") - } + <-ctx.Done() + go func() { + // Wait for both io.Copy goroutines to finish and increment + // their byte counters. + wg.Wait() + if cresp.Logger != nil { + cresp.Logger.WithFields(logrus.Fields{ + "bytesIn": bytesIn, + "bytesOut": bytesOut, + }).Info("closed connection") + } + }() }