Skip to content

runtime: GOMAXPROCS ignores the requested value #2261

Description

@cpunion

Summary

runtime.GOMAXPROCS(n) in an LLGo-generated program returns the host maximum but does not apply positive n. This also makes go test -cpu=1 ineffective and causes the testing package to report that every benchmark left GOMAXPROCS at the host CPU count.

Tested on xgo-dev/llgo main at 60c30f2ff, Go 1.26.5, LLVM 22.1.8, Darwin/arm64.

Minimal reproduction

package main

import "runtime"

func main() {
    before := runtime.GOMAXPROCS(0)
    previous := runtime.GOMAXPROCS(1)
    after := runtime.GOMAXPROCS(0)
    println(before, previous, after)
    if previous != before || after != 1 {
        panic("runtime.GOMAXPROCS did not apply the requested value")
    }
}

With GOMAXPROCS=4, the host Go toolchain prints:

4 4 1

The current LLGo main build prints and then panics:

16 16 16
panic: runtime.GOMAXPROCS did not apply the requested value

The implementation currently ignores n:

func GOMAXPROCS(n int) int {
    return int(c_maxprocs())
}

Impact

  • testing cannot enforce -test.cpu / go test -cpu.
  • Benchmarks emit messages such as testing: BenchmarkDirectCall left GOMAXPROCS set to 16.
  • Programs that use GOMAXPROCS to bound parallelism observe incorrect standard-library behavior.

A regression test can use the program above and assert that the setter returns the previous value and that a subsequent query returns the requested value.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions