Skip to content

Commit 9dc4b98

Browse files
devanbenzphiljb
andauthored
fix: defer calls that return a closure need to be called (#25951) (#26393)
Co-authored-by: Phil Bracikowski <13472206+philjb@users.noreply.github.com> fixes #25950
1 parent ced7c77 commit 9dc4b98

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

cmd/influxd/inspect/type_conflicts/schema.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func (s Schema) WriteConflictsFile(filename string) error {
125125

126126
func (s Schema) encodeSchema(filename string) (rErr error) {
127127
schemaFile, err := os.Create(filename)
128-
defer errors2.Capture(&rErr, schemaFile.Close)
128+
defer errors2.Capture(&rErr, schemaFile.Close)()
129129
if err != nil {
130130
return fmt.Errorf("unable to create schema file: %w", err)
131131
}

sqlite/sqlite.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ func (s *SqlStore) BackupSqlStore(ctx context.Context, w io.Writer) (rErr error)
138138
if err != nil {
139139
return err
140140
}
141-
defer errors2.Capture(&rErr, dest.Close)
141+
142+
defer errors2.Capture(&rErr, dest.Close)() // close the backup sqlite store
142143

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

232-
// Copy the contents of r to the temporary file
233-
if _, err := io.Copy(f, r); err != nil {
234-
return err
235-
}
236-
if err := f.Sync(); err != nil {
237-
return err
234+
// Copy the contents of r to the temporary file
235+
if _, err := io.Copy(f, r); err != nil {
236+
return err
237+
}
238+
if err := f.Sync(); err != nil {
239+
return err
240+
}
241+
return nil
238242
}
239-
if err := f.Close(); err != nil {
243+
if err := copySyncClose(f, r); err != nil {
240244
return err
241245
}
242246

0 commit comments

Comments
 (0)