X Tutup
Skip to content

Commit a6b6097

Browse files
committed
Fix container pid.
Signed-off-by: Lantao Liu <lantaol@google.com>
1 parent 3a31ce2 commit a6b6097

File tree

5 files changed

+54
-20
lines changed

5 files changed

+54
-20
lines changed

container_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1675,6 +1675,55 @@ func TestContainerExecLargeOutputWithTTY(t *testing.T) {
16751675
<-finishedC
16761676
}
16771677

1678+
func TestShortRunningTaskPid(t *testing.T) {
1679+
t.Parallel()
1680+
1681+
client, err := newClient(t, address)
1682+
if err != nil {
1683+
t.Fatal(err)
1684+
}
1685+
defer client.Close()
1686+
1687+
var (
1688+
image Image
1689+
ctx, cancel = testContext(t)
1690+
id = t.Name()
1691+
)
1692+
defer cancel()
1693+
1694+
image, err = client.GetImage(ctx, testImage)
1695+
if err != nil {
1696+
t.Fatal(err)
1697+
}
1698+
1699+
container, err := client.NewContainer(ctx, id, WithNewSnapshot(id, image), WithNewSpec(oci.WithImageConfig(image), withProcessArgs("true")))
1700+
if err != nil {
1701+
t.Fatal(err)
1702+
}
1703+
defer container.Delete(ctx, WithSnapshotCleanup)
1704+
1705+
task, err := container.NewTask(ctx, empty())
1706+
if err != nil {
1707+
t.Fatal(err)
1708+
}
1709+
defer task.Delete(ctx)
1710+
1711+
finishedC, err := task.Wait(ctx)
1712+
if err != nil {
1713+
t.Fatal(err)
1714+
}
1715+
1716+
if err := task.Start(ctx); err != nil {
1717+
t.Fatal(err)
1718+
}
1719+
1720+
int32PID := int32(task.Pid())
1721+
if int32PID <= 0 {
1722+
t.Errorf("Unexpected task pid %d", int32PID)
1723+
}
1724+
<-finishedC
1725+
}
1726+
16781727
func withProcessTTY() cio.Opt {
16791728
return func(opt *cio.Streams) {
16801729
cio.WithTerminal(opt)

pkg/process/exec.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ func (e *execProcess) setExited(status int) {
9696
e.status = status
9797
e.exited = time.Now()
9898
e.parent.Platform.ShutdownConsole(context.Background(), e.console)
99-
e.pid.set(StoppedPID)
10099
close(e.waitBlock)
101100
}
102101

@@ -147,7 +146,7 @@ func (e *execProcess) kill(ctx context.Context, sig uint32, _ bool) error {
147146
switch {
148147
case pid == 0:
149148
return errors.Wrap(errdefs.ErrFailedPrecondition, "process not created")
150-
case pid < 0:
149+
case !e.exited.IsZero():
151150
return errors.Wrapf(errdefs.ErrNotFound, "process already finished")
152151
default:
153152
if err := unix.Kill(pid, syscall.Signal(sig)); err != nil {

pkg/process/init.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ type Init struct {
6666
pausing *atomicBool
6767
status int
6868
exited time.Time
69-
pid safePid
69+
pid int
7070
closers []io.Closer
7171
stdin io.Closer
7272
stdio stdio.Stdio
@@ -116,8 +116,6 @@ func (p *Init) Create(ctx context.Context, r *CreateConfig) error {
116116
pio *processIO
117117
pidFile = newPidFile(p.Bundle)
118118
)
119-
p.pid.Lock()
120-
defer p.pid.Unlock()
121119

122120
if r.Terminal {
123121
if socket, err = runc.NewTempConsoleSocket(); err != nil {
@@ -173,7 +171,7 @@ func (p *Init) Create(ctx context.Context, r *CreateConfig) error {
173171
if err != nil {
174172
return errors.Wrap(err, "failed to retrieve OCI runtime container pid")
175173
}
176-
p.pid.pid = pid
174+
p.pid = pid
177175
return nil
178176
}
179177

@@ -219,7 +217,7 @@ func (p *Init) ID() string {
219217

220218
// Pid of the process
221219
func (p *Init) Pid() int {
222-
return p.pid.get()
220+
return p.pid
223221
}
224222

225223
// ExitStatus of the process
@@ -275,7 +273,6 @@ func (p *Init) setExited(status int) {
275273
p.exited = time.Now()
276274
p.status = status
277275
p.Platform.ShutdownConsole(context.Background(), p.console)
278-
p.pid.set(StoppedPID)
279276
close(p.waitBlock)
280277
}
281278

pkg/process/init_state.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,6 @@ func (s *createdCheckpointState) Start(ctx context.Context) error {
147147
p := s.p
148148
sio := p.stdio
149149

150-
p.pid.Lock()
151-
defer p.pid.Unlock()
152-
153150
var (
154151
err error
155152
socket *runc.Socket
@@ -189,7 +186,7 @@ func (s *createdCheckpointState) Start(ctx context.Context) error {
189186
if err != nil {
190187
return errors.Wrap(err, "failed to retrieve OCI runtime container pid")
191188
}
192-
p.pid.pid = pid
189+
p.pid = pid
193190
return s.transition("running")
194191
}
195192

pkg/process/utils.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ import (
3939
const (
4040
// RuncRoot is the path to the root runc state directory
4141
RuncRoot = "/run/containerd/runc"
42-
// StoppedPID is the pid assigned after a container has run and stopped
43-
StoppedPID = -1
4442
// InitPidFile name of the file that contains the init pid
4543
InitPidFile = "init.pid"
4644
)
@@ -57,12 +55,6 @@ func (s *safePid) get() int {
5755
return s.pid
5856
}
5957

60-
func (s *safePid) set(pid int) {
61-
s.Lock()
62-
s.pid = pid
63-
s.Unlock()
64-
}
65-
6658
type atomicBool int32
6759

6860
func (ab *atomicBool) set(b bool) {

0 commit comments

Comments
 (0)
X Tutup