Skip to content

Commit cfa8756

Browse files
committed
windows: Create init process with task
This prevents `task.Wait()` to return an error if it is called before the task is started. Signed-off-by: Kenfe-Mickael Laventure <mickael.laventure@gmail.com>
1 parent ad52664 commit cfa8756

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ func (p *process) State(ctx context.Context) (runtime.State, error) {
5252
}
5353

5454
func (p *process) Status() runtime.Status {
55+
p.Lock()
56+
defer p.Unlock()
57+
5558
if p.task.getStatus() == runtime.PausedStatus {
5659
return runtime.PausedStatus
5760
}
@@ -71,15 +74,24 @@ func (p *process) Status() runtime.Status {
7174

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

7783
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+
}
7887
err := p.hcs.ResizeConsole(uint16(size.Width), uint16(size.Height))
7988
return errors.Wrap(err, "failed to resize process console")
8089
}
8190

8291
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+
}
8395
return errors.Wrap(p.hcs.CloseStdin(), "failed to close stdin")
8496
}
8597

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: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,17 +111,16 @@ func (t *task) Info() runtime.TaskInfo {
111111
}
112112

113113
func (t *task) Start(ctx context.Context) error {
114-
if p := t.getProcess(t.id); p != nil {
115-
return errors.Wrap(errdefs.ErrFailedPrecondition, "task already started")
114+
p := t.getProcess(t.id)
115+
if p == nil {
116+
panic("init process is missing")
116117
}
117118

118-
conf := newWindowsProcessConfig(t.spec.Process, t.io)
119-
p, err := t.newProcess(ctx, t.id, conf, t.io)
120-
if err != nil {
121-
return err
119+
if p.Status() != runtime.CreatedStatus {
120+
return errors.Wrap(errdefs.ErrFailedPrecondition, "process was already started")
122121
}
122+
123123
if err := p.Start(ctx); err != nil {
124-
t.removeProcess(t.id)
125124
return err
126125
}
127126
t.publisher.Publish(ctx,

0 commit comments

Comments
 (0)