-
Notifications
You must be signed in to change notification settings - Fork 9.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prevent leaking file descriptor during snapshotting and provide better logging of errors #19093
Open
serathius
wants to merge
1
commit into
etcd-io:main
Choose a base branch
from
serathius:snapshot-leak
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
import ( | ||
"context" | ||
"crypto/sha256" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"os" | ||
|
@@ -54,38 +55,58 @@ | |
if err != nil { | ||
return "", err | ||
} | ||
defer cli.Close() | ||
defer func() { | ||
err = cli.Close() | ||
if err != nil { | ||
lg.Error("Failed to close client", zap.Error(err)) | ||
} | ||
}() | ||
|
||
partpath := dbPath + ".part" | ||
defer os.RemoveAll(partpath) | ||
defer func() { | ||
err = os.RemoveAll(partpath) | ||
if err != nil { | ||
lg.Error("Failed to cleanup .part file", zap.Error(err)) | ||
} | ||
}() | ||
|
||
var f *os.File | ||
f, err = os.OpenFile(partpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, fileutil.PrivateFileMode) | ||
f, err := os.OpenFile(partpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, fileutil.PrivateFileMode) | ||
if err != nil { | ||
return "", fmt.Errorf("could not open %s (%w)", partpath, err) | ||
} | ||
defer func() { | ||
err = f.Close() | ||
if err != nil && !errors.Is(err, os.ErrClosed) { | ||
lg.Error("Could not close file descriptor", zap.Error(err)) | ||
} | ||
}() | ||
lg.Info("created temporary db file", zap.String("path", partpath)) | ||
|
||
start := time.Now() | ||
resp, err := cli.SnapshotWithVersion(ctx) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer resp.Snapshot.Close() | ||
defer func() { | ||
err = resp.Snapshot.Close() | ||
if err != nil { | ||
lg.Error("Could not close snapshot stream", zap.Error(err)) | ||
} | ||
}() | ||
lg.Info("fetching snapshot", zap.String("endpoint", cfg.Endpoints[0])) | ||
var size int64 | ||
size, err = io.Copy(f, resp.Snapshot) | ||
if err != nil { | ||
return resp.Version, err | ||
return resp.Version, fmt.Errorf("could not write snapshot: %w", err) | ||
} | ||
if !hasChecksum(size) { | ||
return resp.Version, fmt.Errorf("sha256 checksum not found [bytes: %d]", size) | ||
} | ||
if err = fileutil.Fsync(f); err != nil { | ||
return resp.Version, err | ||
return resp.Version, fmt.Errorf("could not fsync snapshot: %w", err) | ||
} | ||
if err = f.Close(); err != nil { | ||
return resp.Version, err | ||
return resp.Version, fmt.Errorf("could not close file descriptor: %w", err) | ||
} | ||
lg.Info("fetched snapshot", | ||
zap.String("endpoint", cfg.Endpoints[0]), | ||
|
@@ -98,5 +119,5 @@ | |
return resp.Version, fmt.Errorf("could not rename %s to %s (%w)", partpath, dbPath, err) | ||
} | ||
lg.Info("saved", zap.String("path", dbPath)) | ||
return resp.Version, nil | ||
return resp.Version, err | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just curious about using |
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It works. But I think a better and more explicit way is to use
sync.Once
to ensure f.Close is only executed once. So that it's well aware that we need to close the file before renaming, but also need to ensure it's closed in case early return due to any error.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Checked https://pkg.go.dev/os#File.Close and the api can guarantee it can be executed only once.
sync.Once
is not neccesary here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a big deal, please feel free to merge this PR and backport to 3.5 and 3.4.