Fix container pid.
Signed-off-by: Lantao Liu <lantaol@google.com>
This commit is contained in:
parent
3a31ce267d
commit
a6b6097c90
@ -1675,6 +1675,55 @@ func TestContainerExecLargeOutputWithTTY(t *testing.T) {
|
|||||||
<-finishedC
|
<-finishedC
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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(t)
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
func withProcessTTY() cio.Opt {
|
func withProcessTTY() cio.Opt {
|
||||||
return func(opt *cio.Streams) {
|
return func(opt *cio.Streams) {
|
||||||
cio.WithTerminal(opt)
|
cio.WithTerminal(opt)
|
||||||
|
@ -96,7 +96,6 @@ func (e *execProcess) setExited(status int) {
|
|||||||
e.status = status
|
e.status = status
|
||||||
e.exited = time.Now()
|
e.exited = time.Now()
|
||||||
e.parent.Platform.ShutdownConsole(context.Background(), e.console)
|
e.parent.Platform.ShutdownConsole(context.Background(), e.console)
|
||||||
e.pid.set(StoppedPID)
|
|
||||||
close(e.waitBlock)
|
close(e.waitBlock)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -147,7 +146,7 @@ func (e *execProcess) kill(ctx context.Context, sig uint32, _ bool) error {
|
|||||||
switch {
|
switch {
|
||||||
case pid == 0:
|
case pid == 0:
|
||||||
return errors.Wrap(errdefs.ErrFailedPrecondition, "process not created")
|
return errors.Wrap(errdefs.ErrFailedPrecondition, "process not created")
|
||||||
case pid < 0:
|
case !e.exited.IsZero():
|
||||||
return errors.Wrapf(errdefs.ErrNotFound, "process already finished")
|
return errors.Wrapf(errdefs.ErrNotFound, "process already finished")
|
||||||
default:
|
default:
|
||||||
if err := unix.Kill(pid, syscall.Signal(sig)); err != nil {
|
if err := unix.Kill(pid, syscall.Signal(sig)); err != nil {
|
||||||
|
@ -66,7 +66,7 @@ type Init struct {
|
|||||||
pausing *atomicBool
|
pausing *atomicBool
|
||||||
status int
|
status int
|
||||||
exited time.Time
|
exited time.Time
|
||||||
pid safePid
|
pid int
|
||||||
closers []io.Closer
|
closers []io.Closer
|
||||||
stdin io.Closer
|
stdin io.Closer
|
||||||
stdio stdio.Stdio
|
stdio stdio.Stdio
|
||||||
@ -116,8 +116,6 @@ func (p *Init) Create(ctx context.Context, r *CreateConfig) error {
|
|||||||
pio *processIO
|
pio *processIO
|
||||||
pidFile = newPidFile(p.Bundle)
|
pidFile = newPidFile(p.Bundle)
|
||||||
)
|
)
|
||||||
p.pid.Lock()
|
|
||||||
defer p.pid.Unlock()
|
|
||||||
|
|
||||||
if r.Terminal {
|
if r.Terminal {
|
||||||
if socket, err = runc.NewTempConsoleSocket(); err != nil {
|
if socket, err = runc.NewTempConsoleSocket(); err != nil {
|
||||||
@ -173,7 +171,7 @@ func (p *Init) Create(ctx context.Context, r *CreateConfig) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "failed to retrieve OCI runtime container pid")
|
return errors.Wrap(err, "failed to retrieve OCI runtime container pid")
|
||||||
}
|
}
|
||||||
p.pid.pid = pid
|
p.pid = pid
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -219,7 +217,7 @@ func (p *Init) ID() string {
|
|||||||
|
|
||||||
// Pid of the process
|
// Pid of the process
|
||||||
func (p *Init) Pid() int {
|
func (p *Init) Pid() int {
|
||||||
return p.pid.get()
|
return p.pid
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExitStatus of the process
|
// ExitStatus of the process
|
||||||
@ -275,7 +273,6 @@ func (p *Init) setExited(status int) {
|
|||||||
p.exited = time.Now()
|
p.exited = time.Now()
|
||||||
p.status = status
|
p.status = status
|
||||||
p.Platform.ShutdownConsole(context.Background(), p.console)
|
p.Platform.ShutdownConsole(context.Background(), p.console)
|
||||||
p.pid.set(StoppedPID)
|
|
||||||
close(p.waitBlock)
|
close(p.waitBlock)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -147,9 +147,6 @@ func (s *createdCheckpointState) Start(ctx context.Context) error {
|
|||||||
p := s.p
|
p := s.p
|
||||||
sio := p.stdio
|
sio := p.stdio
|
||||||
|
|
||||||
p.pid.Lock()
|
|
||||||
defer p.pid.Unlock()
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
err error
|
err error
|
||||||
socket *runc.Socket
|
socket *runc.Socket
|
||||||
@ -189,7 +186,7 @@ func (s *createdCheckpointState) Start(ctx context.Context) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "failed to retrieve OCI runtime container pid")
|
return errors.Wrap(err, "failed to retrieve OCI runtime container pid")
|
||||||
}
|
}
|
||||||
p.pid.pid = pid
|
p.pid = pid
|
||||||
return s.transition("running")
|
return s.transition("running")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,8 +39,6 @@ import (
|
|||||||
const (
|
const (
|
||||||
// RuncRoot is the path to the root runc state directory
|
// RuncRoot is the path to the root runc state directory
|
||||||
RuncRoot = "/run/containerd/runc"
|
RuncRoot = "/run/containerd/runc"
|
||||||
// StoppedPID is the pid assigned after a container has run and stopped
|
|
||||||
StoppedPID = -1
|
|
||||||
// InitPidFile name of the file that contains the init pid
|
// InitPidFile name of the file that contains the init pid
|
||||||
InitPidFile = "init.pid"
|
InitPidFile = "init.pid"
|
||||||
)
|
)
|
||||||
@ -57,12 +55,6 @@ func (s *safePid) get() int {
|
|||||||
return s.pid
|
return s.pid
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *safePid) set(pid int) {
|
|
||||||
s.Lock()
|
|
||||||
s.pid = pid
|
|
||||||
s.Unlock()
|
|
||||||
}
|
|
||||||
|
|
||||||
type atomicBool int32
|
type atomicBool int32
|
||||||
|
|
||||||
func (ab *atomicBool) set(b bool) {
|
func (ab *atomicBool) set(b bool) {
|
||||||
|
Loading…
Reference in New Issue
Block a user