2

I got into the problem of running out of stack memory using Nokogiri's gem and parsing complex pages (SystemStackError: stack level too deep).

I found a couple of threads here that said I need to change the stack size of Rubys VM, but since I am on a Windows 10 laptop, I seem to can't get it to work.

When I open the ruby console and request the variable value I get:

irb> ENV['RUBY_THREAD_VM_STACK_SIZE'] 
=> nil

when typing this, I do get a value for the stack size (name is slightly different)

irb> RubyVM::DEFAULT_PARAMS
=> {:thread_vm_stack_size=>1048576, :thread_machine_stack_size=>1048576, :fiber_vm_stack_size=>131072, :fiber_machine_stack_size=>524288}    

I tryed out to installed the dotenv gem and set the variables in the .envfile:

RUBY_THREAD_VM_STACK_SIZE = 50000000
THREAD_VM_STACK_SIZE = 50000000

I can now see them in my irb> as ENV variables, but the RubyVM::DEFAULT_PARAMS haven't changed.

Any ideas how I can change the VM stack size for my application? Thanks a lot!!

Felix
  • 85
  • 2
  • 6

1 Answers1

1

The reason you cannot use the dotenv gem, is that these variables have to be readable by Ruby at VM initialization. Setting them through dotenv is too late for VM init.

Therefore you have to set the variables properly, so they are available before the Ruby executable is started.

On Windows, environment variables are stored in the registry. You can change them either from the control panel, or from the command line like so:

# Set temporary variable for this command prompt
set RUBY_THREAD_VM_STACK_SIZE=1234567

# Set permanent user-level variable in the registry
setx RUBY_THREAD_VM_STACK_SIZE 1234567

In case you used setx you have to open up a new command prompt after you used it, and you should now have Ruby pick up the new setting.

Casper
  • 33,403
  • 4
  • 84
  • 79
  • Thanks Casper, that worked! For some reasons I still get the same error message though. I pushed it up to 50 MB of stack memory, same issue.. Is it possible that the machine stack size is limiting the VM stack size, so that I need to change that as well? I tried but it seems not to work though the `set` command. – Felix May 28 '17 at 10:50
  • Yes I guess it's possible that system stack size is the limiting factor. I would file an issue with Nokogiri and see if you can get help there. It might be a bug too. – Casper May 28 '17 at 16:13