在节点 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>>