Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

infinite loop with crypto.randomBytes() causing 100% CPU hang #813

Closed
edef1c opened this issue Feb 12, 2015 · 12 comments
Closed

infinite loop with crypto.randomBytes() causing 100% CPU hang #813

edef1c opened this issue Feb 12, 2015 · 12 comments
Labels
crypto Issues and PRs related to the crypto subsystem.

Comments

@edef1c
Copy link
Contributor

edef1c commented Feb 12, 2015

var crypto = require('crypto')
while (true)
  console.log(crypto.randomBytes(32).toString('hex'))

The process prints a bunch of random numbers (yay!) and then hangs badly (:cry:) at 100% CPU.
I'm not sure how to debug this.

@edef1c
Copy link
Contributor Author

edef1c commented Feb 12, 2015

For reference, the equivalent async program runs fine:

var crypto = require('crypto')

;(function f() {
  crypto.randomBytes(32, function(err, bytes) {
    if (err) throw err
    console.log(bytes.toString('hex'))
    f()
  })
})()

@edef1c
Copy link
Contributor Author

edef1c commented Feb 12, 2015

It actually appears to hang outside the crypto.randomBytes call:

var crypto = require('crypto')

for (var i = 0;; i++) {
  console.log(i + '+')
  crypto.randomBytes(32)
  console.log(i + '-')
}

This always hangs at a - line (often without printing a newline, oddly enough, though the rest of the line always comes through fine)
The amount of iterations after which it hangs appears to be fairly… random.

@brendanashworth
Copy link
Contributor

What sort of system are you running on? crypto.randomBytes will hang if the system does not have enough entropy to generate the cryptographically secure data. However, it shouldn't hang for long. Does it hang indefinitely?

@brendanashworth brendanashworth added crypto Issues and PRs related to the crypto subsystem. confirmed-bug Issues with confirmed bugs. labels Feb 12, 2015
@micnic
Copy link
Contributor

micnic commented Feb 12, 2015

@nathan7 , I do not understand what do you expect from an infinite loop, calling synchronously and indefinitely console.log() and crypto.randomBytes() will block the garbage collector to do its job, will eat up all your memory and in the end it will hang

@Fishrock123 Fishrock123 removed the confirmed-bug Issues with confirmed bugs. label Feb 12, 2015
@Fishrock123 Fishrock123 changed the title synchronous crypto.randomBytes() causing 100% CPU hang infinite loop with crypto.randomBytes() causing 100% CPU hang Feb 12, 2015
@Fishrock123
Copy link
Contributor

Please see the note here: https://iojs.org/api/crypto.html#crypto_crypto_randombytes_size_callback

Your process is blocking and stalling because you are infinitely looping over it. crypto.randomBytes() does some under-the-hood stuff to get cryptographically strong random data, and will fail in this case. I don't think this is avoidable. Don't infinitely loop over it. :)

@edef1c
Copy link
Contributor Author

edef1c commented Feb 14, 2015

I can get plenty of entropy from /dev/random with other methods, without getting 100% CPU usage.
Blocking is fine, it's what I asked for.

@micnic
Copy link
Contributor

micnic commented Feb 14, 2015

@nathan7 , please try this code on your machine:

while (true) {
  console.log('1234567890');
}

and make your conclusions, the problem is not in crypto.randomBytes() it is because you are using an infinite loop, if you need more data just increase the value of the needed random bytes

@rlidwka
Copy link
Contributor

rlidwka commented Feb 14, 2015

Does crypto.pseudoRandomBytes make any difference?

@micnic
Copy link
Contributor

micnic commented Feb 14, 2015

@edef1c
Copy link
Contributor Author

edef1c commented Feb 15, 2015

@Fishrock123
Welp, my bad. That definitely shouldn't lead to indefinite lockups, though?

@Fishrock123
Copy link
Contributor

@nathan7 an infinite loop will... infinitely loop. As fast as possible. I'm not really sure what you expect..?

Type while(true); into any web browser console and see what happens. They all lock up.

@edef1c
Copy link
Contributor Author

edef1c commented Feb 15, 2015

@Fishrock123
They're supposed to keep executing the loop body indefinitely, not lock up without the body executing ever again.
while(true); is indeed "please lock up, possibly using 100% CPU".
"please give me a never-ending stream of random numbers" is a fine thing to ask a computer. It's allowed to pause waiting for entropy. Hanging at 100% CPU doing nothing useful is a rather different matter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crypto Issues and PRs related to the crypto subsystem.
Projects
None yet
Development

No branches or pull requests

5 participants