Can’t create thread (11) (ThreadError) in Ruby

I have been working on some networked code in Ruby which uses EventMachine. This is part of my work as a Research Associate at the University of Birmingham. I recently had a headache with threading/processes whereby I would get this error during thread creation:

Here is the code I was running:

I knew I wasn’t creating too many threads and I knew I wasn’t running out of memory. After some serious digging, and two hundred browser tabs later,  I found the reason: zombie/defunct processes.

The ’11’ in the error code is actually not from Ruby at all, but from the  pthread class which is used to create new threads in Linux (among other OSes). This is the numerical value for the error code EAGAIN, returned by the function pthread_create, which occurs when:

Insufficient resources to create another thread, or a system-imposed limit on the number of threads was encountered

I had used Ruby’s Thread.list.size to ensure I wasn’t using too many threads, so what could it be?

Continue reading »