diff --git a/codec/benchmark/benchmark.go b/codec/benchmark/benchmark.go index ebf0737b599..8bdffda5181 100644 --- a/codec/benchmark/benchmark.go +++ b/codec/benchmark/benchmark.go @@ -32,6 +32,8 @@ import ( "sort" "time" + "github.com/golang/glog" + "github.com/dgraph-io/dgraph/codec" "github.com/dgraph-io/dgraph/protos/pb" "github.com/dgraph-io/dgraph/x" @@ -62,7 +64,11 @@ func read(filename string) []int { if err != nil { x.Panic(err) } - defer f.Close() + defer func() { + if err := f.Close(); err != nil { + glog.Warningf("error while closing fd: %v", err) + } + }() fgzip, err := gzip.NewReader(f) if err != nil { diff --git a/conn/pool.go b/conn/pool.go index d4e97d7c826..56d36e3028e 100644 --- a/conn/pool.go +++ b/conn/pool.go @@ -304,14 +304,18 @@ func (p *Pool) MonitorHealth() { cancel() if err == nil { p.Lock() - p.conn.Close() + if err := p.conn.Close(); err != nil { + glog.Warningf("error while closing connection: %v", err) + } p.conn = conn p.Unlock() return } glog.Errorf("CONN: Unable to connect with %s : %s\n", p.Addr, err) if conn != nil { - conn.Close() + if err := conn.Close(); err != nil { + glog.Warningf("error while closing connection: %v", err) + } } } } diff --git a/dgraph/cmd/bulk/loader.go b/dgraph/cmd/bulk/loader.go index 5baac1dcfd8..9400a1675ae 100644 --- a/dgraph/cmd/bulk/loader.go +++ b/dgraph/cmd/bulk/loader.go @@ -32,6 +32,7 @@ import ( "sync" "time" + "github.com/golang/glog" "google.golang.org/grpc" "google.golang.org/grpc/credentials" "google.golang.org/grpc/credentials/insecure" @@ -196,7 +197,11 @@ func (ld *loader) leaseNamespaces() { func readSchema(opt *options) *schema.ParsedSchema { f, err := filestore.Open(opt.SchemaFile) x.Check(err) - defer f.Close() + defer func() { + if err := f.Close(); err != nil { + glog.Warningf("error while closing fd: %v", err) + } + }() key := opt.EncryptionKey if !opt.Encrypted { @@ -334,7 +339,11 @@ func (ld *loader) processGqlSchema(loadType chunker.InputFormat) { f, err := filestore.Open(ld.opt.GqlSchemaFile) x.Check(err) - defer f.Close() + defer func() { + if err := f.Close(); err != nil { + glog.Warningf("error while closing fd: %v", err) + } + }() key := ld.opt.EncryptionKey if !ld.opt.Encrypted { diff --git a/dgraph/cmd/cert/cert.go b/dgraph/cmd/cert/cert.go index 2f67e5bcc5f..d3f9b80c7c2 100644 --- a/dgraph/cmd/cert/cert.go +++ b/dgraph/cmd/cert/cert.go @@ -29,6 +29,7 @@ import ( "os" "time" + "github.com/golang/glog" "github.com/pkg/errors" ) @@ -128,7 +129,11 @@ func (c *certConfig) generatePair(keyFile, certFile string) error { } return err } - defer fp.Close() + defer func() { + if err := fp.Close(); err != nil { + glog.Warningf("error closing file: %v", err) + } + }() err = pem.Encode(fp, &pem.Block{ Type: "CERTIFICATE", diff --git a/dgraph/cmd/cert/create.go b/dgraph/cmd/cert/create.go index d5978d3af3a..0e2697dcb8b 100644 --- a/dgraph/cmd/cert/create.go +++ b/dgraph/cmd/cert/create.go @@ -28,6 +28,7 @@ import ( "os" "path/filepath" + "github.com/golang/glog" "github.com/pkg/errors" ) @@ -59,7 +60,11 @@ func makeKey(keyFile string, c *certConfig) (crypto.PrivateKey, error) { } return nil, err } - defer fp.Close() + defer func() { + if err := fp.Close(); err != nil { + glog.Warningf("error closing file: %v", err) + } + }() var key crypto.PrivateKey switch c.curve { diff --git a/dgraph/cmd/conv/conv.go b/dgraph/cmd/conv/conv.go index cfee234810a..629edbcb140 100644 --- a/dgraph/cmd/conv/conv.go +++ b/dgraph/cmd/conv/conv.go @@ -26,6 +26,7 @@ import ( "path/filepath" "strings" + "github.com/golang/glog" geojson "github.com/paulmach/go.geojson" "github.com/dgraph-io/dgraph/x" @@ -38,7 +39,11 @@ func writeToFile(fpath string, ch chan []byte) error { return err } - defer f.Close() + defer func() { + if err := f.Close(); err != nil { + glog.Warningf("error while closing fd: %v", err) + } + }() x.Check(err) w := bufio.NewWriterSize(f, 1e6) gw, err := gzip.NewWriterLevel(w, gzip.BestCompression) @@ -66,7 +71,11 @@ func convertGeoFile(input string, output string) error { if err != nil { return err } - defer f.Close() + defer func() { + if err := f.Close(); err != nil { + glog.Warningf("error while closing fd: %v", err) + } + }() var gz io.Reader if filepath.Ext(input) == ".gz" { diff --git a/dgraph/cmd/debug/run.go b/dgraph/cmd/debug/run.go index cd3996dd80e..36d16bd8609 100644 --- a/dgraph/cmd/debug/run.go +++ b/dgraph/cmd/debug/run.go @@ -667,7 +667,7 @@ func printKeys(db *badger.DB) { return err } x.Check(stream.Orchestrate(context.Background())) - w.Flush() + x.Check(w.Flush()) fmt.Println() fmt.Printf("Found %d keys\n", atomic.LoadUint64(&total)) } diff --git a/dgraph/cmd/debuginfo/archive.go b/dgraph/cmd/debuginfo/archive.go index 371d77e87b3..b0a845ed890 100644 --- a/dgraph/cmd/debuginfo/archive.go +++ b/dgraph/cmd/debuginfo/archive.go @@ -57,7 +57,11 @@ func (w *walker) walkPath(path string, info os.FileInfo, err error) error { glog.Errorf("Failed to open %s: %s", path, err) return nil } - defer file.Close() + defer func() { + if err := file.Close(); err != nil { + glog.Warningf("error closing file: %v", err) + } + }() if info.IsDir() { if info.Name() == w.baseDir { @@ -96,10 +100,18 @@ func createArchive(debugDir string) (string, error) { if err != nil { return "", err } - defer file.Close() + defer func() { + if err := file.Close(); err != nil { + glog.Warningf("error closing file: %v", err) + } + }() writer := tar.NewWriter(file) - defer writer.Close() + defer func() { + if err := writer.Close(); err != nil { + glog.Warningf("error closing writer: %v", err) + } + }() var baseDir string if info, err := os.Stat(debugDir); os.IsNotExist(err) { @@ -134,11 +146,19 @@ func createGzipArchive(debugDir string) (string, error) { if err != nil { return "", err } - defer writer.Close() + defer func() { + if err := writer.Close(); err != nil { + glog.Warningf("error closing writer: %v", err) + } + }() archiver := gzip.NewWriter(writer) archiver.Name = filename - defer archiver.Close() + defer func() { + if err := archiver.Close(); err != nil { + glog.Warningf("error closing archiver: %v", err) + } + }() _, err = io.Copy(archiver, reader) if err != nil { diff --git a/dgraph/cmd/debuginfo/debugging.go b/dgraph/cmd/debuginfo/debugging.go index e090e231fa8..07fd58fe61e 100644 --- a/dgraph/cmd/debuginfo/debugging.go +++ b/dgraph/cmd/debuginfo/debugging.go @@ -74,8 +74,12 @@ func saveDebug(sourceURL, filePath string, duration time.Duration) error { if err != nil { return err } + defer func() { + if err := resp.Close(); err != nil { + glog.Warningf("error closing resp reader: %v", err) + } + }() - defer resp.Close() out, err := os.Create(filePath) if err != nil { return fmt.Errorf("error while creating debug file: %s", err) @@ -94,7 +98,11 @@ func fetchURL(source string, timeout time.Duration) (io.ReadCloser, error) { return nil, fmt.Errorf("http fetch: %v", err) } if resp.StatusCode != http.StatusOK { - defer resp.Body.Close() + defer func() { + if err := resp.Body.Close(); err != nil { + glog.Warningf("error closing body: %v", err) + } + }() return nil, statusCodeError(resp) } diff --git a/dgraph/cmd/decrypt/decrypt.go b/dgraph/cmd/decrypt/decrypt.go index 375c9dceffd..f3202beed3e 100644 --- a/dgraph/cmd/decrypt/decrypt.go +++ b/dgraph/cmd/decrypt/decrypt.go @@ -73,7 +73,11 @@ func run() { if err != nil { glog.Fatalf("Error opening file: %v\n", err) } - defer f.Close() + defer func() { + if err := f.Close(); err != nil { + glog.Warningf("error while closing fd: %v", err) + } + }() reader, err := enc.GetReader(opts.keyfile, f) x.Checkf(err, "could not open key reader") if strings.HasSuffix(strings.ToLower(opts.file), ".gz") { diff --git a/dgraph/cmd/live/run.go b/dgraph/cmd/live/run.go index 51314b67264..aace7c19530 100644 --- a/dgraph/cmd/live/run.go +++ b/dgraph/cmd/live/run.go @@ -241,7 +241,11 @@ func (l *loader) processSchemaFile(ctx context.Context, file string, key x.Sensi f, err := filestore.Open(file) x.CheckfNoTrace(err) - defer f.Close() + defer func() { + if err := f.Close(); err != nil { + glog.Warningf("error while closing fd: %v", err) + } + }() reader, err := enc.GetReader(key, f) x.Check(err) diff --git a/dgraph/cmd/zero/oracle.go b/dgraph/cmd/zero/oracle.go index 7e31ccd9075..4a66c12bbda 100644 --- a/dgraph/cmd/zero/oracle.go +++ b/dgraph/cmd/zero/oracle.go @@ -63,7 +63,9 @@ func (o *Oracle) Init() { // close releases the memory associated with btree used for keycommit. func (o *Oracle) close() { - o.keyCommit.Close() + if err := o.keyCommit.Close(); err != nil { + glog.Warningf("error while closing tree: %v", err) + } } func (o *Oracle) updateStartTxnTs(ts uint64) { diff --git a/ee/audit/run_ee.go b/ee/audit/run_ee.go index f188a6a7410..cb99f438a6f 100644 --- a/ee/audit/run_ee.go +++ b/ee/audit/run_ee.go @@ -84,13 +84,20 @@ func run() error { file, err := os.Open(decryptCmd.Conf.GetString("in")) x.Check(err) - defer file.Close() + defer func() { + if err := file.Close(); err != nil { + glog.Warningf("error closing file: %v", err) + } + }() outfile, err := os.OpenFile(decryptCmd.Conf.GetString("out"), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm) x.Check(err) - defer outfile.Close() - + defer func() { + if err := outfile.Close(); err != nil { + glog.Warningf("error closing file: %v", err) + } + }() block, err := aes.NewCipher(key) x.Check(err) diff --git a/ee/backup/run.go b/ee/backup/run.go index 646fc1cfb7e..68cf9ff9af6 100644 --- a/ee/backup/run.go +++ b/ee/backup/run.go @@ -299,7 +299,7 @@ func runLsbackupCmd() error { if err != nil { fmt.Println("error:", err) } - os.Stdout.Write(b) + _, _ = os.Stdout.Write(b) fmt.Println() return nil } diff --git a/go.mod b/go.mod index fb8cafe8ae1..1bd97b91fed 100644 --- a/go.mod +++ b/go.mod @@ -110,7 +110,6 @@ require ( github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/opencontainers/go-digest v1.0.0-rc1 // indirect - github.com/opentracing/opentracing-go v1.1.0 // indirect github.com/pelletier/go-toml v1.2.0 // indirect github.com/philhofer/fwd v1.0.0 // indirect github.com/pierrec/lz4 v2.6.0+incompatible // indirect diff --git a/go.sum b/go.sum index 15e0ee1402d..99326cc707b 100644 --- a/go.sum +++ b/go.sum @@ -147,8 +147,6 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgraph-io/badger v1.6.0/go.mod h1:zwt7syl517jmP8s94KqSxTlM6IMsdhYy6psNgSztDR4= -github.com/dgraph-io/badger/v3 v3.2103.6-0.20230206174300-f35dd0f0c98c h1:r/szX2ayiqpRXyvfwUDBKAAtkDAY9pgMka8fLb5i1gw= -github.com/dgraph-io/badger/v3 v3.2103.6-0.20230206174300-f35dd0f0c98c/go.mod h1:4MPiseMeDQ3FNCYwRbbcBOGJLf5jsE0PPFzRiKjtcdw= github.com/dgraph-io/badger/v3 v3.2103.6-0.20230209075919-3045f88d615c h1:CxDBAdDBThd0cOT9xboEzbKQ0s5b3RxQnXSL0/O8BqQ= github.com/dgraph-io/badger/v3 v3.2103.6-0.20230209075919-3045f88d615c/go.mod h1:4MPiseMeDQ3FNCYwRbbcBOGJLf5jsE0PPFzRiKjtcdw= github.com/dgraph-io/dgo/v210 v210.0.0-20210407152819-261d1c2a6987 h1:5aN6H88a2q3HkO8vSZxDlgjEpJf4Fz8rfy+/Wzx2uAc= @@ -527,7 +525,6 @@ github.com/opencontainers/runc v0.1.1/go.mod h1:qT5XzbpPznkRYVz/mWwUaVBUv2rmF59P github.com/opentracing/basictracer-go v1.0.0/go.mod h1:QfBfYuafItcjQuMwinw9GhYKwFXS9KnPs5lxoYwgW74= github.com/opentracing/basictracer-go v1.1.0/go.mod h1:V2HZueSJEp879yv285Aap1BS69fQMD+MNP1mRs6mBQc= github.com/opentracing/opentracing-go v1.0.2/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= -github.com/opentracing/opentracing-go v1.1.0 h1:pWlfV3Bxv7k65HYwkikxat0+s3pV4bsqf19k25Ur8rU= github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= github.com/openzipkin/zipkin-go v0.1.6/go.mod h1:QgAqvLzwWbR/WpD4A3cGpPtJrZXNIiJc5AZX7/PBEpw= github.com/ory/dockertest v3.3.4+incompatible/go.mod h1:1vX4m9wsvi00u5bseYwXaSnhNrne+V0E6LAcBILJdPs= diff --git a/graphql/authorization/auth.go b/graphql/authorization/auth.go index 224c401b60c..2c6f0b82471 100644 --- a/graphql/authorization/auth.go +++ b/graphql/authorization/auth.go @@ -30,6 +30,7 @@ import ( "time" "github.com/dgrijalva/jwt-go/v4" + "github.com/golang/glog" "github.com/pkg/errors" "google.golang.org/grpc/metadata" "gopkg.in/square/go-jose.v2" @@ -461,7 +462,11 @@ func (a *AuthMeta) FetchJWK(i int) error { if err != nil { return err } - defer resp.Body.Close() + defer func() { + if err := resp.Body.Close(); err != nil { + glog.Warningf("error closing body: %v", err) + } + }() data, err := io.ReadAll(resp.Body) if err != nil { diff --git a/graphql/e2e/common/admin.go b/graphql/e2e/common/admin.go index 2548a1fd714..c378da9e763 100644 --- a/graphql/e2e/common/admin.go +++ b/graphql/e2e/common/admin.go @@ -26,6 +26,7 @@ import ( "testing" "github.com/gogo/protobuf/jsonpb" + "github.com/golang/glog" "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/pkg/errors" @@ -303,7 +304,11 @@ func health(t *testing.T) { var health []pb.HealthInfo resp, err := http.Get(dgraphHealthURL) require.NoError(t, err) - defer resp.Body.Close() + defer func() { + if err := resp.Body.Close(); err != nil { + glog.Warningf("error closing body: %v", err) + } + }() healthRes, err := io.ReadAll(resp.Body) require.NoError(t, err) require.NoError(t, json.Unmarshal(healthRes, &health)) @@ -474,7 +479,11 @@ func adminState(t *testing.T) { var state pb.MembershipState resp, err := http.Get(dgraphStateURL) require.NoError(t, err) - defer resp.Body.Close() + defer func() { + if err := resp.Body.Close(); err != nil { + glog.Warningf("error closing body: %v", err) + } + }() stateRes, err := io.ReadAll(resp.Body) require.NoError(t, err) require.NoError(t, jsonpb.Unmarshal(bytes.NewReader(stateRes), &state)) diff --git a/graphql/e2e/common/common.go b/graphql/e2e/common/common.go index c1b3b0e64e0..387d86548df 100644 --- a/graphql/e2e/common/common.go +++ b/graphql/e2e/common/common.go @@ -1163,7 +1163,11 @@ func RunGQLRequest(req *http.Request) ([]byte, error) { return nil, errors.Errorf("cors headers weren't set in response") } - defer resp.Body.Close() + defer func() { + if err := resp.Body.Close(); err != nil { + glog.Warningf("error closing body: %v", err) + } + }() body, err := io.ReadAll(resp.Body) if err != nil { return nil, errors.Errorf("unable to read response body: %v", err) diff --git a/graphql/e2e/common/lambda.go b/graphql/e2e/common/lambda.go index 9e144167061..17690dcc809 100644 --- a/graphql/e2e/common/lambda.go +++ b/graphql/e2e/common/lambda.go @@ -26,6 +26,7 @@ import ( "testing" "time" + "github.com/golang/glog" "github.com/stretchr/testify/require" "github.com/dgraph-io/dgraph/testutil" @@ -373,11 +374,21 @@ func lambdaOnMutateHooks(t *testing.T) { // let's listen to the changes coming in from the lambda hook and store them in this array var changelog []string server := http.Server{Addr: lambdaHookServerAddr, Handler: http.NewServeMux()} - defer server.Shutdown(context.Background()) + defer func() { + if err := server.Shutdown(context.Background()); err != nil { + glog.Warningf("error while shutting down server: %v", err) + } + }() + go func() { serverMux := server.Handler.(*http.ServeMux) serverMux.HandleFunc("/changelog", func(w http.ResponseWriter, r *http.Request) { - defer r.Body.Close() + defer func() { + if err := r.Body.Close(); err != nil { + glog.Warningf("error closing body: %v", err) + } + }() + b, err := io.ReadAll(r.Body) require.NoError(t, err) diff --git a/graphql/e2e/common/subscription.go b/graphql/e2e/common/subscription.go index 81ab6f3ae1a..b4a49d97477 100644 --- a/graphql/e2e/common/subscription.go +++ b/graphql/e2e/common/subscription.go @@ -23,6 +23,7 @@ import ( "math/rand" "net/http" + "github.com/golang/glog" "github.com/gorilla/websocket" "github.com/dgraph-io/dgraph/graphql/schema" @@ -72,6 +73,12 @@ func NewGraphQLSubscription(url string, req *schema.Request, subscriptionPayload if err != nil { return nil, err } + defer func() { + if err := conn.Close(); err != nil { + glog.Warningf("error while closing connection: %v", err) + } + }() + // Initialize subscription. init := operationMessage{ Type: initMsg, @@ -85,7 +92,6 @@ func NewGraphQLSubscription(url string, req *schema.Request, subscriptionPayload msg := operationMessage{} if err = conn.ReadJSON(&msg); err != nil { - conn.Close() return nil, err } @@ -97,7 +103,6 @@ func NewGraphQLSubscription(url string, req *schema.Request, subscriptionPayload // We got ack, now send start the subscription by sending the query to the server. payload, err := json.Marshal(req) if err != nil { - conn.Close() return nil, err } @@ -108,7 +113,6 @@ func NewGraphQLSubscription(url string, req *schema.Request, subscriptionPayload msg.Payload = payload if err = conn.WriteJSON(msg); err != nil { - conn.Close() return nil, err } return &GraphQLSubscriptionClient{ diff --git a/graphql/schema/custom_http.go b/graphql/schema/custom_http.go index e8ade519161..1545a35875e 100644 --- a/graphql/schema/custom_http.go +++ b/graphql/schema/custom_http.go @@ -27,6 +27,7 @@ import ( "github.com/dgraph-io/dgraph/graphql/authorization" "github.com/dgraph-io/dgraph/x" + "github.com/golang/glog" ) var ( @@ -89,7 +90,11 @@ func (fconf *FieldHTTPConfig) MakeAndDecodeHTTPRequest(client *http.Client, url return nil, nil, x.GqlErrorList{externalRequestError(err, field)} } - defer resp.Body.Close() + defer func() { + if err := resp.Body.Close(); err != nil { + glog.Warningf("error closing body: %v", err) + } + }() b, err = io.ReadAll(resp.Body) if err != nil { return nil, nil, x.GqlErrorList{externalRequestError(err, field)} diff --git a/graphql/schema/remote.go b/graphql/schema/remote.go index 54839685ca8..ba053e81c10 100644 --- a/graphql/schema/remote.go +++ b/graphql/schema/remote.go @@ -25,6 +25,7 @@ import ( "net/url" "time" + "github.com/golang/glog" "github.com/pkg/errors" "github.com/dgraph-io/gqlparser/v2/ast" @@ -73,7 +74,11 @@ func introspectRemoteSchema(url string, headers http.Header) (*introspectedSchem if err != nil { return nil, err } - defer resp.Body.Close() + defer func() { + if err := resp.Body.Close(); err != nil { + glog.Warningf("error closing body: %v", err) + } + }() body, err = io.ReadAll(resp.Body) if err != nil { return nil, err diff --git a/graphql/testdata/custom_bench/dgraph_api_server.go b/graphql/testdata/custom_bench/dgraph_api_server.go index 7fc30154540..28e9e3ed630 100644 --- a/graphql/testdata/custom_bench/dgraph_api_server.go +++ b/graphql/testdata/custom_bench/dgraph_api_server.go @@ -24,6 +24,8 @@ import ( "log" "net/http" "strings" + + "github.com/golang/glog" ) const ( @@ -198,7 +200,11 @@ func makeGqlRequest(query string) (*Response, error) { return nil, err } - defer resp.Body.Close() + defer func() { + if err := resp.Body.Close(); err != nil { + glog.Warningf("error closing body: %v", err) + } + }() b, err = io.ReadAll(resp.Body) if err != nil { return nil, err diff --git a/graphql/testdata/custom_bench/profiling/graphql_profiler.go b/graphql/testdata/custom_bench/profiling/graphql_profiler.go index ff10cb14a32..a479f6fe74c 100644 --- a/graphql/testdata/custom_bench/profiling/graphql_profiler.go +++ b/graphql/testdata/custom_bench/profiling/graphql_profiler.go @@ -32,6 +32,8 @@ import ( "sync/atomic" "syscall" "time" + + "github.com/golang/glog" ) type PprofProfile string @@ -473,7 +475,11 @@ func applySchema(alphaAuthority string, schemaFilePath string) error { if err != nil { return err } - defer f.Close() + defer func() { + if err := f.Close(); err != nil { + glog.Warningf("error while closing fd: %v", err) + } + }() resp, err := http.Post(alphaAuthority+"/admin/schema", "", f) if err != nil { @@ -564,7 +570,11 @@ func saveProfile(profType PprofProfile, profilePath string, profileOpts *Profili log.Println("could not create file: ", profilePath, ", err: ", err) return } - defer f.Close() + defer func() { + if err := f.Close(); err != nil { + glog.Warningf("error while closing fd: %v", err) + } + }() _, err = f.Write(b) if err != nil { @@ -585,7 +595,11 @@ func saveTracing(resp *Response, outputDir string, iteration int) (int64, int64, log.Println(err) return 0, 0, err } - defer f.Close() + defer func() { + if err := f.Close(); err != nil { + glog.Warningf("error while closing fd: %v", err) + } + }() b, err := json.Marshal(resp) if err != nil { @@ -804,7 +818,11 @@ func makeGqlRequest(query string) (*Response, int64, int64, error) { totalProcessingTime, _ := strconv.Atoi(resp.Header.Get("Graphql-Time")) - defer resp.Body.Close() + defer func() { + if err := resp.Body.Close(); err != nil { + glog.Warningf("error closing body: %v", err) + } + }() b, err = io.ReadAll(resp.Body) if err != nil { return nil, rtt, int64(totalProcessingTime), err diff --git a/graphql/testdata/datagen/cmd/run.go b/graphql/testdata/datagen/cmd/run.go index 04d6ea2bf66..58ff0c3589f 100644 --- a/graphql/testdata/datagen/cmd/run.go +++ b/graphql/testdata/datagen/cmd/run.go @@ -15,6 +15,7 @@ import ( "strings" "time" + "github.com/golang/glog" "github.com/spf13/viper" ) @@ -354,7 +355,11 @@ func makeGqlReq(query string, vars interface{}) (*gqlResp, error) { return nil, err } - defer resp.Body.Close() + defer func() { + if err := resp.Body.Close(); err != nil { + glog.Warningf("error closing body: %v", err) + } + }() b, err = io.ReadAll(resp.Body) if err != nil { return nil, err diff --git a/t/t.go b/t/t.go index 1d6e84cff53..8984c60dbbc 100644 --- a/t/t.go +++ b/t/t.go @@ -39,6 +39,7 @@ import ( "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/filters" "github.com/docker/docker/client" + "github.com/golang/glog" "github.com/spf13/pflag" "golang.org/x/tools/go/packages" @@ -169,6 +170,11 @@ func detectRace(prefix string) bool { func outputLogs(prefix string) { f, err := os.CreateTemp(".", prefix+"*.log") x.Check(err) + defer func() { + if err := f.Close(); err != nil { + fmt.Printf("error closing file: %v", err) + } + }() printLogs := func(container string) { in := testutil.GetContainerInstance(prefix, container) c := in.GetContainer() @@ -178,7 +184,9 @@ func outputLogs(prefix string) { logCmd := exec.Command("docker", "logs", c.ID) out, err := logCmd.CombinedOutput() x.Check(err) - f.Write(out) + if _, err := f.Write(out); err != nil { + fmt.Printf("error writing container logs to file: %v", err) + } fmt.Printf("Docker logs for %s is %s with error %+v ", c.ID, string(out), err) } for i := 0; i <= 3; i++ { @@ -188,8 +196,6 @@ func outputLogs(prefix string) { for i := 0; i <= 6; i++ { printLogs("alpha" + strconv.Itoa(i)) } - f.Sync() - f.Close() s := fmt.Sprintf("---> LOGS for %s written to %s .\n", prefix, f.Name()) _, err = oc.Write([]byte(s)) x.Check(err) @@ -234,7 +240,7 @@ func stopCluster(composeFile, prefix string, wg *sync.WaitGroup, err error) { ) } - os.Remove(tmp) + _ = os.Remove(tmp) coverageBulk := strings.Replace(composeFile, "docker-compose.yml", "coverage_bulk.out", -1) if err = appendTestCoverageFile(coverageBulk, coverageFile); err != nil { @@ -322,7 +328,7 @@ func hasTestFiles(pkg string) bool { dir = filepath.Join(*baseDir, dir) hasTests := false - filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { + err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { if hasTests { return filepath.SkipDir } @@ -332,6 +338,7 @@ func hasTestFiles(pkg string) bool { } return nil }) + x.Check(err) return hasTests } @@ -830,7 +837,6 @@ func downloadLDBCFiles() { } wg.Wait() fmt.Printf("Downloaded %d files in %s \n", len(ldbcDataFiles), time.Since(start)) - } func createTestCoverageFile(path string) error { @@ -838,7 +844,11 @@ func createTestCoverageFile(path string) error { if err != nil { return err } - defer outFile.Close() + defer func() { + if err := outFile.Close(); err != nil { + glog.Warningf("error closing file: %v", err) + } + }() cmd := command("echo", coverageFileHeader) cmd.Stdout = outFile @@ -864,7 +874,11 @@ func isTestCoverageEmpty(path string) (bool, error) { if err != nil { return true, err } - defer file.Close() + defer func() { + if err := file.Close(); err != nil { + glog.Warningf("error closing file: %v", err) + } + }() var l int scanner := bufio.NewScanner(file) diff --git a/telemetry/telemetry.go b/telemetry/telemetry.go index d52a1cb442e..13aeb54faa2 100644 --- a/telemetry/telemetry.go +++ b/telemetry/telemetry.go @@ -117,7 +117,11 @@ func (t *Telemetry) Post() error { return err } - defer resp.Body.Close() + defer func() { + if err := resp.Body.Close(); err != nil { + glog.Warningf("error closing body: %v", err) + } + }() body, err := io.ReadAll(resp.Body) if err != nil { return err diff --git a/testutil/bulk.go b/testutil/bulk.go index de5c9d809ad..fadc6a5441a 100644 --- a/testutil/bulk.go +++ b/testutil/bulk.go @@ -25,6 +25,7 @@ import ( "os/exec" "strconv" + "github.com/golang/glog" "github.com/pkg/errors" "github.com/dgraph-io/dgraph/x" @@ -158,7 +159,9 @@ func freePort(port int) int { p := port + offset listener, err := net.Listen("tcp", fmt.Sprintf(":%d", p)) if err == nil { - listener.Close() + if err := listener.Close(); err != nil { + glog.Warningf("error closing listener: %v", err) + } return offset } } diff --git a/testutil/client.go b/testutil/client.go index 7a5deeed20c..9172614fa54 100644 --- a/testutil/client.go +++ b/testutil/client.go @@ -32,6 +32,7 @@ import ( "testing" "time" + "github.com/golang/glog" "github.com/pkg/errors" "github.com/spf13/viper" "github.com/stretchr/testify/require" @@ -340,7 +341,11 @@ func HttpLogin(params *LoginParams) (*HttpToken, error) { if err != nil { return nil, errors.Wrapf(err, "login through curl failed") } - defer resp.Body.Close() + defer func() { + if err := resp.Body.Close(); err != nil { + glog.Warningf("error closing body: %v", err) + } + }() respBody, err := io.ReadAll(resp.Body) if err != nil { diff --git a/testutil/docker.go b/testutil/docker.go index 48379f89387..2bb8bcdb861 100644 --- a/testutil/docker.go +++ b/testutil/docker.go @@ -72,7 +72,7 @@ func (in ContainerInstance) BestEffortWaitForHealthy(privatePort uint16) error { var body []byte if resp != nil && resp.Body != nil { body, _ = io.ReadAll(resp.Body) - resp.Body.Close() + _ = resp.Body.Close() } if err == nil && resp.StatusCode == http.StatusOK { return checkACL(body) @@ -245,7 +245,8 @@ func DockerCpFromContainer(containerID, srcPath, dstPath string) error { return nil } tr := tar.NewReader(tarStream) - tr.Next() + _, err = tr.Next() + x.Check(err) data, err := io.ReadAll(tr) x.Check(err) @@ -287,7 +288,7 @@ func CheckHealthContainer(socketAddrHttp string) error { if err != nil { return err } - resp.Body.Close() + _ = resp.Body.Close() } fmt.Printf("health check for container failed: %v. Response: %q. Retrying...\n", err, body) time.Sleep(time.Second) diff --git a/testutil/graphql.go b/testutil/graphql.go index cfb3c8edf08..1ee33d2b331 100644 --- a/testutil/graphql.go +++ b/testutil/graphql.go @@ -29,6 +29,7 @@ import ( "time" "github.com/dgrijalva/jwt-go/v4" + "github.com/golang/glog" "github.com/pkg/errors" "github.com/stretchr/testify/require" "google.golang.org/grpc/metadata" @@ -70,7 +71,12 @@ func (resp *GraphQLResponse) RequireNoGraphQLErrors(t *testing.T) { } func RequireNoGraphQLErrors(t *testing.T, resp *http.Response) { - defer resp.Body.Close() + defer func() { + if err := resp.Body.Close(); err != nil { + glog.Warningf("error closing body: %v", err) + } + }() + b, err := io.ReadAll(resp.Body) require.NoError(t, err) @@ -120,7 +126,12 @@ func MakeGQLRequestWithAccessJwtAndTLS(t *testing.T, params *GraphQLParams, resp, err := client.Do(req) require.NoError(t, err) - defer resp.Body.Close() + defer func() { + if err := resp.Body.Close(); err != nil { + glog.Warningf("error closing body: %v", err) + } + }() + b, err = io.ReadAll(resp.Body) require.NoError(t, err) diff --git a/testutil/schema.go b/testutil/schema.go index e37ba19bf34..870d79569e6 100644 --- a/testutil/schema.go +++ b/testutil/schema.go @@ -24,6 +24,7 @@ import ( "net/http" "testing" + "github.com/golang/glog" "github.com/stretchr/testify/require" "github.com/dgraph-io/dgo/v210" @@ -157,7 +158,11 @@ func UpdateGQLSchema(t *testing.T, sockAddrHttp, schema string) { require.NoError(t, err) resp, err := http.Post(adminUrl, "application/json", bytes.NewBuffer(b)) require.NoError(t, err) - defer resp.Body.Close() + defer func() { + if err := resp.Body.Close(); err != nil { + glog.Warningf("error closing body: %v", err) + } + }() } func GetGQLSchema(t *testing.T, sockAddrHttp string) string { @@ -168,7 +173,11 @@ func GetGQLSchema(t *testing.T, sockAddrHttp string) string { require.NoError(t, err) resp, err := http.Post(adminUrl, "application/json", bytes.NewBuffer(b)) require.NoError(t, err) - defer resp.Body.Close() + defer func() { + if err := resp.Body.Close(); err != nil { + glog.Warningf("error closing body: %v", err) + } + }() var data interface{} require.NoError(t, json.NewDecoder(resp.Body).Decode(&data)) return JsonGet(data, "data", "getGQLSchema", "schema").(string) diff --git a/testutil/utils.go b/testutil/utils.go index 17a96579dde..fd45e1c5c45 100644 --- a/testutil/utils.go +++ b/testutil/utils.go @@ -25,6 +25,7 @@ import ( "time" "github.com/dgrijalva/jwt-go" + "github.com/golang/glog" "github.com/stretchr/testify/require" "github.com/dgraph-io/dgo/v210" @@ -112,7 +113,11 @@ func WaitForTask(t *testing.T, taskId string, useHttps bool, socketAddrHttp stri } response, err := client.Post(adminUrl, "application/json", bytes.NewBuffer(request)) require.NoError(t, err) - defer response.Body.Close() + defer func() { + if err := response.Body.Close(); err != nil { + glog.Warningf("error closing body: %v", err) + } + }() var data interface{} require.NoError(t, json.NewDecoder(response.Body).Decode(&data)) diff --git a/testutil/zero.go b/testutil/zero.go index 583c655f399..cb98c84fb6f 100644 --- a/testutil/zero.go +++ b/testutil/zero.go @@ -24,6 +24,7 @@ import ( "net/http" "strings" + "github.com/golang/glog" "github.com/pkg/errors" "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" @@ -66,7 +67,11 @@ func GetState() (*StateResponse, error) { if err != nil { return nil, err } - defer resp.Body.Close() + defer func() { + if err := resp.Body.Close(); err != nil { + glog.Warningf("error closing body: %v", err) + } + }() b, err := io.ReadAll(resp.Body) if err != nil { @@ -95,7 +100,11 @@ func GetStateHttps(tlsConfig *tls.Config) (*StateResponse, error) { if err != nil { return nil, err } - defer resp.Body.Close() + defer func() { + if err := resp.Body.Close(); err != nil { + glog.Warningf("error closing body: %v", err) + } + }() b, err := io.ReadAll(resp.Body) if err != nil { diff --git a/upgrade/utils.go b/upgrade/utils.go index b57e3173b81..a38b291adc9 100644 --- a/upgrade/utils.go +++ b/upgrade/utils.go @@ -26,6 +26,7 @@ import ( "strings" "time" + "github.com/golang/glog" "github.com/pkg/errors" "github.com/dgraph-io/dgo/v210" @@ -163,7 +164,11 @@ func runGQLRequest(req *http.Request) ([]byte, error) { return nil, errors.Errorf("cors headers weren't set in response") } - defer resp.Body.Close() + defer func() { + if err := resp.Body.Close(); err != nil { + glog.Warningf("error closing body: %v", err) + } + }() body, err := io.ReadAll(resp.Body) if err != nil { return nil, errors.Errorf("unable to read response body: %v", err) diff --git a/worker/backup.go b/worker/backup.go index 22c248b12db..66d890b1765 100644 --- a/worker/backup.go +++ b/worker/backup.go @@ -20,6 +20,7 @@ import ( "context" "math" + "github.com/golang/glog" "github.com/pkg/errors" "github.com/dgraph-io/badger/v3" @@ -110,14 +111,15 @@ func StoreExport(request *pb.ExportRequest, dir string, key x.Sensitive) error { WithValueThreshold(1 << 10). WithNumVersionsToKeep(math.MaxInt32). WithEncryptionKey(key)) - if err != nil { return err } + defer func() { + if err := db.Close(); err != nil { + glog.Warningf("error closing the DB: %v", err) + } + }() _, err = exportInternal(context.Background(), request, db, true) - // It is important to close the db before sending err to ch. Else, we will see a memory - // leak. - db.Close() return errors.Wrapf(err, "cannot export data inside DB at %s", dir) } diff --git a/worker/queue.go b/worker/queue.go index d662a068722..b4da16fe047 100644 --- a/worker/queue.go +++ b/worker/queue.go @@ -229,7 +229,9 @@ func (t *tasks) worker() { var task taskRequest select { case <-x.ServerCloser.HasBeenClosed(): - t.log.Close() + if err := t.log.Close(); err != nil { + glog.Warningf("error closing log file: %v", err) + } return case <-shouldCleanup.C: t.cleanup() diff --git a/worker/restore_map.go b/worker/restore_map.go index 23b416a4a43..958e9ecf348 100644 --- a/worker/restore_map.go +++ b/worker/restore_map.go @@ -177,7 +177,11 @@ func (m *mapper) writeToDisk(buf *z.Buffer) error { if err != nil { return errors.Wrap(err, "openOutputFile") } - defer f.Close() + defer func() { + if err := f.Close(); err != nil { + glog.Warningf("error while closing fd: %v", err) + } + }() // Create partition keys for the map file. header := &pb.MapHeader{PartitionKeys: [][]byte{}} @@ -680,7 +684,9 @@ func RunMapper(req *pb.RestoreRequest, mapDir string) (*mapResult, error) { } go mapper.Progress() defer func() { - mapper.Flush() + if err := mapper.Flush(); err != nil { + glog.Warningf("error calling flush during map: %v", err) + } mapper.closer.SignalAndWait() }() diff --git a/x/init.go b/x/init.go index d3b82951d50..acee238c1aa 100644 --- a/x/init.go +++ b/x/init.go @@ -148,7 +148,11 @@ func ExecutableChecksum() []byte { if err != nil { return nil } - defer execFile.Close() + defer func() { + if err := execFile.Close(); err != nil { + glog.Warningf("error closing file: %v", err) + } + }() h := sha256.New() if _, err := io.Copy(h, execFile); err != nil { diff --git a/x/log_writer.go b/x/log_writer.go index f852703ad06..933a55c4497 100644 --- a/x/log_writer.go +++ b/x/log_writer.go @@ -31,6 +31,8 @@ import ( "sync" "time" + "github.com/golang/glog" + "github.com/dgraph-io/badger/v3/y" "github.com/dgraph-io/ristretto/z" ) @@ -243,7 +245,7 @@ func (l *LogWriter) open() error { l.writer = bufio.NewWriterSize(l.file, bufferSize) if l.EncryptionKey != nil { - rand.Read(l.baseIv[:]) + _, _ = rand.Read(l.baseIv[:]) bytes, err := encrypt(l.EncryptionKey, l.baseIv, []byte(VerificationText)) if err != nil { return err @@ -310,17 +312,32 @@ func compress(src string) error { return err } - defer f.Close() + defer func() { + if err := f.Close(); err != nil { + glog.Warningf("error while closing fd: %v", err) + } + }() gzf, err := os.OpenFile(src+".gz", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, os.ModePerm) if err != nil { return err } + defer func() { + if err := gzf.Close(); err != nil { + glog.Warningf("error closing file: %v", err) + } + }() - defer gzf.Close() gz := gzip.NewWriter(gzf) - defer gz.Close() + defer func() { + if err := gz.Close(); err != nil { + glog.Warningf("error closing gzip writer: %v", err) + } + }() + if _, err := io.Copy(gz, f); err != nil { - os.Remove(src + ".gz") + if err := os.Remove(src + ".gz"); err != nil { + glog.Warningf("error deleting file [%v]: %v", src, err) + } return err } // close the descriptors because we need to delete the file diff --git a/x/x.go b/x/x.go index 2411fc0ec7e..b282be561d8 100644 --- a/x/x.go +++ b/x/x.go @@ -428,7 +428,12 @@ func Reply(w http.ResponseWriter, rep interface{}) { // ParseRequest parses the body of the given request. func ParseRequest(w http.ResponseWriter, r *http.Request, data interface{}) bool { - defer r.Body.Close() + defer func() { + if err := r.Body.Close(); err != nil { + glog.Warningf("error closing body: %v", err) + } + }() + decoder := json.NewDecoder(r.Body) if err := decoder.Decode(&data); err != nil { SetStatus(w, Error, fmt.Sprintf("While parsing request: %v", err)) diff --git a/xidmap/xidmap.go b/xidmap/xidmap.go index 1099587e9cf..5637bcd572e 100644 --- a/xidmap/xidmap.go +++ b/xidmap/xidmap.go @@ -357,7 +357,9 @@ func (m *XidMap) Flush() error { // memory and causing OOM sometimes. Making shards explicitly nil in this method fixes this. // TODO: find why xidmap is not getting GCed without below line. for _, shards := range m.shards { - shards.tree.Close() + if err := shards.tree.Close(); err != nil { + glog.Warningf("error closing shards tree: %v", err) + } } m.shards = nil if m.writer == nil {