question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Long chain promise => Out of memory

See original GitHub issue

Bluebird 3.3.4 and node v5.10.1.

I’m not sure if this is a bug or an expected behaviour that the earlier resolved promised in a chain doesn’t get collected up in GC. This might even be a node problem.

var Promise = require('bluebird')

function add(x){
  return x+1
}

var a = Promise.resolve(0)
for(var i=0;i<10e8;i++){
  a = a.then(add)
}

node screams with message like this

<--- Last few GCs --->

    7440 ms: Scavenge 1413.8 (1458.1) -> 1413.8 (1458.1) MB, 24.1 / 0 ms (+ 0.9 ms in 1 steps since last GC) [allocation failure] [incremental marking delaying mark-sweep].
    8138 ms: Mark-sweep 1413.8 (1458.1) -> 1413.8 (1458.1) MB, 698.2 / 0 ms (+ 0.9 ms in 1 steps since start of marking, biggest step 0.9 ms) [last resort gc].
    8829 ms: Mark-sweep 1413.8 (1458.1) -> 1413.8 (1458.1) MB, 691.4 / 0 ms [last resort gc].


<--- JS stacktrace --->

==== JS stack trace =========================================

Security context: 0x3d33367e3ac1 <JS Object>
    1: _then [xxx/node_modules/bluebird/js/release/promise.js:~217] [pc=0x1df6cf8b05d6] (this=0x35cfdd004101 <a Promise with map 0x1c4d6bb33c21>,didFulfill=0x2597f0bb95a9 <JS Function add (SharedFunctionInfo 0x2597f0b4ae79)>,didReject=0x3d3336704189 <undefined>,_=0x3d3336704189 <undefined>,receiver=0x3d3336704189 <undefined>,internalData=0x3d...

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - process out of memory
Abort trap: 6

I know I could use each and such. But, the specific problem I’m trying to solve will involve stream of random data which I do not want to store the entire thing in a memory which will need to put in to db.

Is there a way to get around this?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
petkaantonovcommented, Apr 12, 2016

No nextTick doesn’t mean the callbacks are called, only the callback being called means the callback has been called. setImmediate scheduler is used in node so the same code probably works with a setTimeout rather than nextTick which won’t allow setImmediate to trigger.

0reactions
piti118commented, Apr 12, 2016

For unsuspecting passerby in the future, this one works (very slowly), but it works. (One could group it so setImmediate doesn’t get called so often)

var Promise = require('bluebird')

function add(x){
  return x+1
}

var a = Promise.resolve(0)
function f(i){
  if(i>0){
    a=a.then(add)
    setImmediate(()=>f(i-1))
    if(i%100000==0){console.log(i)}
  }else{
    a.then(console.log)
  }
}

f(10e7)
Read more comments on GitHub >

github_iconTop Results From Across the Web

JavaScript Promise Chaining Memory Leak Pattern - Cribl
In this post we'll walk through a memory leak pattern we recently encountered when using Javascript Promises.
Read more >
How do you know when an indefinitely long promise chain ...
I'm being told that a "good" promise implementation would not cause accumulating memory from an indefinitely growing promise chain.
Read more >
chain of never resolved promises create memory leaks #179
If we build a long/infinite promise chain gradually without the recursive code pattern, and only keep reference to the tail of the chain,...
Read more >
Introduction - Alexandru Nedelcu
But It Does Unlimited Chaining of Promises; 3. ... This piece of code will leak memory and eventually crash your Node.js process or...
Read more >
Using promises - JavaScript - MDN Web Docs
With this pattern, you can create longer chains of processing, where each promise represents the completion of one asynchronous step in the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found