Goroutine leak hunt (#874)

* close the writers of gin loggers + kill the tomb of httpServer

* body close defer
This commit is contained in:
Thibault "bui" Koechlin 2021-07-30 11:41:17 +02:00 committed by GitHub
parent cedfca07c2
commit 01028d0a09
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 7 deletions

View file

@ -119,7 +119,6 @@ func (e *ErrorResponse) Error() string {
func newResponse(r *http.Response) *Response {
response := &Response{Response: r}
//response.populatePageValues()
return response
}

View file

@ -57,7 +57,9 @@ func (c *ApiClient) Do(ctx context.Context, req *http.Request, v interface{}) (*
}
resp, err := c.client.Do(req)
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
if err != nil {
// If we got an error, and the context has been canceled,
// the context's error is probably more useful.

View file

@ -3,6 +3,7 @@ package apiserver
import (
"context"
"fmt"
"io"
"net"
"net/http"
"os"
@ -252,11 +253,6 @@ func (s *APIServer) Run() error {
}
}()
<-s.httpServerTomb.Dying()
log.Infof("run: shutting down api server")
if err := s.Shutdown(); err != nil {
log.Errorf("while shutting down API Server : %s", err)
return err
}
return nil
})
@ -278,5 +274,17 @@ func (s *APIServer) Shutdown() error {
if err := s.httpServer.Shutdown(context.TODO()); err != nil {
return err
}
//close io.writer logger given to gin
if pipe, ok := gin.DefaultErrorWriter.(*io.PipeWriter); ok {
pipe.Close()
}
if pipe, ok := gin.DefaultWriter.(*io.PipeWriter); ok {
pipe.Close()
}
s.httpServerTomb.Kill(nil)
if err := s.httpServerTomb.Wait(); err != nil {
return errors.Wrap(err, "while waiting on httpServerTomb")
}
return nil
}