Skip to content

Commit

Permalink
Ignore ERROR_ACCESS_DENIED on Kill (microsoft#1252)
Browse files Browse the repository at this point in the history
When calling HcsTerminateProcess on a process that has exited, but we
still have open handles to, an ERROR_ACCESS_DENIED may be returned.

https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-terminateprocess#remarks

Signed-off-by: Gabriel Adrian Samfira <[email protected]>
  • Loading branch information
gabriel-samfira committed Dec 29, 2021
1 parent 0124eb3 commit 58caeed
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
14 changes: 14 additions & 0 deletions internal/hcs/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package hcs
import (
"context"
"encoding/json"
"errors"
"io"
"os"
"sync"
"syscall"
"time"
Expand Down Expand Up @@ -150,6 +152,18 @@ func (process *Process) Kill(ctx context.Context) (bool, error) {
}

resultJSON, err := vmcompute.HcsTerminateProcess(ctx, process.handle)
if err != nil && errors.Is(err, os.ErrPermission) {
// HcsTerminateProcess ends up calling TerminateProcess in the context
// of a container. According to the TerminateProcess documentation:
// https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-terminateprocess#remarks
// After a process has terminated, call to TerminateProcess with open
// handles to the process fails with ERROR_ACCESS_DENIED (5) error code.
// It's safe to ignore this error here. HCS should always have permissions
// to kill processes inside any container. So an ERROR_ACCESS_DENIED
// is unlikely to be anything else than what the ending remarks in the
// documentation states.
return true, nil
}
events := processHcsResult(ctx, resultJSON)
delivered, err := process.processSignalResult(ctx, err)
if err != nil {
Expand Down
14 changes: 14 additions & 0 deletions test/vendor/github.com/Microsoft/hcsshim/internal/hcs/process.go

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

0 comments on commit 58caeed

Please sign in to comment.