# Node.js callbacks causing stack overflow

**URL:** <https://forums.blinkstick.com/t/node-js-callbacks-causing-stack-overflow/150>\
**Category:** Support\
**Created:** [March 11, 2015, 4:24pm UTC](https://forums.blinkstick.com/t/node-js-callbacks-causing-stack-overflow/150 "2015-03-11T16:24:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![SkyLined](https://forums.blinkstick.com/user_avatar/forums.blinkstick.com/skylined/32/90_2.png) [@SkyLined](https://forums.blinkstick.com/u/SkyLined)\
**Post date:** [March 11, 2015, 4:24pm UTC](https://forums.blinkstick.com/t/node-js-callbacks-causing-stack-overflow/150/1 "2015-03-11T16:24:10Z")

</div>

I’m trying to update my Node.js code to use the callbacks in order to make sure everything is done in the right order, but doing so is causing a stack overflow exception in Node.js, which terminates the process.  
The below program should run forever (and I would not expect to see many errors). However, I see errors quite frequently and after a few thousand loops, Node.js crashes with a stack exhaustion.

```auto
var mBlinkStick = require("blinkstick"),
    oBlinkStick = mBlinkStick.findFirst();
function fSetMode() {
  oBlinkStick.setMode(2, function (oError) {
    console.log("mode set:", oError);
    fSetMode();
  });
};
fSetMode();

```

By adding a timeout, the stack exhaustion is avoided. I still see errors, but the program can run forever.

```auto
var mBlinkStick = require("blinkstick"),
    mColor = require("mColor"),
    oBlinkStick = mBlinkStick.findFirst();
function fSetMode() {
  oBlinkStick.setMode(2, function (oError) {
    console.log("mode set:", oError);
    setTimeout(fSetMode, 0);
  });
};
fSetMode();

```

The errors are not related to this issue, but might be interesting to look into at some point.

I assume this crashes because you are calling the callback from your addon immediately, which would cause a loop, rather than queuing the call to be made after your code is finished. I’m not sure how to resolve this, as I’ve not dealt with callbacks in addon code myself yet.

---

<div class="post-metadata">

**Author:** ![arvydas](https://forums.blinkstick.com/user_avatar/forums.blinkstick.com/arvydas/32/242_2.png) [@arvydas](https://forums.blinkstick.com/u/arvydas)\
**Post date:** [March 11, 2015, 6:34pm UTC](https://forums.blinkstick.com/t/node-js-callbacks-causing-stack-overflow/150/2 "2015-03-11T18:34:01Z")

</div>

I’m looking now into all of the issues you reported.

---

<div class="post-metadata">

**Author:** ![SkyLined](https://forums.blinkstick.com/user_avatar/forums.blinkstick.com/skylined/32/90_2.png) [@SkyLined](https://forums.blinkstick.com/u/SkyLined)\
**Post date:** [March 13, 2015, 4:09pm UTC](https://forums.blinkstick.com/t/node-js-callbacks-causing-stack-overflow/150/3 "2015-03-13T16:09:51Z")

</div>

I noticed that the Node.js documentation for `process.nextTick` actually mentions exactly this problem and explains that to prevent this, you should never do `callback(...)` but use `process.nextTick(function(){callback(...);});` instead. I’ve been rewriting a lot of my code as well 🙂

---

<div class="post-metadata">

**Author:** ![arvydas](https://forums.blinkstick.com/user_avatar/forums.blinkstick.com/arvydas/32/242_2.png) [@arvydas](https://forums.blinkstick.com/u/arvydas)\
**Post date:** [March 13, 2015, 4:24pm UTC](https://forums.blinkstick.com/t/node-js-callbacks-causing-stack-overflow/150/4 "2015-03-13T16:24:45Z")

</div>

That’s a handy thing to know 😃 Time to patch the code too!
