package main
import (
"fmt"
"websocket"
)
func openConn(ch chan<- int) {
ws, err := websocket.Dial("ws://localhost:9999/", "", "http://
localhost/")
if err != nil {
fmt.Sprintf("Dial: " + err.String())
}
defer ws.Close()
ch <- 1
}
func main() {
ch := make(chan int)
for i := 0; i < 1000; i++ {
go openConn(ch)
}
ch <- 1
}
And it fails with:
runtime/cgo: pthread_create failed: Resource temporarily unavailable
SIGABRT: abort
PC=0xf97422
goroutine 1001 [1]:
main.openConn /home/vladimir/goplayground/testclient/test.go:8
main.openConn(0x977c18d0, 0x0)
runtime.goexit /archive/vladimir/Dropbox/workspace/go/src/pkg/runtime/
proc.c:246
runtime.goexit()
----- goroutine created by -----
main.main+0x5e /home/vladimir/goplayground/testclient/test.go:22
My settings:
ulimit -n = 65535
cat /proc/sys/fs/file-max = 205074
What can I check to fix this?
On Nov 21, 5:56 pm, Vladimir Mihailenco <vladimir.web...@gmail.com>
wrote:
This checks overall thread limit
cat 12345 > /proc/sys/kernel/threads-max
Sets the max threads (any number really, be careful though or you'll run out of memory)
The websocket.Dial function is (indirectly) using cgo. Each cgo call
behaves like a syscall. The websocket.Dial() function will try to
allocate C memory when performing address lookup - the memory
allocation (a cgo call) causes the Go runtime to create another OS
thread to handle goroutines. An OS thread (presumably, the newly
created one) calls websocket.Dial() from another goroutine created by
your program, and tries to allocate another piece of C memory, which
in turn creates another OS thread.
This ends up creating too many OS threads.