From 91196914797c6649896e2a68327878d59318ef9a Mon Sep 17 00:00:00 2001 From: Thomas Hipp Date: Mon, 23 Oct 2023 12:41:27 +0200 Subject: [PATCH] [lxd-import] lxd/instance/drivers: Check running status with `InitPID` for cgroups Cgroup storage limits are not applied when the container starts up. That is because `IsRunning` returns `false` during the container startup. This commits fixes this issue by relying on `InitPID` instead of `IsRunning`. The former will return a positive integer if the container is running even if the container is not fully set up yet. Fixes #12343 Signed-off-by: Thomas Hipp --- internal/server/instance/drivers/driver_lxc.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/internal/server/instance/drivers/driver_lxc.go b/internal/server/instance/drivers/driver_lxc.go index 8117ab3c712..4535ee0d462 100644 --- a/internal/server/instance/drivers/driver_lxc.go +++ b/internal/server/instance/drivers/driver_lxc.go @@ -1752,7 +1752,10 @@ func (d *lxc) deviceHandleMounts(mounts []deviceConfig.MountEntryItem) error { // DeviceEventHandler actions the results of a RunConfig after an event has occurred on a device. func (d *lxc) DeviceEventHandler(runConf *deviceConfig.RunConfig) error { // Device events can only be processed when the container is running. - if !d.IsRunning() { + // We use InitPID here rather than IsRunning because this task can be triggered during the + // container startup process, which is during the time that the start lock is held, which causes + // IsRunning to return false (because the container hasn't fully started yet). + if d.InitPID() <= 0 { return nil } @@ -3967,8 +3970,12 @@ func (d *lxc) CGroupSet(key string, value string) error { return err } - // Make sure the container is running - if !d.IsRunning() { + // Make sure the container is running. + // We use InitPID here rather than IsRunning because this task can be triggered during the container's + // startup process, which is during the time that the start lock is held, which causes IsRunning to + // return false (because the container hasn't fully started yet) but it is sufficiently started to + // have its cgroup disk limits set. + if d.InitPID() <= 0 { return fmt.Errorf("Can't set cgroups on a stopped container") }