Node.js - 诊断“(节点)警告:递归process.nextTick检测到。”

时间:2015-07-21 23:04:57

标签: javascript node.js stack-overflow

我的应用程序的情况是,操作需要很长时间才能完成,然后打印以下消息的几百个实例:

(node) warning: Recursive process.nextTick detected. This will break in the next version of node. Please use setImmediate for recursive deferral.

然后应用程序崩溃:

RangeError: Maximum call stack size exceeded

没有堆栈跟踪。

如何诊断此类错误?例如,我想知道哪个函数调用先于RangeError

要复制错误,请使用以下内容在文件node foo.js上运行foo.js

var foo = function () {
    process.nextTick(foo);
};
foo();

我对应用程序中此问题的特定原因并不感兴趣,因为我知道如何在节点中诊断此类问题。

节点版本为v0.10.39

1 个答案:

答案 0 :(得分:0)

您可以替换process.nextTick来打印某种有用的信息 - 这些信息的内容如下:

var _nextTick = process.nextTick;
var alreadyLogged = new Set();

process.nextTick = function nextTick(f) {
    if (!alreadyLogged.has(f)) {
        alreadyLogged.add(f);

        process.nextTick = _nextTick;
        console.log(f);
        process.nextTick = nextTick;
    }

    return _nextTick.apply(this, arguments);
};