Skip to content

Weird behaviour of verify() with setTimeout() #25

Closed

Description

@julien-f

The promise returned by verify() does not resolve before the first timer is up.

Steps to reproduce

// Without this line, verify() takes about 50ms to complete, with it, it takes 1 second.
setTimeout(function () {}, 1e3)

console.time('verify()')

require('argon2').verify('$argon2i$m=4096,t=3,p=1$tbagT6b1YH33niCo9lVzuA$htv/k+OqWk1V9zD9k5DOBi2kcfcZ6Xu3tWmwEPV3/nc', 'password')
  .then(function () {
    console.timeEnd('verify()')
  })

Environment

Operating system: Debian Stretch
Node version: 4.3.0

Activity

  1. ranisalt commented on Feb 25, 2016

    @ranisalt
    Owner

    It's not the first time I see this issue, which seems to be an issue with v8. Apparently, it happens the promise resolve/reject call happens on the next tick of the Node event loop, but setTimeout extends this tick until it the timeout is reached, and only then the event loop ticks.

    There seems to be some workarounds for this but they are really weird and probably not worth the trouble. Care to share a real example where it is an issue?

  2. ranisalt commented on Feb 26, 2016

    @ranisalt
    Owner

    Marked as wontfix at least until there's a clarification on why this happens by v8.

  3. julien-f commented on Feb 26, 2016

    @julien-f
    Author

    I don't know where it comes from, this issue does not appear when using promises directly, neither combine with setImmediate()/setTimeout() or process.nextTick():

    setTimeout(function () {}, 1e3)
    
    console.time('Promise.resolve()')
    Promise.resolve()
      .then(function () {
        console.timeEnd('Promise.resolve()')
      })
    // → Promise.resolve(): 4ms
    
    console.time('setImmediate()')
    new Promise(function (resolve) { setImmediate(resolve, 0)})
      .then(function () {
        console.timeEnd('setImmediate()')
      })
    // → setImmediate(): 7ms
    
    console.time('setTimeout()')
    new Promise(function (resolve) { setTimeout(resolve, 0)})
      .then(function () {
        console.timeEnd('setTimeout()')
      })
    // → setTimeout(): 7ms
    
    console.time('process.nextTick()')
    new Promise(function (resolve) { process.nextTick(resolve)})
      .then(function () {
        console.timeEnd('process.nextTick()')
      })
    // → process.nextTick(): 4ms

    This behavior currently breaks my tests.

  4. ranisalt commented on Feb 26, 2016

    @ranisalt
    Owner

    Oh, I see in your tests this is behavior for 0.9.x, I'll look at the changes from 0.8 and see what possible broken.

  5. julien-f commented on Apr 3, 2016

    @julien-f
    Author

    Is there any news regarding this issue?

    If it's completely broken in C++, maybe you should create promises in the JS wrapper…

  6. ranisalt commented on Apr 7, 2016

    @ranisalt
    Owner

    Locally it worked properly, and I added a timeout test to prevent regression in the future. Could you check?

    (sorry for the delay, life's a bit rushing)

  7. julien-f commented on Apr 7, 2016

    @julien-f
    Author

    I don't seem to be able to build the package from the git repo:

    $ npm i
    
    > argon2@0.10.0 install /tmp/hashy/node_modules/argon2
    > node-gyp rebuild
    
    make: Entering directory '/tmp/hashy/node_modules/argon2/build'
    make: *** No rule to make target 'Release/obj.target/libargon2/argon2/src/argon2.o', needed by 'Release/obj.target/argon2.a'.  Stop.
    make: Leaving directory '/tmp/hashy/node_modules/argon2/build'
    gyp ERR! build error 
    gyp ERR! stack Error: `make` failed with exit code: 2
    gyp ERR! stack     at ChildProcess.onExit (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:276:23)
    gyp ERR! stack     at emitTwo (events.js:87:13)
    gyp ERR! stack     at ChildProcess.emit (events.js:172:7)
    gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:200:12)
    gyp ERR! System Linux 4.2.0-1-amd64
    gyp ERR! command "/usr/local/bin/node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
    gyp ERR! cwd /tmp/hashy/node_modules/argon2
    gyp ERR! node -v v4.4.1
    gyp ERR! node-gyp -v v3.3.1
    gyp ERR! not ok 
    
  8. ranisalt commented on Apr 7, 2016

    @ranisalt
    Owner

    You don't have the submodule. Clone with --recursive or run git submodule update --init.

  9. julien-f commented on Apr 7, 2016

    @julien-f
    Author

    My bad :)
    Maybe it could be in the README ;)

    It works! Thanks for the fix.

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions