Skip to content

Modern Docker Management, Designed for Everyone

fix: report CPU count from scheduler affinity so LXC core limits are respected (#3568)

Kyle Mendellf2b5089b1c10
diff --git a/backend/api/ws/handler.go b/backend/api/ws/handler.go
index 85a2ed1d2..9fd28d264 100644
--- a/backend/api/ws/handler.go
+++ b/backend/api/ws/handler.go
@@ -1178,6 +1178,7 @@ func (h *WebSocketHandler) applyCgroupLimits(cpuCount int, memUsed, memTotal uin
if cgroup.IsDockerContainer() {
return cpuCount, memUsed, memTotal
}
+ cpuCount = applyCPUAffinityLimitInternal(cpuCount)
cgroupLimits := h.getCachedCgroupLimitsInternal()
if cgroupLimits == nil {
return cpuCount, memUsed, memTotal
@@ -1198,6 +1199,20 @@ func (h *WebSocketHandler) applyCgroupLimits(cpuCount int, memUsed, memTotal uin
return cpuCount, memUsed, memTotal
}
+// applyCPUAffinityLimitInternal caps the CPU count at the scheduler affinity
+// mask size. In an unprivileged LXC guest the cgroup cpu quota is usually
+// unset at the guest root (the limit lives host-side), so the affinity mask is
+// the only signal of the cores actually assigned — including gap bindings like
+// CPU1+CPU7 — while /proc/cpuinfo shows the whole host (#3161). runtime.NumCPU
+// reads that mask at process start. Not applied inside Docker, matching
+// applyCgroupLimits' host-totals convention there.
+func applyCPUAffinityLimitInternal(cpuCount int) int {
+ if n := runtime.NumCPU(); n > 0 && (cpuCount == 0 || n < cpuCount) {
+ return n
+ }
+ return cpuCount
+}
+
// getDiskInfo returns disk usage and total.
func (h *WebSocketHandler) getDiskInfo(ctx context.Context) (uint64, uint64) {
diskUsagePath := h.getDiskUsagePath(ctx)
diff --git a/backend/api/ws/handler_test.go b/backend/api/ws/handler_test.go
new file mode 100644
index 000000000..5d0ce4098
--- /dev/null
+++ b/backend/api/ws/handler_test.go
@@ -0,0 +1,20 @@
+package ws
+
+import (
+ "runtime"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+)
+
+func TestApplyCPUAffinityLimitInternal(t *testing.T) {
+ affinity := runtime.NumCPU()
+
+ // A host-wide count larger than the affinity mask (LXC without lxcfs,
+ // #3161) is capped at the mask size; a smaller cgroup-derived count wins.
+ require.Equal(t, affinity, applyCPUAffinityLimitInternal(affinity+8))
+ require.Equal(t, affinity, applyCPUAffinityLimitInternal(0))
+ if affinity > 1 {
+ require.Equal(t, 1, applyCPUAffinityLimitInternal(1))
+ }
+}