Skip to content

Commit b766494

Browse files
committed
fix runc's poststart behaviour doesn't match the runtime-spec
Signed-off-by: ningmingxiao <[email protected]>
1 parent 3778ae6 commit b766494

File tree

1 file changed

+29
-17
lines changed

1 file changed

+29
-17
lines changed

libcontainer/container_linux.go

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -212,21 +212,46 @@ func (c *Container) Start(process *Process) error {
212212
// after start returns but opens the fifo after start returns.
213213
func (c *Container) Run(process *Process) error {
214214
c.m.Lock()
215-
defer c.m.Unlock()
216215
if err := c.start(process); err != nil {
216+
c.m.Unlock()
217217
return err
218218
}
219219
if process.Init {
220-
return c.exec()
220+
err := c.exec()
221+
if err != nil {
222+
c.m.Unlock()
223+
return err
224+
}
225+
c.m.Unlock()
226+
return c.PostStart()
227+
}
228+
c.m.Unlock()
229+
return nil
230+
}
231+
232+
func (c *Container) PostStart() error {
233+
s, err := c.OCIState()
234+
if err != nil {
235+
return err
236+
}
237+
if c.config.Hooks != nil {
238+
if err := c.config.Hooks.Run(configs.Poststart, s); err != nil {
239+
return fmt.Errorf("run poststart hook %w", err)
240+
}
221241
}
222242
return nil
223243
}
224244

225245
// Exec signals the container to exec the users process at the end of the init.
226246
func (c *Container) Exec() error {
227247
c.m.Lock()
228-
defer c.m.Unlock()
229-
return c.exec()
248+
err := c.exec()
249+
if err != nil {
250+
c.m.Unlock()
251+
return err
252+
}
253+
c.m.Unlock()
254+
return c.PostStart()
230255
}
231256

232257
func (c *Container) exec() error {
@@ -353,19 +378,6 @@ func (c *Container) start(process *Process) (retErr error) {
353378

354379
if process.Init {
355380
c.fifo.Close()
356-
if c.config.Hooks != nil {
357-
s, err := c.currentOCIState()
358-
if err != nil {
359-
return err
360-
}
361-
362-
if err := c.config.Hooks.Run(configs.Poststart, s); err != nil {
363-
if err := ignoreTerminateErrors(parent.terminate()); err != nil {
364-
logrus.Warn(fmt.Errorf("error running poststart hook: %w", err))
365-
}
366-
return err
367-
}
368-
}
369381
}
370382
return nil
371383
}

0 commit comments

Comments
 (0)