修改当前引用导致最大堆栈大小超出崩溃

Modifying current reference causes maximum stack size exceeded crash

在节点 js 中,使用 'firebase-admin' SDK 的 4.1.0 版,我有一个侦听器,它侦听我的数据库中的消息队列引用,处理消息,然后尝试将其从队列中删除参考。

当我在启动脚本之前队列中的记录数超过一定数量(在我的机器上为 1354 条)时,脚本崩溃并出现最大调用堆栈超出错误。

奇怪的是,只有当我在脚本启动之前队列中有 1354+ 个值时才会发生这种情况。低于此值,问题就会消失。

我不知道为什么会这样,但我知道只有当我尝试 modify/remove 快照引用中的对象时才会发生。

这是一个独立的mcve,问题区域在评论中被标记:

var admin = require("firebase-admin");

var serviceAccount = require("<ADMIN JSON FILE PATH GOES HERE>");

admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    databaseURL: "<FIREBASE URL GOES HERE>"
});

var ref = admin.database().ref();

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// the number of messages to generate for the queue. when this is >= 1354 (on my machine) the program crashes, if it's less than that,
// it works perfectly fine; your tipping point may vary
var amount = 1354;
// message payload to deliver to the queue <amount> times
var payload = {};

// message generation loop
for (i = 0; i < amount; i++) {
    var message = {msg: "hello"};
    payload['message-queue/' + ref.push().key] = message;
}

// add the generated messages simultaneously to message-queue
ref.update(payload).then(function () {

    // 'on child added' listener that causes the crash of the program when there are 1354+ pre-existing messages in the queue prior to application start
    ref.child('message-queue').on('child_added', function(snapshot) {

        var msgKey = snapshot.key;
        var msgContents = snapshot.val().msg

        // do something with msgContents (e.g. sanitize message and deliver to some user's message-received node in the firebase)

        // ***THIS*** is what causes the crash. if you remove this line of code, the program does not crash. it seems that any
        // modification/removal to/of the current <msgKey> node does the same
        ref.child('message-queue').child(msgKey).remove();
    });
});

这里是崩溃的堆栈跟踪:

FIREBASE WARNING: Exception was thrown by user callback. RangeError: Maximum call stack size exceeded

    at RegExp.exec (native)
    at RegExp.test (native)
    at tc (<MY_PROJECT_PATH>\node_modules\firebase-admin\lib\database\database.js:63:86)
    at ub (<MY_PROJECT_PATH>\node_modules\firebase-admin\lib\database\database.js:60:136)
    at vb (<MY_PROJECT_PATH>\node_modules\firebase-admin\lib\database\database.js:43:1228)
    at Xb.h.remove (<MY_PROJECT_PATH>\node_modules\firebase-admin\lib\database\database.js:52:44)
    at Xb.h.remove (<MY_PROJECT_PATH>\node_modules\firebase-admin\lib\database\database.js:52:136)
    at Xb.h.remove (<MY_PROJECT_PATH>\node_modules\firebase-admin\lib\database\database.js:52:136)
    at Xb.h.remove (<MY_PROJECT_PATH>\node_modules\firebase-admin\lib\database\database.js:52:136)
    at Xb.h.remove (<MY_PROJECT_PATH>\node_modules\firebase-admin\lib\database\database.js:52:136)

<MY_PROJECT_PATH>\node_modules\firebase-admin\lib\database\database.js:63
(d="0"+d),c+=d;return c.toLowerCase()}var zc=/^-?\d{1,10}$/;function tc(a){retur
n zc.test(a)&&(a=Number(a),-2147483648<=a&&2147483647>=a)?a:null}function Ac(a){
try{a()}catch(b){setTimeout(function(){N("Exception was thrown by user callback.
",b.stack||"");throw b;},Math.floor(0))}}function Bc(a,b,c){Object.definePropert
y(a,b,{get:c})}function Cc(a,b){var c=setTimeout(a,b);"object"===typeof c&&c.unr
ef&&c.unref();return c};function Dc(a){var b={},c={},d={},e="";try{var f=a.split
("."),b=bb(hc(f[0])||""),c=bb(hc(f[1])||""),e=f[2],d=c.d||{};delete c.d}catch(g)
{}return{wg:b,Ge:c,data:d,mg:e}}function Ec(a){a=Dc(a);var b=a.Ge;return!!a.mg&&
!!b&&"object"===typeof b&&b.hasOwnProperty("iat")}function Fc(a){a=Dc(a).Ge;retu
rn"object"===typeof a&&!0===y(a,"admin")};function Gc(a,b,c){this.type=Hc;this.s
ource=a;this.path=b;this.children=c}Gc.prototype.Jc=function(a){if(this.path.e()
)return a=this.children.sub

RangeError: Maximum call stack size exceeded
    at RegExp.exec (native)
    at RegExp.test (native)
    at tc (<MY_PROJECT_PATH>\node_modules\firebase-admin\lib\database\database.js:63:86)
    at ub (<MY_PROJECT_PATH>\node_modules\firebase-admin\lib\database\database.js:60:136)
    at vb (<MY_PROJECT_PATH>\node_modules\firebase-admin\lib\database\database.js:43:1228)
    at Xb.h.remove (<MY_PROJECT_PATH>\node_modules\firebase-admin\lib\database\database.js:52:44)
    at Xb.h.remove (<MY_PROJECT_PATH>\node_modules\firebase-admin\lib\database\database.js:52:136)
    at Xb.h.remove (<MY_PROJECT_PATH>\node_modules\firebase-admin\lib\database\database.js:52:136)
    at Xb.h.remove (<MY_PROJECT_PATH>\node_modules\firebase-admin\lib\database\database.js:52:136)
    at Xb.h.remove (<MY_PROJECT_PATH>\node_modules\firebase-admin\lib\database\database.js:52:136)

<MY_PROJECT_PATH>>

<MY_PROJECT_PATH>>

<MY_PROJECT_PATH>>

即使你没有处理它,对 remove() 的调用仍然是 async/promise-based 并生成一个到 运行 的上下文。Promise 上下文相当大,它不是令人惊讶的是你 运行ning out out here。如果您确实需要这样的模式才能正常工作,您可以批量更新 - 让 child_added 将值插入 "to be deleted" 数组,然后一次处理该数组的一批条目作为一个单独的条目任务,直到它为空。 BlueBird (http://bluebirdjs.com/) 库中有很多用于处理数组和 Promises 的辅助方法,可以帮助解决这个问题(例如 map/mapSeries)。

这并不是真正的 Firebase 问题 - 每个其他 VM(PHP、Java 等)也有堆栈大小限制需要处理。与大多数其他人一样,V8 是可调的,如果需要,您可以使用如下命令查询(和调整)它:

node --v8-options | grep -B0 -A1 stack_size

但我相信您最好的方法是构建您的程序以尽量减少此删除模式的堆栈使用。增加筹码量总是会让您面临 "is it big enough now?" 问题。

最小化分配给 stack.for 函数内示例静态数组的内存,而不是使用动态数组。