0

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

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

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

我不知道为什么会发生这种情况,但我知道它仅在我尝试修改/删除快照引用处的对象时发生。

这是一个独立的 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>>
4

2 回答 2

1

即使您没有处理它,对它的调用remove()仍然是基于异步/承诺的,并生成一个要在其中运行的上下文。承诺上下文相当大,您在这里用完堆栈也就不足为奇了。如果您真的需要这样的模式才能正常工作,您可以批量更新 -child_added将值插入“待删除”数组,然后将该数组作为单独的任务一次处理一批条目,直到它为空. BlueBird ( http://bluebirdjs.com/ ) 库中有很多辅助方法可用于处理数组和 Promises (例如map/ mapSeries)。

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

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

但我相信你最好的方法是构建你的程序,以尽量减少这种删除模式的堆栈使用。增加筹码量总是会让你对“现在足够大吗?”持开放态度。问题。

于 2017-02-14T19:08:57.023 回答
0

最小化分配给堆栈的内存。例如函数内部的静态数组,而不是使用动态数组。

于 2017-12-13T01:58:58.013 回答