Projects & Stories

SystemStackError: stack level too deep

I just had some trouble running a intense ruby task. The result was following error:

SystemStackError: stack level too deep  
/home/diaspora/diaspora/app/models/aspect_membership.rb:13:in `block in <class:AspectMembership>'
/home/diaspora/diaspora/app/models/user/connecting.rb:46:in `remove_contact'
/home/diaspora/diaspora/app/models/user/connecting.rb:62:in `disconnect'
/home/diaspora/diaspora/app/models/aspect_membership.rb:14:in `block in <class:AspectMembership>'

This can happen if you run into recursion like this:

def name  
  @name = name
end  

The compiler assigns the function to the variable which results into a infinity loop and at some point you will hit the system resource limit:

user@3a29f5ee5ccb:~/diaspora$ ulimit -a  
[...]
stack size              (kbytes, -s) 8191  
[...]

You can simply fix it by adjusting your code:

def name=(name)  
  @name = name
end  

But in my case it was just an expensive job and then you have to increase the mentioned system resource limit. I set the stack size temporarily to unlimited:

root@3a29f5ee5ccb:~/$ ulimit -s unlimited  

If you use docker its a little bit tricky to set the ulimit.
For older version which not support the --ulimit <type>=<soft>:<hard> parameter you have to adjust the host limits, restart the docker daemon and the container:

service docker stop  
# adjust /etc/security/limits.conf if it should be permanent
ulimit -s unlimited  
service docker start  
docker restart container  
# log into your container and verify the correct limit
ulimit -a  

//edit: Please reset the resource limit to its original value or adjust it, but setting it permanently to unlimited is probably not the best solution.