镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

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

Description

@edef1c
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.

Activity

  1. edef1c commented on Feb 12, 2015

    @edef1c
    ContributorAuthor

    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()
      })
    })()
  2. edef1c commented on Feb 12, 2015

    @edef1c
    ContributorAuthor

    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.

  3. brendanashworth commented on Feb 12, 2015

    @brendanashworth
    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?

  4. added
    cryptoIssues and PRs related to the crypto subsystem.
    confirmed-bugIssues and PRs for confirmed bugs.
    on Feb 12, 2015
  5. micnic commented on Feb 12, 2015

    @micnic
    Contributor

    @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

  6. changed the title [-]synchronous crypto.randomBytes() causing 100% CPU hang[/-] [+]infinite loop with crypto.randomBytes() causing 100% CPU hang[/+] on Feb 12, 2015
  7. Fishrock123 commented on Feb 12, 2015

    @Fishrock123
    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. :)

  8. edef1c commented on Feb 14, 2015

    @edef1c
    ContributorAuthor

    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.

  9. micnic commented on Feb 14, 2015

    @micnic
    Contributor

    @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

  10. rlidwka commented on Feb 14, 2015

    @rlidwka
    Contributor

    Does crypto.pseudoRandomBytes make any difference?

  11. micnic commented on Feb 14, 2015

    @micnic
    Contributor
  12. edef1c commented on Feb 15, 2015

    @edef1c
    ContributorAuthor

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

  13. Fishrock123 commented on Feb 15, 2015

    @Fishrock123
    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.

  14. edef1c commented on Feb 15, 2015

    @edef1c
    ContributorAuthor

    @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

Metadata

Metadata

Assignees

No one assigned

    Labels

    cryptoIssues and PRs related to the crypto subsystem.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions