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

deasync hangs in an asynchronous context #21

Open
sterling opened this Issue Jul 7, 2015 · 14 comments

Comments

Projects
None yet
10 participants
@sterling
Copy link

sterling commented Jul 7, 2015

When using a deasynced function in asynchronous code, deasync hangs and never returns.

var deasync = require('deasync');

function async(cb) {
  setTimeout(function() {
    cb(null, 'value');
  }, 0);
}

function sync() {
  return deasync(async)();
}

console.log('start', sync());
async(function(err, val) {
  console.log('async result', val);
  console.log(sync());
  console.log('done');
});

Notice that when run, the second call to sync hangs. I was originally using promises when testing this and derived the previous code from this:

var Promise = require('q');
var deasync = require('deasync');

function async() {
  return Promise.resolve('value');
}

function sync() {
  var result, done;
  async().then(function(response) {
    result = response;
  }).finally(function() {
    done = true;
  });

  deasync.loopWhile(function() { return !done; });

  return result;
}

console.log('start', sync());
async().then(function(result) {
  console.log('async result', result);
  return sync();
}).then(function(value) {
  console.log(value);
  console.log('done');
});

I also tried the above example using the Bluebird promise library and ended up with the same results. In the promise example, adding a console.log inside the loopWhile handler shows that it is stuck checking done since the promise chain never completes.

@maxvyaznikov

This comment has been minimized.

Copy link

maxvyaznikov commented Jul 10, 2015

Much more simple example:

var deasync = require('deasync');

function async(cb) {
    console.log('1');
    setTimeout(function() {
        console.log('2');
        cb(null, 'value');
        console.log('3');
    }, 0);
}

console.log('A', deasync(async)());
setTimeout(function() {
    console.log('B', deasync(async)());
}, 0);

I had:

$ node test.js 
1
2
3
A value
1
^C

@abbr abbr added the bug label Jul 10, 2015

@abbr

This comment has been minimized.

Copy link
Owner

abbr commented Jul 20, 2015

Although haven't found a fix yet, the problem appears only if the nested setTimeout having same timeout parameter. In @maxvyaznikov 's example, changing one of the timeout param away from 0 and 1 (b/c 0 is treated as 1 by Node.js implementation of setTimeout) will avoid hung. This serves as a temporary workaround.

@marcellodesales

This comment has been minimized.

Copy link

marcellodesales commented Jul 22, 2015

Functions with Bluebird Promises get stuck

I have a bluebird promises implementation of a function that returns the CPS function that hangs as well... https://github.com/marcellodesales/node-pom-parser/blob/master/lib/index.js#L36-L59

If I use the following code, it will just hang:

/**
 * Setup the temporary pom.xml settings as we still use it.
 * TODO: Remove this when we have a registry
 */
Core.prototype._loadPomXmlSettings = function _loadPomXmlSettings() {
  this.APP_POM_PATH = this.APP_DIR + "/pom.xml";

  if (fs.existsSync(this.APP_POM_PATH)) {
    var parser = deasync(pomParser.parse);
    console.log(parser);

    var resp = parser({filePath: this.APP_POM_PATH});
    console.log(resp);
    this.pom = resp.pomObject;
  }
};

This only prints the following:

[Function]

When it should print the object below the resp call.

Functions without it does not

I have implemented others functions using the same CPS pattern without bluebird and it works as expected... Those below work find!!!

https://github.com/marcellodesales/node-newrelic-api/blob/master/src/deployments.js#L90

https://github.com/marcellodesales/node-newrelic-api/blob/master/src/applications.js#L46

@abbr any suggestion for this one?

@abbr

This comment has been minimized.

Copy link
Owner

abbr commented Aug 10, 2015

The problem appears to be a Node bug. Have filed issue #25831 and pr #2830

@marcellodesales

This comment has been minimized.

Copy link

marcellodesales commented Aug 10, 2015

@abbr thanks for following up...

@kjhangiani

This comment has been minimized.

Copy link

kjhangiani commented Sep 14, 2015

Ran into this issue as well, using node 0.12.7, deasync hangs if called within a .then block of an async function. I noticed a PR has been submitted, @abbr, do you know if this has been merged in yet or if there is any way to port the fix?

My specific use case is within a .then block of a sequelize.findAll query (sequelize uses bluebird for promises iirc)

@sterling

This comment has been minimized.

Copy link
Author

sterling commented Feb 22, 2016

@abbr has there been any progress on this?

@abbr

This comment has been minimized.

Copy link
Owner

abbr commented Feb 23, 2016

No. If the issue affects a run time env under your control and there is no workaround, you can compile NodeJS from source with my pr applied (move 1 line of code in lib/timers.js)

@Aminadav

This comment has been minimized.

Copy link

Aminadav commented Mar 21, 2016

@sterling , @maxvyaznikov
The code your provide to reproduce the bug, will work, in my merge.

@dy

This comment has been minimized.

Copy link

dy commented Apr 8, 2016

@abbr could you please react on the PR #52?

I faced that issue in glslify-sync, it is quite acute:

setTimeout(function () {
 var source = glslify('./file.glsl');
});

Just hangs. Setting timeout to 0, 1 does not help. Please, help.
Oh, I see, #52 is not very good.

@abbr you have mentioned this for nodejs/node-v0.x-archive#25831, but is there the same issue for node 4.x, 5.x? because I face this in node 5.5.0

@vellotis

This comment has been minimized.

Copy link

vellotis commented Jun 26, 2016

It's a pity.. Seems that it is thought to be a nofix by node team @ nodejs/node#2830.

@codejamninja

This comment has been minimized.

Copy link

codejamninja commented Jan 24, 2019

Is there any way to work around this bug?

@leo60228

This comment has been minimized.

Copy link

leo60228 commented Jan 24, 2019

@codejamninja

This comment has been minimized.

Copy link

codejamninja commented Jan 24, 2019

Not true. I need it to run some async code on import.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.