Skip to content

Commit 72bb45a

Browse files
Merge pull request containerd#1603 from mlaventure/fix-windows-flaky-tests
windows: ensure a init process exist after "Create()"
2 parents b2e3482 + cfa8756 commit 72bb45a

File tree

4 files changed

+46
-19
lines changed

4 files changed

+46
-19
lines changed

container_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,9 @@ func TestContainerOutput(t *testing.T) {
203203
}
204204

205205
status := <-statusC
206-
code, _, _ := status.Result()
206+
code, _, err := status.Result()
207207
if code != 0 {
208-
t.Errorf("expected status 0 but received %d", code)
208+
t.Errorf("expected status 0 but received %d: %v", code, err)
209209
}
210210
if _, err := task.Delete(ctx); err != nil {
211211
t.Error(err)

windows/process.go

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package windows
55
import (
66
"context"
77
"io"
8+
"sync"
89
"syscall"
910
"time"
1011

@@ -19,13 +20,14 @@ import (
1920

2021
// process implements containerd.Process and containerd.State
2122
type process struct {
23+
sync.Mutex
24+
2225
hcs hcsshim.Process
2326

24-
id string
25-
pid uint32
26-
io *pipeSet
27-
status runtime.Status
28-
task *task
27+
id string
28+
pid uint32
29+
io *pipeSet
30+
task *task
2931

3032
exitCh chan struct{}
3133
exitCode uint32
@@ -50,6 +52,9 @@ func (p *process) State(ctx context.Context) (runtime.State, error) {
5052
}
5153

5254
func (p *process) Status() runtime.Status {
55+
p.Lock()
56+
defer p.Unlock()
57+
5358
if p.task.getStatus() == runtime.PausedStatus {
5459
return runtime.PausedStatus
5560
}
@@ -69,15 +74,24 @@ func (p *process) Status() runtime.Status {
6974

7075
func (p *process) Kill(ctx context.Context, sig uint32, all bool) error {
7176
// On windows all signals kill the process
77+
if p.Status() == runtime.CreatedStatus {
78+
return errors.Wrap(errdefs.ErrFailedPrecondition, "process was not started")
79+
}
7280
return errors.Wrap(p.hcs.Kill(), "failed to kill process")
7381
}
7482

7583
func (p *process) ResizePty(ctx context.Context, size runtime.ConsoleSize) error {
84+
if p.Status() == runtime.CreatedStatus {
85+
return errors.Wrap(errdefs.ErrFailedPrecondition, "process was not started")
86+
}
7687
err := p.hcs.ResizeConsole(uint16(size.Width), uint16(size.Height))
7788
return errors.Wrap(err, "failed to resize process console")
7889
}
7990

8091
func (p *process) CloseIO(ctx context.Context) error {
92+
if p.Status() == runtime.CreatedStatus {
93+
return errors.Wrap(errdefs.ErrFailedPrecondition, "process was not started")
94+
}
8195
return errors.Wrap(p.hcs.CloseStdin(), "failed to close stdin")
8296
}
8397

@@ -93,6 +107,13 @@ func (p *process) ExitCode() (uint32, time.Time, error) {
93107
}
94108

95109
func (p *process) Start(ctx context.Context) (err error) {
110+
p.Lock()
111+
defer p.Unlock()
112+
113+
if p.hcs != nil {
114+
return errors.Wrap(errdefs.ErrFailedPrecondition, "process already started")
115+
}
116+
96117
// If we fail, close the io right now
97118
defer func() {
98119
if err != nil {
@@ -132,6 +153,7 @@ func (p *process) Start(ctx context.Context) (err error) {
132153
if p.io.stderr != nil {
133154
go ioCopy("stderr", p.io.stderr, stderr)
134155
}
156+
p.hcs = hp
135157

136158
// Wait for the process to exit to get the exit status
137159
go func() {
@@ -174,8 +196,6 @@ func (p *process) Start(ctx context.Context) (err error) {
174196
// Cleanup HCS resources
175197
hp.Close()
176198
}()
177-
p.status = runtime.RunningStatus
178-
p.hcs = hp
179199
return nil
180200
}
181201

windows/runtime.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,11 @@ func (r *windowsRuntime) newTask(ctx context.Context, namespace, id string, spec
304304
hcsContainer: ctr,
305305
terminateDuration: createOpts.TerminateDuration,
306306
}
307+
// Create the new process but don't start it
308+
pconf := newWindowsProcessConfig(t.spec.Process, t.io)
309+
if _, err = t.newProcess(ctx, t.id, pconf, t.io); err != nil {
310+
return nil, err
311+
}
307312
r.tasks.Add(ctx, t)
308313

309314
var rootfs []*containerdtypes.Mount

windows/task.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,16 @@ func (t *task) Info() runtime.TaskInfo {
111111
}
112112

113113
func (t *task) Start(ctx context.Context) error {
114-
conf := newWindowsProcessConfig(t.spec.Process, t.io)
115-
p, err := t.newProcess(ctx, t.id, conf, t.io)
116-
if err != nil {
117-
return err
114+
p := t.getProcess(t.id)
115+
if p == nil {
116+
panic("init process is missing")
118117
}
118+
119+
if p.Status() != runtime.CreatedStatus {
120+
return errors.Wrap(errdefs.ErrFailedPrecondition, "process was already started")
121+
}
122+
119123
if err := p.Start(ctx); err != nil {
120-
t.removeProcess(t.id)
121124
return err
122125
}
123126
t.publisher.Publish(ctx,
@@ -136,13 +139,13 @@ func (t *task) Pause(ctx context.Context) error {
136139
t.Lock()
137140
t.status = runtime.PausedStatus
138141
t.Unlock()
139-
}
140-
if err == nil {
142+
141143
t.publisher.Publish(ctx,
142144
runtime.TaskPausedEventTopic,
143145
&eventsapi.TaskPaused{
144146
ContainerID: t.id,
145147
})
148+
return nil
146149
}
147150
return errors.Wrap(err, "hcsshim failed to pause task")
148151
}
@@ -157,13 +160,13 @@ func (t *task) Resume(ctx context.Context) error {
157160
t.Lock()
158161
t.status = runtime.RunningStatus
159162
t.Unlock()
160-
}
161-
if err == nil {
163+
162164
t.publisher.Publish(ctx,
163165
runtime.TaskResumedEventTopic,
164166
&eventsapi.TaskResumed{
165167
ContainerID: t.id,
166168
})
169+
return nil
167170
}
168171
return errors.Wrap(err, "hcsshim failed to resume task")
169172
}
@@ -313,7 +316,6 @@ func (t *task) newProcess(ctx context.Context, id string, conf *hcsshim.ProcessC
313316
id: id,
314317
pid: pid,
315318
io: pset,
316-
status: runtime.CreatedStatus,
317319
task: t,
318320
exitCh: make(chan struct{}),
319321
conf: conf,

0 commit comments

Comments
 (0)