Commit f2b7fbc3 by Arve Knudsen Committed by GitHub

Chore: Check errors from Close calls (#29562)

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
parent 3c9310e9
...@@ -15,7 +15,6 @@ import ( ...@@ -15,7 +15,6 @@ import (
"github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/components/dashdiffs" "github.com/grafana/grafana/pkg/components/dashdiffs"
"github.com/grafana/grafana/pkg/components/simplejson" "github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/metrics" "github.com/grafana/grafana/pkg/infra/metrics"
"github.com/grafana/grafana/pkg/plugins" "github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/services/guardian" "github.com/grafana/grafana/pkg/services/guardian"
...@@ -334,7 +333,7 @@ func (hs *HTTPServer) GetHomeDashboard(c *models.ReqContext) Response { ...@@ -334,7 +333,7 @@ func (hs *HTTPServer) GetHomeDashboard(c *models.ReqContext) Response {
dashRedirect := dtos.DashboardRedirect{RedirectUri: url} dashRedirect := dtos.DashboardRedirect{RedirectUri: url}
return JSON(200, &dashRedirect) return JSON(200, &dashRedirect)
} }
log.Warnf("Failed to get slug from database, %s", err.Error()) hs.log.Warn("Failed to get slug from database", "err", err)
} }
filePath := hs.Cfg.DefaultHomeDashboardPath filePath := hs.Cfg.DefaultHomeDashboardPath
...@@ -346,7 +345,11 @@ func (hs *HTTPServer) GetHomeDashboard(c *models.ReqContext) Response { ...@@ -346,7 +345,11 @@ func (hs *HTTPServer) GetHomeDashboard(c *models.ReqContext) Response {
if err != nil { if err != nil {
return Error(500, "Failed to load home dashboard", err) return Error(500, "Failed to load home dashboard", err)
} }
defer file.Close() defer func() {
if err := file.Close(); err != nil {
hs.log.Warn("Failed to close dashboard file", "path", filePath, "err", err)
}
}()
dash := dtos.DashboardFullWithMeta{} dash := dtos.DashboardFullWithMeta{}
dash.Meta.IsHome = true dash.Meta.IsHome = true
......
...@@ -116,7 +116,11 @@ func executeServer(configFile, homePath, pidFile, packaging string, traceDiagnos ...@@ -116,7 +116,11 @@ func executeServer(configFile, homePath, pidFile, packaging string, traceDiagnos
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer f.Close() defer func() {
if err := f.Close(); err != nil {
log.Error("Failed to write trace diagnostics", "path", traceDiagnostics.file, "err", err)
}
}()
if err := trace.Start(f); err != nil { if err := trace.Start(f); err != nil {
panic(err) panic(err)
......
...@@ -49,7 +49,11 @@ func (az *AzureBlobUploader) Upload(ctx context.Context, imageDiskPath string) ( ...@@ -49,7 +49,11 @@ func (az *AzureBlobUploader) Upload(ctx context.Context, imageDiskPath string) (
if err != nil { if err != nil {
return "", err return "", err
} }
defer file.Close() defer func() {
if err := file.Close(); err != nil {
az.log.Warn("Failed to close file", "path", imageDiskPath, "err", err)
}
}()
randomFileName, err := util.GetRandomString(30) randomFileName, err := util.GetRandomString(30)
if err != nil { if err != nil {
......
...@@ -153,7 +153,11 @@ func (u *Uploader) uploadFile( ...@@ -153,7 +153,11 @@ func (u *Uploader) uploadFile(
if err != nil { if err != nil {
return err return err
} }
defer fileReader.Close() defer func() {
if err := fileReader.Close(); err != nil {
u.log.Warn("Failed to close file", "err", err, "path", imageDiskPath)
}
}()
// Set public access if not generating a signed URL // Set public access if not generating a signed URL
pubAcc := !u.enableSignedURLs pubAcc := !u.enableSignedURLs
......
...@@ -80,12 +80,17 @@ func (u *S3Uploader) Upload(ctx context.Context, imageDiskPath string) (string, ...@@ -80,12 +80,17 @@ func (u *S3Uploader) Upload(ctx context.Context, imageDiskPath string) (string,
if err != nil { if err != nil {
return "", err return "", err
} }
defer file.Close() defer func() {
if err := file.Close(); err != nil {
u.log.Warn("Failed to close file", "path", imageDiskPath, "err", err)
}
}()
sess, err = session.NewSession(cfg) sess, err = session.NewSession(cfg)
if err != nil { if err != nil {
return "", err return "", err
} }
uploader := s3manager.NewUploader(sess) uploader := s3manager.NewUploader(sess)
result, err := uploader.UploadWithContext(ctx, &s3manager.UploadInput{ result, err := uploader.UploadWithContext(ctx, &s3manager.UploadInput{
Bucket: aws.String(u.bucket), Bucket: aws.String(u.bucket),
......
...@@ -80,6 +80,10 @@ func Warnf(format string, v ...interface{}) { ...@@ -80,6 +80,10 @@ func Warnf(format string, v ...interface{}) {
Root.Warn(message) Root.Warn(message)
} }
func Error(msg string, args ...interface{}) {
Root.Error(msg, args...)
}
func Errorf(skip int, format string, v ...interface{}) { func Errorf(skip int, format string, v ...interface{}) {
Root.Error(fmt.Sprintf(format, v...)) Root.Error(fmt.Sprintf(format, v...))
} }
......
...@@ -91,7 +91,6 @@ func GetPluginDashboards(orgId int64, pluginId string) ([]*PluginDashboardInfoDT ...@@ -91,7 +91,6 @@ func GetPluginDashboards(orgId int64, pluginId string) ([]*PluginDashboardInfoDT
func loadPluginDashboard(pluginId, path string) (*models.Dashboard, error) { func loadPluginDashboard(pluginId, path string) (*models.Dashboard, error) {
plugin, exists := Plugins[pluginId] plugin, exists := Plugins[pluginId]
if !exists { if !exists {
return nil, PluginNotFoundError{pluginId} return nil, PluginNotFoundError{pluginId}
} }
...@@ -102,7 +101,11 @@ func loadPluginDashboard(pluginId, path string) (*models.Dashboard, error) { ...@@ -102,7 +101,11 @@ func loadPluginDashboard(pluginId, path string) (*models.Dashboard, error) {
return nil, err return nil, err
} }
defer reader.Close() defer func() {
if err := reader.Close(); err != nil {
plog.Warn("Failed to close file", "path", dashboardFilePath, "err", err)
}
}()
data, err := simplejson.NewFromReader(reader) data, err := simplejson.NewFromReader(reader)
if err != nil { if err != nil {
......
...@@ -113,7 +113,11 @@ func getPluginSignatureState(log log.Logger, plugin *PluginBase) PluginSignature ...@@ -113,7 +113,11 @@ func getPluginSignatureState(log log.Logger, plugin *PluginBase) PluginSignature
if err != nil { if err != nil {
return PluginSignatureModified return PluginSignatureModified
} }
defer f.Close() defer func() {
if err := f.Close(); err != nil {
log.Warn("Failed to close plugin file", "path", fp, "err", err)
}
}()
h := sha256.New() h := sha256.New()
if _, err := io.Copy(h, f); err != nil { if _, err := io.Copy(h, f); err != nil {
......
...@@ -273,7 +273,11 @@ func (pm *PluginManager) scan(pluginDir string, requireSigned bool) error { ...@@ -273,7 +273,11 @@ func (pm *PluginManager) scan(pluginDir string, requireSigned bool) error {
if err != nil { if err != nil {
return err return err
} }
defer reader.Close() defer func() {
if err := reader.Close(); err != nil {
scanner.log.Warn("Failed to close JSON file", "path", jsonFPath, "err", err)
}
}()
jsonParser := json.NewDecoder(reader) jsonParser := json.NewDecoder(reader)
...@@ -343,7 +347,11 @@ func (s *PluginScanner) loadPlugin(pluginJSONFilePath string) error { ...@@ -343,7 +347,11 @@ func (s *PluginScanner) loadPlugin(pluginJSONFilePath string) error {
if err != nil { if err != nil {
return err return err
} }
defer reader.Close() defer func() {
if err := reader.Close(); err != nil {
s.log.Warn("Failed to close JSON file", "path", pluginJSONFilePath, "err", err)
}
}()
jsonParser := json.NewDecoder(reader) jsonParser := json.NewDecoder(reader)
pluginCommon := PluginBase{} pluginCommon := PluginBase{}
......
...@@ -2,6 +2,7 @@ package notifiers ...@@ -2,6 +2,7 @@ package notifiers
import ( import (
"bytes" "bytes"
"fmt"
"io" "io"
"mime/multipart" "mime/multipart"
"os" "os"
...@@ -168,12 +169,20 @@ func (dn *DiscordNotifier) embedImage(cmd *models.SendWebhookSync, imagePath str ...@@ -168,12 +169,20 @@ func (dn *DiscordNotifier) embedImage(cmd *models.SendWebhookSync, imagePath str
return err return err
} }
} }
defer func() {
defer f.Close() if err := f.Close(); err != nil {
dn.log.Warn("Failed to close file", "path", imagePath, "err", err)
}
}()
var b bytes.Buffer var b bytes.Buffer
w := multipart.NewWriter(&b) w := multipart.NewWriter(&b)
defer func() {
if err := w.Close(); err != nil {
// Should be OK since we already close it on non-error path
dn.log.Warn("Failed to close multipart writer", "err", err)
}
}()
fw, err := w.CreateFormField("payload_json") fw, err := w.CreateFormField("payload_json")
if err != nil { if err != nil {
return err return err
...@@ -192,7 +201,9 @@ func (dn *DiscordNotifier) embedImage(cmd *models.SendWebhookSync, imagePath str ...@@ -192,7 +201,9 @@ func (dn *DiscordNotifier) embedImage(cmd *models.SendWebhookSync, imagePath str
return err return err
} }
w.Close() if err := w.Close(); err != nil {
return fmt.Errorf("failed to close multipart writer: %w", err)
}
cmd.Body = b.String() cmd.Body = b.String()
cmd.ContentType = w.FormDataContentType() cmd.ContentType = w.FormDataContentType()
......
...@@ -292,7 +292,11 @@ func (pn *PushoverNotifier) genPushoverBody(evalContext *alerting.EvalContext, m ...@@ -292,7 +292,11 @@ func (pn *PushoverNotifier) genPushoverBody(evalContext *alerting.EvalContext, m
if err != nil { if err != nil {
return nil, b, err return nil, b, err
} }
defer f.Close() defer func() {
if err := f.Close(); err != nil {
pn.log.Warn("Failed to close file", "path", evalContext.ImageOnDiskPath, "err", err)
}
}()
fw, err := w.CreateFormFile("attachment", evalContext.ImageOnDiskPath) fw, err := w.CreateFormFile("attachment", evalContext.ImageOnDiskPath)
if err != nil { if err != nil {
......
...@@ -321,7 +321,7 @@ func (sn *SlackNotifier) Notify(evalContext *alerting.EvalContext) error { ...@@ -321,7 +321,7 @@ func (sn *SlackNotifier) Notify(evalContext *alerting.EvalContext) error {
return err return err
} }
if sn.Token != "" && sn.UploadImage { if sn.Token != "" && sn.UploadImage {
err = slackFileUpload(evalContext, sn.log, "https://slack.com/api/files.upload", sn.Recipient, sn.Token) err = sn.slackFileUpload(evalContext, sn.log, "https://slack.com/api/files.upload", sn.Recipient, sn.Token)
if err != nil { if err != nil {
return err return err
} }
...@@ -329,12 +329,12 @@ func (sn *SlackNotifier) Notify(evalContext *alerting.EvalContext) error { ...@@ -329,12 +329,12 @@ func (sn *SlackNotifier) Notify(evalContext *alerting.EvalContext) error {
return nil return nil
} }
func slackFileUpload(evalContext *alerting.EvalContext, log log.Logger, url string, recipient string, token string) error { func (sn *SlackNotifier) slackFileUpload(evalContext *alerting.EvalContext, log log.Logger, url string, recipient string, token string) error {
if evalContext.ImageOnDiskPath == "" { if evalContext.ImageOnDiskPath == "" {
evalContext.ImageOnDiskPath = filepath.Join(setting.HomePath, "public/img/mixed_styles.png") evalContext.ImageOnDiskPath = filepath.Join(setting.HomePath, "public/img/mixed_styles.png")
} }
log.Info("Uploading to slack via file.upload API") log.Info("Uploading to slack via file.upload API")
headers, uploadBody, err := generateSlackBody(evalContext.ImageOnDiskPath, token, recipient) headers, uploadBody, err := sn.generateSlackBody(evalContext.ImageOnDiskPath, token, recipient)
if err != nil { if err != nil {
return err return err
} }
...@@ -346,37 +346,47 @@ func slackFileUpload(evalContext *alerting.EvalContext, log log.Logger, url stri ...@@ -346,37 +346,47 @@ func slackFileUpload(evalContext *alerting.EvalContext, log log.Logger, url stri
return nil return nil
} }
func generateSlackBody(file string, token string, recipient string) (map[string]string, bytes.Buffer, error) { func (sn *SlackNotifier) generateSlackBody(path string, token string, recipient string) (map[string]string, bytes.Buffer, error) {
// Slack requires all POSTs to files.upload to present // Slack requires all POSTs to files.upload to present
// an "application/x-www-form-urlencoded" encoded querystring // an "application/x-www-form-urlencoded" encoded querystring
// See https://api.slack.com/methods/files.upload // See https://api.slack.com/methods/files.upload
var b bytes.Buffer var b bytes.Buffer
w := multipart.NewWriter(&b) w := multipart.NewWriter(&b)
defer func() {
if err := w.Close(); err != nil {
// Shouldn't matter since we already close w explicitly on the non-error path
sn.log.Warn("Failed to close multipart writer", "err", err)
}
}()
// Add the generated image file // Add the generated image file
f, err := os.Open(file) f, err := os.Open(path)
if err != nil { if err != nil {
return nil, b, err return nil, b, err
} }
defer f.Close() defer func() {
fw, err := w.CreateFormFile("file", file) if err := f.Close(); err != nil {
sn.log.Warn("Failed to close file", "path", path, "err", err)
}
}()
fw, err := w.CreateFormFile("file", path)
if err != nil { if err != nil {
return nil, b, err return nil, b, err
} }
_, err = io.Copy(fw, f) if _, err := io.Copy(fw, f); err != nil {
if err != nil {
return nil, b, err return nil, b, err
} }
// Add the authorization token // Add the authorization token
err = w.WriteField("token", token) if err := w.WriteField("token", token); err != nil {
if err != nil {
return nil, b, err return nil, b, err
} }
// Add the channel(s) to POST to // Add the channel(s) to POST to
err = w.WriteField("channels", recipient) if err := w.WriteField("channels", recipient); err != nil {
if err != nil {
return nil, b, err return nil, b, err
} }
w.Close() if err := w.Close(); err != nil {
return nil, b, fmt.Errorf("failed to close multipart writer: %w", err)
}
headers := map[string]string{ headers := map[string]string{
"Content-Type": w.FormDataContentType(), "Content-Type": w.FormDataContentType(),
"Authorization": "auth_token=\"" + token + "\"", "Authorization": "auth_token=\"" + token + "\"",
......
...@@ -347,7 +347,11 @@ func (fr *FileReader) readDashboardFromFile(path string, lastModified time.Time, ...@@ -347,7 +347,11 @@ func (fr *FileReader) readDashboardFromFile(path string, lastModified time.Time,
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer reader.Close() defer func() {
if err := reader.Close(); err != nil {
fr.log.Warn("Failed to close file", "path", path, "err", err)
}
}()
all, err := ioutil.ReadAll(reader) all, err := ioutil.ReadAll(reader)
if err != nil { if err != nil {
......
...@@ -96,7 +96,12 @@ func (rs *RenderingService) renderViaHttp(ctx context.Context, renderKey string, ...@@ -96,7 +96,12 @@ func (rs *RenderingService) renderViaHttp(ctx context.Context, renderKey string,
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer out.Close() defer func() {
if err := out.Close(); err != nil {
// We already close the file explicitly in the non-error path, so shouldn't be a problem
rs.log.Warn("Failed to close file", "path", filePath, "err", err)
}
}()
_, err = io.Copy(out, resp.Body) _, err = io.Copy(out, resp.Body)
if err != nil { if err != nil {
// check that we didn't timeout while receiving the response. // check that we didn't timeout while receiving the response.
...@@ -107,6 +112,9 @@ func (rs *RenderingService) renderViaHttp(ctx context.Context, renderKey string, ...@@ -107,6 +112,9 @@ func (rs *RenderingService) renderViaHttp(ctx context.Context, renderKey string,
rs.log.Error("Remote rendering request failed", "error", err) rs.log.Error("Remote rendering request failed", "error", err)
return nil, fmt.Errorf("remote rendering request failed: %w", err) return nil, fmt.Errorf("remote rendering request failed: %w", err)
} }
if err := out.Close(); err != nil {
return nil, fmt.Errorf("failed to write to %q: %w", filePath, err)
}
return &RenderResult{FilePath: filePath}, err return &RenderResult{FilePath: filePath}, err
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment