Issue #16958 has been updated by jeremyevans0 (Jeremy Evans).

Status changed from Open to Feedback

I can recreate this if I do `SecureRandom.singleton_class.send(:remove_method, :gen_random)` or `SecureRandom.singleton_class.prepend Random::Formatter.dup`.  Is it possible something like that is happening in your environment?  In either case, it isn't a bug in SecureRandom.

If you could put together a small example showing the problem without using any gems, that would be helpful for debugging.

----------------------------------------
Bug #16958: bytes: stack level too deep (SystemStackError)
https://bugs.ruby-lang.org/issues/16958#change-86129

* Author: mikwat (Michael Watts)
* Status: Feedback
* Priority: Normal
* ruby -v: 2.7.1
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
Although I can't reliably reproduce this error, it appears that `SecureRandom.base64` sometimes lands in an infinite loop. Here's the stack trace in our environment:

```
/opt/ui/vendor/ruby-2.7.1/lib/ruby/2.7.0/securerandom.rb:73:in `bytes': stack level too deep (SystemStackError)
from /opt/ui/vendor/ruby-2.7.1/lib/ruby/2.7.0/securerandom.rb:260:in `gen_random'
from /opt/ui/vendor/ruby-2.7.1/lib/ruby/2.7.0/securerandom.rb:73:in `bytes'
from /opt/ui/vendor/ruby-2.7.1/lib/ruby/2.7.0/securerandom.rb:260:in `gen_random'
from /opt/ui/vendor/ruby-2.7.1/lib/ruby/2.7.0/securerandom.rb:73:in `bytes'
from /opt/ui/vendor/ruby-2.7.1/lib/ruby/2.7.0/securerandom.rb:260:in `gen_random'
from /opt/ui/vendor/ruby-2.7.1/lib/ruby/2.7.0/securerandom.rb:73:in `bytes'
from /opt/ui/vendor/ruby-2.7.1/lib/ruby/2.7.0/securerandom.rb:260:in `gen_random'
from /opt/ui/vendor/ruby-2.7.1/lib/ruby/2.7.0/securerandom.rb:73:in `bytes'
... 10908 levels...
from /opt/ui/vendor/bundle/ruby/2.7.0/gems/mongo-2.12.1/lib/mongo/server/connection_pool/populator.rb:46:in `do_work'
from /opt/ui/vendor/bundle/ruby/2.7.0/gems/mongo-2.12.1/lib/mongo/background_thread.rb:101:in `block (2 levels) in start!'
from /opt/ui/vendor/bundle/ruby/2.7.0/gems/mongo-2.12.1/lib/mongo/background_thread.rb:99:in `catch'
from /opt/ui/vendor/bundle/ruby/2.7.0/gems/mongo-2.12.1/lib/mongo/background_thread.rb:99:in `block in start!'
```

`SecureRandom.base64` was called from the mongo gem here:
https://github.com/mongodb/mongo-ruby-driver/blob/v2.12.1/lib/mongo/auth/scram/conversation.rb#L232

What I don't understand: from the stack trace it appears the definition of `gen_random` from the base module `Random::Formatter` is getting called instead of the definition from the sub module `SecureRandom`, resulting in an infinite loop.






-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>