From d61eb13a6c14ef00b44f5876264ee1c30ae166c4 Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Tue, 10 Feb 2015 17:28:58 +0000 Subject: [PATCH 1/7] Add unhandled rejection tracking --- q.js | 93 ++++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 63 insertions(+), 30 deletions(-) diff --git a/q.js b/q.js index 49e945a2..7c8d7483 100644 --- a/q.js +++ b/q.js @@ -91,6 +91,7 @@ var nextTick =(function () { var flushing = false; var requestTick = void 0; var isNodeJS = false; + var laterQueue = []; // queue for late tasks function flush() { /* jshint loopfunc: true */ @@ -105,43 +106,50 @@ var nextTick =(function () { head.domain = void 0; domain.enter(); } + runSingle(task); - try { - task(); - - } catch (e) { - if (isNodeJS) { - // In node, uncaught exceptions are considered fatal errors. - // Re-throw them synchronously to interrupt flushing! - - // Ensure continuation if the uncaught exception is suppressed - // listening "uncaughtException" events (as domains does). - // Continue in next event to avoid tick recursion. - if (domain) { - domain.exit(); - } - setTimeout(flush, 0); - if (domain) { - domain.enter(); - } + } + while(laterQueue.length){ + var fn = laterQueue.pop(); + runSingle(fn); + } + flushing = false; + } - throw e; + function runSingle(task){ // runs a single function in the async queue + try { + task(); - } else { - // In browsers, uncaught exceptions are not fatal. - // Re-throw them asynchronously to avoid slow-downs. - setTimeout(function() { - throw e; - }, 0); + } catch (e) { + if (isNodeJS) { + // In node, uncaught exceptions are considered fatal errors. + // Re-throw them synchronously to interrupt flushing! + + // Ensure continuation if the uncaught exception is suppressed + // listening "uncaughtException" events (as domains does). + // Continue in next event to avoid tick recursion. + if (domain) { + domain.exit(); + } + setTimeout(flush, 0); + if (domain) { + domain.enter(); } - } - if (domain) { - domain.exit(); + throw e; + + } else { + // In browsers, uncaught exceptions are not fatal. + // Re-throw them asynchronously to avoid slow-downs. + setTimeout(function() { + throw e; + }, 0); } } - flushing = false; + if (domain) { + domain.exit(); + } } nextTick = function (task) { @@ -203,7 +211,13 @@ var nextTick =(function () { setTimeout(flush, 0); }; } - + nextTick.runAfter = function(task){ + laterQueue.push(task); + if (!flushing) { + flushing = true; + requestTick(); + } + }; return nextTick; })(); @@ -997,6 +1011,7 @@ Promise.prototype.isRejected = function () { // shimmed environments, this would naturally be a `Set`. var unhandledReasons = []; var unhandledRejections = []; +var reportedUnhandledRejections = []; var trackUnhandledRejections = true; function resetUnhandledRejections() { @@ -1012,6 +1027,14 @@ function trackRejection(promise, reason) { if (!trackUnhandledRejections) { return; } + if(typeof process === "object" && typeof process.emit === "function"){ + Q.nextTick.runAfter(function(){ + if(array_indexOf(unhandledRejections, promise) !== -1){ + process.emit("unhandledRejection", reason, promise); + reportedUnhandledRejections.push(promise); + } + }); + } unhandledRejections.push(promise); if (reason && typeof reason.stack !== "undefined") { @@ -1028,6 +1051,16 @@ function untrackRejection(promise) { var at = array_indexOf(unhandledRejections, promise); if (at !== -1) { + if(typeof process === "object" && typeof process.emit === "function"){ + Q.nextTick.runAfter(function(){ + var atReport = array_indexOf(reportedUnhandledRejections, promise); + if(atReport !== -1) { + process.emit("rejectionHandled", unhandledReasons[at], promise); + reportedUnhandledRejections.splice(atReport, 1); + } + + }); + } unhandledRejections.splice(at, 1); unhandledReasons.splice(at, 1); } From 153dd4d12f2bad92e2c9bddfe1ff7eba684fdb08 Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Fri, 13 Feb 2015 15:31:29 +0200 Subject: [PATCH 2/7] whitespace convention --- .DS_Store | Bin 0 -> 8196 bytes q.js | 17 ++++++++++------- spec/.DS_Store | Bin 0 -> 6148 bytes 3 files changed, 10 insertions(+), 7 deletions(-) create mode 100644 .DS_Store create mode 100644 spec/.DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..35fcd11212309225cebf90df915fb31b628a8653 GIT binary patch literal 8196 zcmeHM-EPw`6h7X%EnO#3{-jCVH(Vel?8-ileLnk~&rY0^5)mtRtlLEMM5G~ena`m} zNxYxuu{062T!Cc36E*3G3~JM1)g9AT4T=H9fMP%~pcqgL{0j`=%;rfu=iHaQs#FXp z2L4M1`13(W=CTyYzLet8fkK1;kQp?KggW8?fzcybiez6(0mYaydSDThMIi>0aP;e3 z9kLY3zLXM9Ou~spGqWf{VPbaRbfHeHB&8}91B!uh26*hAC6jFO=pY`y`v<1$o9N{? z)E(rT1CdZ8nx!?mN2|0+cd0@(T14D$Row*U`v~R|Fb{y(p>3*CnI5866(gje+4cx} znk?8DkV6gfk~6U%Bf1(iPw*Y$h+5=wR8n7f^E&3u9K2{#2MmPt3^Q=dB0V3&agXA- z8M+S-*1*6fIH)9N5gx>~=U|Nijt@~b&SoG#)tfjjLoZUT!J3GN-o#)4yNITQUJG6{ zl9mwcW?IZ&sTt7ms3x~Lp{Zfa4s7K)g%QqtLO+YOtzciQ(*|V9x^nK>Nz9fUn76@t z9c#h@@Xt$t$kn~j8e*!L8#K}<4>;|o9KmRgYt`meyOF!AfWM*o5M$69T z_%DBX_DQYjcWa>$c7n3eek`sW%QmC>mJv43>N~#g4(o=w>)73T{@R-Fg@)tV?HF`_ z&+#n3TlPDi73|mKczcc&HtYH8TW$Lo&@IEWcb&j79oGq8YFYlP=jTsP3x$;hy|7&D zE$FAk;?jb?TqyQ>`OlhmC0EK4Twc2Bwdx)!}1$yLqd*g zZO`?+2HLoOj&`wN+t|icag(Or(qg$|%K)G_g(21XKcrbz6ayE7fk|oRJdgh?=YRjd z7|tjw6$6TaKVpDo)@$o!K+oPBMn~bNypH@7nJ2E-mr_7bBwhq?J^aHEbsef4Q;K9? TO0*!U4*`M(RVW7jDg(a(Q;9Ga literal 0 HcmV?d00001 diff --git a/q.js b/q.js index 7c8d7483..59bf6faa 100644 --- a/q.js +++ b/q.js @@ -91,7 +91,8 @@ var nextTick =(function () { var flushing = false; var requestTick = void 0; var isNodeJS = false; - var laterQueue = []; // queue for late tasks + // queue for late tasks, used by unhandled rejection tracking + var laterQueue = []; function flush() { /* jshint loopfunc: true */ @@ -211,6 +212,9 @@ var nextTick =(function () { setTimeout(flush, 0); }; } + // runs a task after all other tasks have been run + // this is useful for unhandled rejection tracking that needs to happen + // after all `then`d tasks have been run. nextTick.runAfter = function(task){ laterQueue.push(task); if (!flushing) { @@ -1027,9 +1031,9 @@ function trackRejection(promise, reason) { if (!trackUnhandledRejections) { return; } - if(typeof process === "object" && typeof process.emit === "function"){ - Q.nextTick.runAfter(function(){ - if(array_indexOf(unhandledRejections, promise) !== -1){ + if(typeof process === "object" && typeof process.emit === "function") { + Q.nextTick.runAfter(function() { + if(array_indexOf(unhandledRejections, promise) !== -1) { process.emit("unhandledRejection", reason, promise); reportedUnhandledRejections.push(promise); } @@ -1051,14 +1055,13 @@ function untrackRejection(promise) { var at = array_indexOf(unhandledRejections, promise); if (at !== -1) { - if(typeof process === "object" && typeof process.emit === "function"){ - Q.nextTick.runAfter(function(){ + if(typeof process === "object" && typeof process.emit === "function") { + Q.nextTick.runAfter(function() { var atReport = array_indexOf(reportedUnhandledRejections, promise); if(atReport !== -1) { process.emit("rejectionHandled", unhandledReasons[at], promise); reportedUnhandledRejections.splice(atReport, 1); } - }); } unhandledRejections.splice(at, 1); diff --git a/spec/.DS_Store b/spec/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..54f4f53b0fad618bf2d4c94528db59309b4dd605 GIT binary patch literal 6148 zcmeHKOHRW;41I2k)T%iUV+)@a!2?`M?$- zuqFE?jy>+YiQ*c7EUNVdkN}uc6-P6gFGQcK4rCNPM?|qTN}S;yYwGhL+8w`<0X@6K z&Z|I$_WA9x!7I-3i1NDFt@2`*TP$!zJZ(&DOJfvxK;Dab??uh=!ndR3o3X))>u#~f z6N+w@+-Me6n#R_i^|A)PMm~CVj+s%hhHAUsAk+5o*4l>^bmI&-1J1xdV1PYaWjYV& z)){aHoPh%a`h7^Lidn=upnW=MYy==q=r-ZC&JvoFM$97C0r`X?A(a?XC5{*p(&fpx;)Ehro$IF-4#@(#bq1V)E(7Cmn`r;P{C@uLCi$H+;0*jJ24u3l zT`st#*jopi(_WiW@2DyoR|h<#@WNU#W3?5ZP)+Dh^@5m1tOL?R@jn7agBxeyM;Z78 Dw~tPB literal 0 HcmV?d00001 From 862654454e276d7b8ec4372737ddd321a1770a14 Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Fri, 13 Feb 2015 15:32:11 +0200 Subject: [PATCH 3/7] whitespace fixes --- .DS_Store | Bin 8196 -> 0 bytes spec/.DS_Store | Bin 6148 -> 0 bytes 2 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 .DS_Store delete mode 100644 spec/.DS_Store diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index 35fcd11212309225cebf90df915fb31b628a8653..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 8196 zcmeHM-EPw`6h7X%EnO#3{-jCVH(Vel?8-ileLnk~&rY0^5)mtRtlLEMM5G~ena`m} zNxYxuu{062T!Cc36E*3G3~JM1)g9AT4T=H9fMP%~pcqgL{0j`=%;rfu=iHaQs#FXp z2L4M1`13(W=CTyYzLet8fkK1;kQp?KggW8?fzcybiez6(0mYaydSDThMIi>0aP;e3 z9kLY3zLXM9Ou~spGqWf{VPbaRbfHeHB&8}91B!uh26*hAC6jFO=pY`y`v<1$o9N{? z)E(rT1CdZ8nx!?mN2|0+cd0@(T14D$Row*U`v~R|Fb{y(p>3*CnI5866(gje+4cx} znk?8DkV6gfk~6U%Bf1(iPw*Y$h+5=wR8n7f^E&3u9K2{#2MmPt3^Q=dB0V3&agXA- z8M+S-*1*6fIH)9N5gx>~=U|Nijt@~b&SoG#)tfjjLoZUT!J3GN-o#)4yNITQUJG6{ zl9mwcW?IZ&sTt7ms3x~Lp{Zfa4s7K)g%QqtLO+YOtzciQ(*|V9x^nK>Nz9fUn76@t z9c#h@@Xt$t$kn~j8e*!L8#K}<4>;|o9KmRgYt`meyOF!AfWM*o5M$69T z_%DBX_DQYjcWa>$c7n3eek`sW%QmC>mJv43>N~#g4(o=w>)73T{@R-Fg@)tV?HF`_ z&+#n3TlPDi73|mKczcc&HtYH8TW$Lo&@IEWcb&j79oGq8YFYlP=jTsP3x$;hy|7&D zE$FAk;?jb?TqyQ>`OlhmC0EK4Twc2Bwdx)!}1$yLqd*g zZO`?+2HLoOj&`wN+t|icag(Or(qg$|%K)G_g(21XKcrbz6ayE7fk|oRJdgh?=YRjd z7|tjw6$6TaKVpDo)@$o!K+oPBMn~bNypH@7nJ2E-mr_7bBwhq?J^aHEbsef4Q;K9? TO0*!U4*`M(RVW7jDg(a(Q;9Ga diff --git a/spec/.DS_Store b/spec/.DS_Store deleted file mode 100644 index 54f4f53b0fad618bf2d4c94528db59309b4dd605..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHKOHRW;41I2k)T%iUV+)@a!2?`M?$- zuqFE?jy>+YiQ*c7EUNVdkN}uc6-P6gFGQcK4rCNPM?|qTN}S;yYwGhL+8w`<0X@6K z&Z|I$_WA9x!7I-3i1NDFt@2`*TP$!zJZ(&DOJfvxK;Dab??uh=!ndR3o3X))>u#~f z6N+w@+-Me6n#R_i^|A)PMm~CVj+s%hhHAUsAk+5o*4l>^bmI&-1J1xdV1PYaWjYV& z)){aHoPh%a`h7^Lidn=upnW=MYy==q=r-ZC&JvoFM$97C0r`X?A(a?XC5{*p(&fpx;)Ehro$IF-4#@(#bq1V)E(7Cmn`r;P{C@uLCi$H+;0*jJ24u3l zT`st#*jopi(_WiW@2DyoR|h<#@WNU#W3?5ZP)+Dh^@5m1tOL?R@jn7agBxeyM;Z78 Dw~tPB From ca28f9bed802c77b1bebe6fda50bd7e20753c1eb Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Fri, 13 Feb 2015 16:00:17 +0200 Subject: [PATCH 4/7] domain test with node failed --- .DS_Store | Bin 0 -> 8196 bytes nodetest.js | 7 +++++++ q.js | 6 +++--- 3 files changed, 10 insertions(+), 3 deletions(-) create mode 100644 .DS_Store create mode 100644 nodetest.js diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..8f03eba6592247cee61edaf14e2cce07b7bb086c GIT binary patch literal 8196 zcmeHM&2G~`5T0$*)@hIk0;Eb@d<3bgiqb0AG;M%Lv{X%Lg(wPfoF=BOZ4##`sv_mg z6L9ALF^~|PcmQ|)1aMr} zSsk(v%0VClIEesGBD5^R3`I!m!0A$*L?Mu>RSYNw#u?z;JxvBR$f0^Pe|PJK?Hahs zH`G1U+vl>NM>J3C^nliAi|$j2Dzt>W-z?iP%#RVwRbUj2XM)M{T%@st^rY;!p=@1qevqUe(a2!z- zH%AW>N~r`SvZ-Z&kB5x7&>qK(}j7W6$bXhGkp+ zD=p2RYIfo5ESFnd)N?EO{-S=C&o3|PE4h5XpZ%g~x9+U2Z9eh(uiw6V|Ka1O&tJdE zZ9pPCSBW18(efQ%hJ>6{+K%lyO^mUAhW4;vJJ|eXd21&=2;_1PmqAD%!`1m8rCIeA z1CwT8Ixx7v`G4v1-~T7=ma?Z9Pz+3v0g>9MY!qSe;vHbrHXqeG>T^^stTzaxgrJe9 jIiyU-A%}k$qOU`hF@;bL0*Mh6^&%i?P>o_>q73{6#+^O* literal 0 HcmV?d00001 diff --git a/nodetest.js b/nodetest.js new file mode 100644 index 00000000..d0217600 --- /dev/null +++ b/nodetest.js @@ -0,0 +1,7 @@ +var q = require("./q/index.js"); + +q.reject("Heh"); + +process.on("unhandledRejection", function(handler, reason){ + console.log("Found it!", handler, reason); +}); \ No newline at end of file diff --git a/q.js b/q.js index 59bf6faa..aa3fbcc4 100644 --- a/q.js +++ b/q.js @@ -107,7 +107,7 @@ var nextTick =(function () { head.domain = void 0; domain.enter(); } - runSingle(task); + runSingle(task, domain); } while(laterQueue.length){ @@ -116,8 +116,8 @@ var nextTick =(function () { } flushing = false; } - - function runSingle(task){ // runs a single function in the async queue + // runs a single function in the async queue + function runSingle(task, domain){ try { task(); From 382220a5802a5efa04f6b06bd4d825fc5aa23d5a Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Fri, 13 Feb 2015 16:00:55 +0200 Subject: [PATCH 5/7] stupid DS_Store thing again, first time using GH on a mac --- .DS_Store | Bin 8196 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index 8f03eba6592247cee61edaf14e2cce07b7bb086c..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 8196 zcmeHM&2G~`5T0$*)@hIk0;Eb@d<3bgiqb0AG;M%Lv{X%Lg(wPfoF=BOZ4##`sv_mg z6L9ALF^~|PcmQ|)1aMr} zSsk(v%0VClIEesGBD5^R3`I!m!0A$*L?Mu>RSYNw#u?z;JxvBR$f0^Pe|PJK?Hahs zH`G1U+vl>NM>J3C^nliAi|$j2Dzt>W-z?iP%#RVwRbUj2XM)M{T%@st^rY;!p=@1qevqUe(a2!z- zH%AW>N~r`SvZ-Z&kB5x7&>qK(}j7W6$bXhGkp+ zD=p2RYIfo5ESFnd)N?EO{-S=C&o3|PE4h5XpZ%g~x9+U2Z9eh(uiw6V|Ka1O&tJdE zZ9pPCSBW18(efQ%hJ>6{+K%lyO^mUAhW4;vJJ|eXd21&=2;_1PmqAD%!`1m8rCIeA z1CwT8Ixx7v`G4v1-~T7=ma?Z9Pz+3v0g>9MY!qSe;vHbrHXqeG>T^^stTzaxgrJe9 jIiyU-A%}k$qOU`hF@;bL0*Mh6^&%i?P>o_>q73{6#+^O* From c79dc03d0251fb45faac89a9bb9734db484c7de2 Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Fri, 13 Feb 2015 16:02:44 +0200 Subject: [PATCH 6/7] the amount of fail I have with git today is spectacular --- nodetest.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nodetest.js b/nodetest.js index d0217600..48f192df 100644 --- a/nodetest.js +++ b/nodetest.js @@ -3,5 +3,5 @@ var q = require("./q/index.js"); q.reject("Heh"); process.on("unhandledRejection", function(handler, reason){ - console.log("Found it!", handler, reason); + console.log("Found it!", handler, "reason", reason); }); \ No newline at end of file From 86cb5c6327271edd670d999979ce5b8d6c7657d7 Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Fri, 13 Feb 2015 16:07:59 +0200 Subject: [PATCH 7/7] removed node test dep --- nodetest.js | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 nodetest.js diff --git a/nodetest.js b/nodetest.js deleted file mode 100644 index 48f192df..00000000 --- a/nodetest.js +++ /dev/null @@ -1,7 +0,0 @@ -var q = require("./q/index.js"); - -q.reject("Heh"); - -process.on("unhandledRejection", function(handler, reason){ - console.log("Found it!", handler, "reason", reason); -}); \ No newline at end of file