Skip to content

Commit

Permalink
Fix container pid.
Browse files Browse the repository at this point in the history
Signed-off-by: Lantao Liu <lantaol@google.com>
(cherry picked from commit a6b6097)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
  • Loading branch information
Random-Liu authored and thaJeztah committed Feb 17, 2020
1 parent 89c46ed commit b970987
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 20 deletions.
49 changes: 49 additions & 0 deletions container_test.go
Expand Up @@ -1528,3 +1528,52 @@ func TestContainerHook(t *testing.T) {
}
defer task.Delete(ctx, WithProcessKill)
}

func TestShortRunningTaskPid(t *testing.T) {
t.Parallel()

client, err := newClient(t, address)
if err != nil {
t.Fatal(err)
}
defer client.Close()

var (
image Image
ctx, cancel = testContext()
id = t.Name()
)
defer cancel()

image, err = client.GetImage(ctx, testImage)
if err != nil {
t.Fatal(err)
}

container, err := client.NewContainer(ctx, id, WithNewSnapshot(id, image), WithNewSpec(oci.WithImageConfig(image), withProcessArgs("true")))
if err != nil {
t.Fatal(err)
}
defer container.Delete(ctx, WithSnapshotCleanup)

task, err := container.NewTask(ctx, empty())
if err != nil {
t.Fatal(err)
}
defer task.Delete(ctx)

finishedC, err := task.Wait(ctx)
if err != nil {
t.Fatal(err)
}

if err := task.Start(ctx); err != nil {
t.Fatal(err)
}

int32PID := int32(task.Pid())
if int32PID <= 0 {
t.Errorf("Unexpected task pid %d", int32PID)
}
<-finishedC
}
3 changes: 1 addition & 2 deletions runtime/v1/linux/proc/exec.go
Expand Up @@ -96,7 +96,6 @@ func (e *execProcess) setExited(status int) {
e.status = status
e.exited = time.Now()
e.parent.Platform.ShutdownConsole(context.Background(), e.console)
e.pid.set(StoppedPID)
close(e.waitBlock)
}

Expand Down Expand Up @@ -147,7 +146,7 @@ func (e *execProcess) kill(ctx context.Context, sig uint32, _ bool) error {
switch {
case pid == 0:
return errors.Wrap(errdefs.ErrFailedPrecondition, "process not created")
case pid < 0:
case !e.exited.IsZero():
return errors.Wrapf(errdefs.ErrNotFound, "process already finished")
default:
if err := unix.Kill(pid, syscall.Signal(sig)); err != nil {
Expand Down
9 changes: 3 additions & 6 deletions runtime/v1/linux/proc/init.go
Expand Up @@ -69,7 +69,7 @@ type Init struct {
pausing *atomicBool
status int
exited time.Time
pid safePid
pid int
closers []io.Closer
stdin io.Closer
stdio proc.Stdio
Expand Down Expand Up @@ -116,8 +116,6 @@ func (p *Init) Create(ctx context.Context, r *CreateConfig) error {
err error
socket *runc.Socket
)
p.pid.Lock()
defer p.pid.Unlock()

if r.Terminal {
if socket, err = runc.NewTempConsoleSocket(); err != nil {
Expand Down Expand Up @@ -197,7 +195,7 @@ func (p *Init) Create(ctx context.Context, r *CreateConfig) error {
if err != nil {
return errors.Wrap(err, "failed to retrieve OCI runtime container pid")
}
p.pid.pid = pid
p.pid = pid
return nil
}

Expand All @@ -213,7 +211,7 @@ func (p *Init) ID() string {

// Pid of the process
func (p *Init) Pid() int {
return p.pid.get()
return p.pid
}

// ExitStatus of the process
Expand Down Expand Up @@ -269,7 +267,6 @@ func (p *Init) setExited(status int) {
p.exited = time.Now()
p.status = status
p.Platform.ShutdownConsole(context.Background(), p.console)
p.pid.set(StoppedPID)
close(p.waitBlock)
}

Expand Down
5 changes: 1 addition & 4 deletions runtime/v1/linux/proc/init_state.go
Expand Up @@ -161,9 +161,6 @@ func (s *createdCheckpointState) Start(ctx context.Context) error {
p := s.p
sio := p.stdio

p.pid.Lock()
defer p.pid.Unlock()

var (
err error
socket *runc.Socket
Expand Down Expand Up @@ -209,7 +206,7 @@ func (s *createdCheckpointState) Start(ctx context.Context) error {
if err != nil {
return errors.Wrap(err, "failed to retrieve OCI runtime container pid")
}
p.pid.pid = pid
p.pid = pid
return s.transition("running")
}

Expand Down
2 changes: 0 additions & 2 deletions runtime/v1/linux/proc/process.go
Expand Up @@ -25,8 +25,6 @@ import (
// RuncRoot is the path to the root runc state directory
const (
RuncRoot = "/run/containerd/runc"
// StoppedPID is the pid assigned after a container has run and stopped
StoppedPID = -1
)

func stateName(v interface{}) string {
Expand Down
6 changes: 0 additions & 6 deletions runtime/v1/linux/proc/utils.go
Expand Up @@ -46,12 +46,6 @@ func (s *safePid) get() int {
return s.pid
}

func (s *safePid) set(pid int) {
s.Lock()
s.pid = pid
s.Unlock()
}

type atomicBool int32

func (ab *atomicBool) set(b bool) {
Expand Down

0 comments on commit b970987

Please sign in to comment.