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:
1 |
Can't create thread (11) (ThreadError) in Ruby |
Here is the code I was running:
1 2 3 4 5 6 7 8 9 10 11 |
# Launch a child process fork do create_server end ... # Somewhere inside create_server Thread.new do # Do some work end |
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?