Spawn 1000 websockets in separate goroutines

118 views
Skip to first unread message

Vladimir Mihailenco

unread,
Nov 21, 2011, 11:56:19 AM11/21/11
to golang-nuts
I have following code which spanws 1000 websockets in separate
goroutines (i.e. I want to test server with 1000 open websockets):

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?

unread,
Nov 21, 2011, 12:59:11 PM11/21/11
to golang-nuts
The program creates 1000 OS threads.

On Nov 21, 5:56 pm, Vladimir Mihailenco <vladimir.web...@gmail.com>
wrote:

si guy

unread,
Nov 21, 2011, 1:13:52 PM11/21/11
to golan...@googlegroups.com
echo /proc/sys/kernel/threads-max

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)

unread,
Nov 21, 2011, 4:08:23 PM11/21/11
to golang-nuts
According to the stack-trace:

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.

Reply all
Reply to author
Forward
0 new messages