GOMAXPROCS in a container: when Go thinks the whole server is its own

In the previous post I described Sima's scheduler restarting every few minutes. Before we found the real root cause, a deadlock over the connection pool, we arrested another suspect first: GOMAXPROCS. It wasn't the main culprit, but it was a real problem of its own, and it's probably in your services too. So it deserves its own post.
The problem
The scheduler ran in Docker Swarm with a cpus: 0.25 limit and was built with Go 1.24. Up to 1.24, Go set GOMAXPROCS to the number of usable logical cores (runtime.NumCPU()). The CPU limit Docker applies through cgroups doesn't affect that number. So the runtime believed every core on the server was its own, while its share was a quarter of one.
Why that's bad
On Linux a CPU limit is enforced with CFS bandwidth control. The container gets a quota per 100 ms period, which for 0.25 is 25 ms. If several threads work at once, the quota runs out sooner and the whole container is throttled until the period ends, doing nothing at all.
With a large GOMAXPROCS, the runtime puts many threads on the CPU at once, and the GC spreads its work across every P. So a small burst of work, a GC cycle or a few concurrent tasks, burns the whole quota in a few milliseconds, and the process then sits frozen until the period ends. From outside it looks like this:
- Long-tail latency: p50 is fine, but p99 is sometimes tens of times worse.
- A healthcheck that sometimes times out, without the service actually being broken.
- CPU usage that looks below the limit on the chart, while the
nr_throttledcounter incpu.statkeeps climbing.
What we did
Two small changes. We set GOMAXPROCS=1 as an environment variable and raised the CPU share to one full core. The scheduler does no heavy parallel work, and one P is enough. The point was to make the share and GOMAXPROCS agree.
The restarts didn't stop with this change, because the main culprit was elsewhere. But the throttling disappeared, and seeing the real problem got easier. The first lesson: one symptom can have two causes stacked on top of each other. Measure again after every fix.
What Go 1.25 changed
Since Go 1.25, the runtime on Linux also considers the cgroup CPU limit. If that limit is lower than the core count, it becomes the default GOMAXPROCS, rounded up. If the limit changes, the runtime updates the value periodically. So if you build with 1.25 or newer, much of this problem solves itself, as long as you haven't set GOMAXPROCS explicitly. If you have, the runtime respects it.
Two footnotes:
- It only considers the limit. A Kubernetes CPU request has no effect on GOMAXPROCS.
runtime.NumCPU()has not changed and still returns the host's usable cores.
The trap that remains: anything looking at NumCPU
The second footnote matters more than it seems. Many libraries size their resources from NumCPU, not GOMAXPROCS. An example close to home: pgxpool defaults its connection cap to the larger of 4 and runtime.NumCPU(). So that same quarter-core service, landing on a 32-core server, may open up to 32 connections to Postgres. Multiply ten replicas by that, and the database's max_connections fills up sooner than you'd think.
cfg, err := pgxpool.ParseConfig(dsn)
if err != nil {
return nil, err
}
// size the pool for this service, not for the host it lands on
cfg.MaxConns = 8
Or write the same in the DSN: ?pool_max_conns=8.
A short checklist
- Build with Go 1.25 or newer and don't set GOMAXPROCS by hand unless you have a specific reason.
- On older versions, set
GOMAXPROCSto the CPU share, or usego.uber.org/automaxprocs. - Size pools and workers explicitly; don't lean on NumCPU.
- To diagnose, read
cpu.stat:nr_throttledandthrottled_usec.
These are lessons from running Sima on Docker Swarm. The next post is about SSR: rendering React without Next.js, on a Go backend.
GOMAXPROCS was not the real cause of the restarts; that was a deadlock between an advisory lock and the pool. Both came up while moving from Laravel to Go.
Comments
Sign in to leave a comment.
Sign inLoading…



