Skip to content

Commit d47ee95

Browse files
gcapizzidanail-branekov
authored andcommitted
Check if a process exists before returning it
Fixes #4632. Signed-off-by: Giuseppe Capizzi <[email protected]> Co-authored-by: Danail Branekov <[email protected]> (cherry picked from commit 8eda32e)
1 parent 53371c8 commit d47ee95

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
@@ -940,6 +940,73 @@ func TestContainerAttachProcess(t *testing.T) {
940940
<-status
941941
}
942942

943+
func TestContainerLoadUnexistingProcess(t *testing.T) {
944+
t.Parallel()
945+
946+
if runtime.GOOS == "windows" {
947+
// On windows, closing the write side of the pipe closes the read
948+
// side, sending an EOF to it and preventing reopening it.
949+
// Hence this test will always fails on windows
950+
t.Skip("invalid logic on windows")
951+
}
952+
953+
client, err := newClient(t, address)
954+
if err != nil {
955+
t.Fatal(err)
956+
}
957+
defer client.Close()
958+
959+
var (
960+
image Image
961+
ctx, cancel = testContext(t)
962+
id = t.Name()
963+
)
964+
defer cancel()
965+
966+
image, err = client.GetImage(ctx, testImage)
967+
if err != nil {
968+
t.Fatal(err)
969+
}
970+
971+
container, err := client.NewContainer(ctx, id, WithNewSnapshot(id, image), WithNewSpec(oci.WithImageConfig(image), withProcessArgs("sleep", "100")))
972+
if err != nil {
973+
t.Fatal(err)
974+
}
975+
defer container.Delete(ctx, WithSnapshotCleanup)
976+
977+
// creating IO early for easy resource cleanup
978+
direct, err := newDirectIO(ctx, false)
979+
if err != nil {
980+
t.Fatal(err)
981+
}
982+
defer direct.Delete()
983+
984+
task, err := container.NewTask(ctx, empty())
985+
if err != nil {
986+
t.Fatal(err)
987+
}
988+
defer task.Delete(ctx)
989+
990+
status, err := task.Wait(ctx)
991+
if err != nil {
992+
t.Error(err)
993+
}
994+
995+
if err := task.Start(ctx); err != nil {
996+
t.Fatal(err)
997+
}
998+
999+
if _, err = task.LoadProcess(ctx, "this-process-does-not-exist", direct.IOAttach); err == nil {
1000+
t.Fatal("an error should have occurred when loading a process that does not exist")
1001+
}
1002+
1003+
if err := task.Kill(ctx, syscall.SIGKILL); err != nil {
1004+
t.Error(err)
1005+
}
1006+
1007+
<-status
1008+
}
1009+
9431010
func TestContainerUserID(t *testing.T) {
9441011
t.Parallel()
9451012

runtime/v2/shim.go

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

432432
func (s *shim) Process(ctx context.Context, id string) (runtime.Process, error) {
433-
return &process{
433+
p := &process{
434434
id: id,
435435
shim: s,
436-
}, nil
436+
}
437+
if _, err := p.State(ctx); err != nil {
438+
return nil, err
439+
}
440+
return p, nil
437441
}
438442

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

0 commit comments

Comments
 (0)