Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Better error handling around a method call

- Make sure not to double-resolve a future (using two different
  techniques!)

- Try/finally cleanup some resources
  • Loading branch information...
commit 57104f6a8da109e2ab648318e2e50e4171a2a1d3 1 parent 53f2b73
@glasser glasser authored
Showing with 23 additions and 10 deletions.
  1. +14 −10 tools/auth.js
  2. +9 −0 tools/fiber-helpers.js
View
24 tools/auth.js
@@ -6,7 +6,7 @@ var files = require('./files.js');
var config = require('./config.js');
var httpHelpers = require('./http-helpers.js');
var archinfo = require('./archinfo.js');
-var inFiber = require('./fiber-helpers.js').inFiber;
+var fiberHelpers = require('./fiber-helpers.js');
var release = require('./release.js');
var querystring = require('querystring');
var url = require('url');
@@ -109,21 +109,25 @@ var sessionMethodCaller = function (methodName, options) {
});
var fut = new Future();
var conn = options.connection || openAccountsConnection();
- conn.apply(methodName, args, fut.resolver());
+ conn.apply(methodName, args, fiberHelpers.firstTimeResolver(fut));
if (options.timeout !== undefined) {
- var timer = setTimeout(inFiber(function () {
- fut.throw(new Error('Method call timed out'));
+ var timer = setTimeout(fiberHelpers.inFiber(function () {
+ if (!fut.isResolved())
+ fut.throw(new Error('Method call timed out'));
}), options.timeout);
}
- var result = fut.wait();
- if (timer) {
- clearTimeout(timer);
+ try {
+ var result = fut.wait();
+ } finally {
+ if (timer) {
+ clearTimeout(timer);
+ }
+ if (! options.connection)
+ conn.close();
}
if (result && result.session) {
auth.setSessionId(config.getAccountsDomain(), result.session);
}
- if (! options.connection)
- conn.close();
return result && result.result;
};
};
@@ -736,7 +740,7 @@ exports.pollForRegistrationCompletion = function (options) {
fut['return'](result);
});
- var timer = setTimeout(inFiber(function () {
+ var timer = setTimeout(fiberHelpers.inFiber(function () {
if (! fut.isResolved()) {
fut['return'](null);
}
View
9 tools/fiber-helpers.js
@@ -37,3 +37,12 @@ exports.parallelEach = function (collection, callback, context) {
// Throw if any threw.
_.each(futures, function (f) { f.get(); });
};
+
+exports.firstTimeResolver = function (fut) {
+ var resolver = fut.resolver();
+ return function (err, val) {
+ if (fut.isResolved())
+ return;
+ resolver(err, val);
+ };
+};
Please sign in to comment.
Something went wrong with that request. Please try again.