4

longjohn次のコードを使用すると、間違ったスタック トレースが取得されます。setTimeoutから呼び出されていることが示されてfirstfunctionいますが、実際には最初の関数が実行される前にプログラムがクラッシュしています。

ここでチケットを作成しました https://github.com/mattinsler/longjohn/issues/16

var longjohn = require("longjohn");

setTimeout(function () {
    throw new Error();
}, 10);


setTimeout(function () {
    firstfunction();
}, 10000);


var firstfunction = function () {
    setTimeout(function () {
        console.log("First function");
    }, 10);
}

スタックトレース

/home/jeevan/node_js/node_modules/longjohn/dist/longjohn.js:181
        throw e;
              ^
Error
    at firstfunction (/home/jeevan/node_js/longjohn.js:11:11)
    at listOnTimeout (timers.js:110:15)
---------------------------------------------
    at Object.<anonymous> (/home/jeevan/node_js/longjohn.js:10:1)
    at Module._compile (module.js:456:26)
    at Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Module._load (module.js:312:12)
    at Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:901:3

私の質問は、何が問題になるのか、それを修正する方法です。

4

1 に答える 1

1

なくても起こりlongjohnます。

理由は正確にはわかりませんが、コールバックに名前を付けると、よりうまく機能します。

setTimeout(function MyFirstTimeoutCallback() {
  throw new Error();
}, 10);
...

これにより、次のトレースバックが生成されます。

/private/tmp/node_modules/longjohn/dist/longjohn.js:181
        throw e;
              ^
Error
    at MyFirstTimeoutCallback (/private/tmp/lj.js:4:9) <-- much better!
    at listOnTimeout (timers.js:110:15)
---------------------------------------------
    at Object.<anonymous> (/private/tmp/lj.js:3:1)
    at Module._compile (module.js:456:26)
    at Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Module._load (module.js:312:12)
    at Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:901:3

編集:このバグはノード 0.10 で (どこかで) 導入されたようです。0.8.23 でテストすると、問題ないように見えます。

timers.js:103
            if (!process.listeners('uncaughtException').length) throw e;
                                                                      ^
Error
    at Object.<anonymous> (/private/tmp/lj.js:4:9) <-- correct
    at list.ontimeout (timers.js:101:19)
    ...

(バグレポート)

編集 #2:これは V8 で確認されたバグです (リンクされたバグ レポートを参照してください)。

于 2013-06-11T11:08:35.813 に答える