Nodejs - promises, raw termination and memory leak

seeking help from a nodejs guru there using promises. I have the following test program in which I call the async "q" function, which simply throws an exception. This program runs pretty fast in memory; but the leak disappears if you uncomment the .done () call.

Why does a leak occur when a promise is not broken (i.e., a call to done ())? I tried to execute the documentation , but do not understand the understanding of the done () method. Thanks in advance for your help!

Here is my code:

(function() { var MAX_ITER_COUNT, Q, iterCount, maxMem, noop, qDoit, test; Q = require("q"); iterCount = 0; MAX_ITER_COUNT = 10 * 1000; maxMem = 0; noop = function() {}; qDoit = function() { var currentMem; currentMem = Math.round(process.memoryUsage().heapUsed / 1024 / 1024); if (currentMem > maxMem) { maxMem = currentMem; } console.log("" + iterCount + " - memory is: " + currentMem + "/" + maxMem + " MB"); return Q(10).then(function() { throw new Error("X"); }); }; test = function() { if (iterCount++ > MAX_ITER_COUNT) { console.log("DONE"); return; } // ---- If I uncomment the done() call below the leak goes away ---- return qDoit()["finally"](function() { return setImmediate(test); }) //.done(noop, noop, noop); }; Q.onerror = function() {}; test(); }).call(this); 
+6
source share
1 answer

Answering my own question, I hope this helps someone.

When digging bits into q library code, it looks like all unhandled exceptions are put into an array called unhandledRejections by default. Not sure why this was implemented like this, but apparently to help developers track exceptions without handling. This behavior can be changed by calling Q.stopUnhandledRejectionTracking() . When I did this, the memory leak disappeared, even without calling .done() .

+5
source

Source: https://habr.com/ru/post/978550/


All Articles