diff --git a/libcontainer/container_linux.go b/libcontainer/container_linux.go index e3e2cf2bdb8..ac2db63365d 100644 --- a/libcontainer/container_linux.go +++ b/libcontainer/container_linux.go @@ -212,12 +212,32 @@ func (c *Container) Start(process *Process) error { // after start returns but opens the fifo after start returns. func (c *Container) Run(process *Process) error { c.m.Lock() - defer c.m.Unlock() if err := c.start(process); err != nil { + c.m.Unlock() return err } if process.Init { - return c.exec() + err := c.exec() + if err != nil { + c.m.Unlock() + return err + } + c.m.Unlock() + return c.PostStart() + } + c.m.Unlock() + return nil +} + +func (c *Container) PostStart() error { + s, err := c.OCIState() + if err != nil { + return err + } + if c.config.Hooks != nil { + if err := c.config.Hooks.Run(configs.Poststart, s); err != nil { + return fmt.Errorf("run poststart hook %w", err) + } } return nil } @@ -225,8 +245,13 @@ func (c *Container) Run(process *Process) error { // Exec signals the container to exec the users process at the end of the init. func (c *Container) Exec() error { c.m.Lock() - defer c.m.Unlock() - return c.exec() + err := c.exec() + if err != nil { + c.m.Unlock() + return err + } + c.m.Unlock() + return c.PostStart() } func (c *Container) exec() error { @@ -353,19 +378,6 @@ func (c *Container) start(process *Process) (retErr error) { if process.Init { c.fifo.Close() - if c.config.Hooks != nil { - s, err := c.currentOCIState() - if err != nil { - return err - } - - if err := c.config.Hooks.Run(configs.Poststart, s); err != nil { - if err := ignoreTerminateErrors(parent.terminate()); err != nil { - logrus.Warn(fmt.Errorf("error running poststart hook: %w", err)) - } - return err - } - } } return nil }