Merge pull request #15031 from ahrtr/SnapshotWithVersion_nil_panic_20221220

clientv3: add protection code to prevent SnapshotWithVersion from panicking
This commit is contained in:
Benjamin Wang 2022-12-21 06:45:31 +08:00 committed by GitHub
commit 054b24b425
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 16 deletions

View File

@ -239,6 +239,7 @@ func (m *maintenance) SnapshotWithVersion(ctx context.Context) (*SnapshotRespons
resp, err := ss.Recv()
if err != nil {
m.logAndCloseWithError(err, pw)
return nil, err
}
go func() {
// Saving response is blocking
@ -260,10 +261,11 @@ func (m *maintenance) SnapshotWithVersion(ctx context.Context) (*SnapshotRespons
}
}
}()
return &SnapshotResponse{
Header: resp.Header,
Header: resp.GetHeader(),
Snapshot: &snapshotReadCloser{ctx: ctx, ReadCloser: pr},
Version: resp.Version,
Version: resp.GetVersion(),
}, err
}

View File

@ -68,7 +68,7 @@ func SaveWithVersion(ctx context.Context, lg *zap.Logger, cfg clientv3.Config, d
start := time.Now()
resp, err := cli.SnapshotWithVersion(ctx)
if err != nil {
return resp.Version, err
return "", err
}
defer resp.Snapshot.Close()
lg.Info("fetching snapshot", zap.String("endpoint", cfg.Endpoints[0]))
@ -99,16 +99,3 @@ func SaveWithVersion(ctx context.Context, lg *zap.Logger, cfg clientv3.Config, d
lg.Info("saved", zap.String("path", dbPath))
return resp.Version, nil
}
// Save fetches snapshot from remote etcd server and saves data
// to target path. If the context "ctx" is canceled or timed out,
// snapshot save stream will error out (e.g. context.Canceled,
// context.DeadlineExceeded). Make sure to specify only one endpoint
// in client configuration. Snapshot API must be requested to a
// selected node, and saved snapshot is the point-in-time state of
// the selected node.
// Deprecated: Use SaveWithVersion instead.
func Save(ctx context.Context, lg *zap.Logger, cfg clientv3.Config, dbPath string) error {
_, err := SaveWithVersion(ctx, lg, cfg, dbPath)
return err
}