Skip to content

Commit

Permalink
Add ErrInvalidHandle and fix list stats (#1276) (#1277)
Browse files Browse the repository at this point in the history
When querying the stats of a container that is in the process of being
stopped, an ERROR_INVALID_HANDLE (0x6) may be returned. This change
ignores that error and returns an empty stats object.

This change also fixes the return values of Stats() when encountering
one of the expected errors. Returning nil stats when error is nil will
break caller assumptions of finding a valid value when error is nil.
Instead, an ErrNotFound is returned.

Signed-off-by: Gabriel Adrian Samfira <[email protected]>
(cherry picked from commit 790bcae)
Signed-off-by: Daniel Canter <[email protected]>
  • Loading branch information
dcantah authored Jan 13, 2022
1 parent d615e4a commit fc4f38d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
3 changes: 2 additions & 1 deletion cmd/containerd-shim-runhcs-v1/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,6 @@ func isStatsNotFound(err error) bool {
hcs.IsNotExist(err) ||
hcs.IsOperationInvalidState(err) ||
gcs.IsNotExist(err) ||
hcs.IsAccessIsDenied(err)
hcs.IsAccessIsDenied(err) ||
hcs.IsErrorInvalidHandle(err)
}
6 changes: 5 additions & 1 deletion cmd/containerd-shim-runhcs-v1/task_hcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -920,9 +920,13 @@ func hcsPropertiesToWindowsStats(props *hcsschema.Properties) *stats.Statistics_
func (ht *hcsTask) Stats(ctx context.Context) (*stats.Statistics, error) {
s := &stats.Statistics{}
props, err := ht.c.PropertiesV2(ctx, hcsschema.PTStatistics)
if err != nil && !isStatsNotFound(err) {
if err != nil {
if isStatsNotFound(err) {
return nil, errors.Wrapf(errdefs.ErrNotFound, "failed to fetch stats: %s", err)
}
return nil, err
}

if props != nil {
if ht.isWCOW {
s.Container = hcsPropertiesToWindowsStats(props)
Expand Down
12 changes: 12 additions & 0 deletions internal/hcs/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ var (

// ErrProcessAlreadyStopped is returned by hcs if the process we're trying to kill has already been stopped.
ErrProcessAlreadyStopped = syscall.Errno(0x8037011f)

// ErrInvalidHandle is an error that can be encountrered when querying the properties of a compute system when the handle to that
// compute system has already been closed.
ErrInvalidHandle = syscall.Errno(0x6)
)

type ErrorEvent struct {
Expand Down Expand Up @@ -252,6 +256,14 @@ func IsNotExist(err error) bool {
err == ErrElementNotFound
}

// IsErrorInvalidHandle checks whether the error is the result of an operation carried
// out on a handle that is invalid/closed. This error popped up while trying to query
// stats on a container in the process of being stopped.
func IsErrorInvalidHandle(err error) bool {
err = getInnerError(err)
return err == ErrInvalidHandle
}

// IsAlreadyClosed checks if an error is caused by the Container or Process having been
// already closed by a call to the Close() method.
func IsAlreadyClosed(err error) bool {
Expand Down
12 changes: 12 additions & 0 deletions test/vendor/github.com/Microsoft/hcsshim/internal/hcs/errors.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fc4f38d

Please sign in to comment.