Search code examples
gogoroutine

golang runtime: failed to create new OS thread (have 2049 already; errno=12)


I created lots of goroutines on MacOs, and there was an error emitted when program executing.

goRoutineId = 3710, i = 3683, len(chan) = 2049
runtime: failed to create new OS thread (have 2049 already; errno=12)
fatal error: runtime.newosproc

So I wonder what the "failed to create new OS thread" means, is that an operating system limitation, of just golang has no ability to create more goroutine? Thank you for helping me.


Solution

  • It's OS's limitation. I would assume you are using linux.

    According to the source of go, it is calling clone system call

    ret := clone(cloneFlags, stk, unsafe.Pointer(mp), unsafe.Pointer(mp.g0), unsafe.Pointer(funcPC(mstart)))
    sigprocmask(_SIG_SETMASK, &oset, nil)
    
    if ret < 0 {
        print("runtime: failed to create new OS thread (have ", mcount(), " already; errno=", -ret, ")\n")
        if ret == -_EAGAIN {
            println("runtime: may need to increase max user processes (ulimit -u)")
        }
        throw("newosproc")
    }
    

    from the manpage of clone(2), when the errno=12, the error reason is out of memory

    ENOMEM Cannot allocate sufficient memory to allocate a task structure
                  for the child, or to copy those parts of the caller's context
                  that need to be copied.