Skip to content

Commit 8eda32e

Browse files
Check if a process exists before returning it
Fixes #4632. Signed-off-by: Giuseppe Capizzi <[email protected]> Co-authored-by: Danail Branekov <[email protected]>
1 parent ba83775 commit 8eda32e

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
@@ -441,10 +441,14 @@ func (s *shim) Stats(ctx context.Context) (*ptypes.Any, error) {
441441
}
442442

443443
func (s *shim) Process(ctx context.Context, id string) (runtime.Process, error) {
444-
return &process{
444+
p := &process{
445445
id: id,
446446
shim: s,
447-
}, nil
447+
}
448+
if _, err := p.State(ctx); err != nil {
449+
return nil, err
450+
}
451+
return p, nil
448452
}
449453

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

0 commit comments

Comments
 (0)