Skip to content

Commit

Permalink
fix: defer calls that return a closure need to be called (#25951)
Browse files Browse the repository at this point in the history
* fix: defer calls that return a closure need to be called

* fixes #25950

* chore: avoid a double close

* chore: call defer
  • Loading branch information
philjb authored Feb 4, 2025
1 parent 73722a5 commit de12b6d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion cmd/influxd/inspect/type_conflicts/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (s Schema) WriteConflictsFile(filename string) error {

func (s Schema) encodeSchema(filename string) (rErr error) {
schemaFile, err := os.Create(filename)
defer errors2.Capture(&rErr, schemaFile.Close)
defer errors2.Capture(&rErr, schemaFile.Close)()
if err != nil {
return fmt.Errorf("unable to create schema file: %w", err)
}
Expand Down
22 changes: 13 additions & 9 deletions sqlite/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ func (s *SqlStore) BackupSqlStore(ctx context.Context, w io.Writer) (rErr error)
if err != nil {
return err
}
defer errors2.Capture(&rErr, dest.Close)

defer errors2.Capture(&rErr, dest.Close)() // close the backup sqlite store

if err := backup(ctx, dest, s); err != nil {
return err
Expand Down Expand Up @@ -227,16 +228,19 @@ func (s *SqlStore) RestoreSqlStore(ctx context.Context, r io.Reader) (rErr error
if err != nil {
return err
}
defer errors2.Capture(&rErr, f.Close)
copySyncClose := func(f *os.File, r io.Reader) (innerErr error) {
defer errors2.Capture(&innerErr, f.Close)() // close the temp file

// Copy the contents of r to the temporary file
if _, err := io.Copy(f, r); err != nil {
return err
}
if err := f.Sync(); err != nil {
return err
// Copy the contents of r to the temporary file
if _, err := io.Copy(f, r); err != nil {
return err
}
if err := f.Sync(); err != nil {
return err
}
return nil
}
if err := f.Close(); err != nil {
if err := copySyncClose(f, r); err != nil {
return err
}

Expand Down

0 comments on commit de12b6d

Please sign in to comment.