Skip to content

Commit 013411a

Browse files
authored
Merge pull request #4650 from estesp/cp-4645-1.4
[release/1.4] cherry-pick: Check if a process exists before returning it
2 parents e44e8eb + cc6f72a commit 013411a

2 files changed

Lines changed: 73 additions & 2 deletions

File tree

container_linux_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,6 +1008,73 @@ func TestContainerAttachProcess(t *testing.T) {
10081008
<-status
10091009
}
10101010

1011+
func TestContainerLoadUnexistingProcess(t *testing.T) {
1012+
t.Parallel()
1013+
1014+
if runtime.GOOS == "windows" {
1015+
// On windows, closing the write side of the pipe closes the read
1016+
// side, sending an EOF to it and preventing reopening it.
1017+
// Hence this test will always fails on windows
1018+
t.Skip("invalid logic on windows")
1019+
}
1020+
1021+
client, err := newClient(t, address)
1022+
if err != nil {
1023+
t.Fatal(err)
1024+
}
1025+
defer client.Close()
1026+
1027+
var (
1028+
image Image
1029+
ctx, cancel = testContext(t)
1030+
id = t.Name()
1031+
)
1032+
defer cancel()
1033+
1034+
image, err = client.GetImage(ctx, testImage)
1035+
if err != nil {
1036+
t.Fatal(err)
1037+
}
1038+
1039+
container, err := client.NewContainer(ctx, id, WithNewSnapshot(id, image), WithNewSpec(oci.WithImageConfig(image), withProcessArgs("sleep", "100")))
1040+
if err != nil {
1041+
t.Fatal(err)
1042+
}
1043+
defer container.Delete(ctx, WithSnapshotCleanup)
1044+
1045+
// creating IO early for easy resource cleanup
1046+
direct, err := newDirectIO(ctx, false)
1047+
if err != nil {
1048+
t.Fatal(err)
1049+
}
1050+
defer direct.Delete()
1051+
1052+
task, err := container.NewTask(ctx, empty())
1053+
if err != nil {
1054+
t.Fatal(err)
1055+
}
1056+
defer task.Delete(ctx)
1057+
1058+
status, err := task.Wait(ctx)
1059+
if err != nil {
1060+
t.Error(err)
1061+
}
1062+
1063+
if err := task.Start(ctx); err != nil {
1064+
t.Fatal(err)
1065+
}
1066+
1067+
if _, err = task.LoadProcess(ctx, "this-process-does-not-exist", direct.IOAttach); err == nil {
1068+
t.Fatal("an error should have occurred when loading a process that does not exist")
1069+
}
1070+
1071+
if err := task.Kill(ctx, syscall.SIGKILL); err != nil {
1072+
t.Error(err)
1073+
}
1074+
1075+
<-status
1076+
}
1077+
10111078
func TestContainerUserID(t *testing.T) {
10121079
t.Parallel()
10131080

runtime/v2/shim.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,10 +433,14 @@ func (s *shim) Stats(ctx context.Context) (*ptypes.Any, error) {
433433
}
434434

435435
func (s *shim) Process(ctx context.Context, id string) (runtime.Process, error) {
436-
return &process{
436+
p := &process{
437437
id: id,
438438
shim: s,
439-
}, nil
439+
}
440+
if _, err := p.State(ctx); err != nil {
441+
return nil, err
442+
}
443+
return p, nil
440444
}
441445

442446
func (s *shim) State(ctx context.Context) (runtime.State, error) {

0 commit comments

Comments
 (0)