Skip to content

Instantly share code, notes, and snippets.

@renoirb
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renoirb/f401fc52b0234b5c21e7 to your computer and use it in GitHub Desktop.
Save renoirb/f401fc52b0234b5c21e7 to your computer and use it in GitHub Desktop.
Figure out why sometimes NodeJS MySQL call fails
fxa.db.ERROR: getMediawikiProfile: Error: This socket has been ended by the other party
at Socket.writeAfterFIN [as write] (net.js:276:12)
at Protocol.<anonymous> (/srv/webplatform/auth-20140522/fxa-profile-server-webplatform-customizations/node_modules/mysql/lib/Connection.js:69:27)
at Protocol.EventEmitter.emit (events.js:95:17)
at Protocol._emitPacket (/srv/webplatform/auth-20140522/fxa-profile-server-webplatform-customizations/node_modules/mysql/lib/protocol/Protocol.js:208:8)
at Query.<anonymous> (/srv/webplatform/auth-20140522/fxa-profile-server-webplatform-customizations/node_modules/mysql/lib/protocol/Protocol.js:121:12)
at Query.EventEmitter.emit (events.js:95:17)
at Query.start (/srv/webplatform/auth-20140522/fxa-profile-server-webplatform-customizations/node_modules/mysql/lib/protocol/sequences/Query.js:29:8)
at Protocol._startSequence (/srv/webplatform/auth-20140522/fxa-profile-server-webplatform-customizations/node_modules/mysql/lib/protocol/Protocol.js:262:14)
at Protocol._enqueue (/srv/webplatform/auth-20140522/fxa-profile-server-webplatform-customizations/node_modules/mysql/lib/protocol/Protocol.js:134:10)
at Connection.query (/srv/webplatform/auth-20140522/fxa-profile-server-webplatform-customizations/node_modules/mysql/lib/Connection.js:165:25)
--------------------
at Protocol._enqueue (/srv/webplatform/auth-20140522/fxa-profile-server-webplatform-customizations/node_modules/mysql/lib/protocol/Protocol.js:110:48) at Connection.query (/srv/webplatform/auth-20140522/fxa-profile-server-webplatform-customizations/node_modules/mysql/lib/Connection.js:165:25) at Object.getMediawikiProfile (/srv/webplatform/auth-20140522/fxa-profile-server-webplatform-customizations/lib/db/mysql.js:133:20) at /srv/webplatform/auth-20140522/fxa-profile-server-webplatform-customizations/lib/db/index.js:34:29 at tryCatch1 (/srv/webplatform/auth-20140522/fxa-profile-server-webplatform-customizations/node_modules/bluebird/js/main/util.js:63:19) at Promise$_callHandler [as _callHandler] (/srv/webplatform/auth-20140522/fxa-profile-server-webplatform-customizations/node_modules/bluebird/js/main/promise.js:695:13) at Promise$_settlePromiseFromHandler [as _settlePromiseFromHandler] (/srv/webplatform/auth-20140522/fxa-profile-server-webplatform-customizations/node_modules/bluebird/js/main/promise.js:711:18) at Promise$_settlePromiseAt [as _settlePromiseAt] (/srv/webplatform/auth-20140522/fxa-profile-server-webplatform-customizations/node_modules/bluebird/js/main/promise.js:868:14) at Promise.b (domain.js:183:18)
at Async$_consumeFunctionBuffer [as _consumeFunctionBuffer] (/srv/webplatform/auth-20140522/fxa-profile-server-webplatform-customizations/node_modules/bluebird/js/main/async.js:74:12)
fxa.summary.ERROR: {"code":500,"errno":999,"path":"/v1/profile","agent":"python-requests/2.3.0 CPython/2.7.6 Linux/3.13.0-24-generic","t":116,"stack":"Error:
This socket has been ended by the other party\n at Socket.writeAfterFIN [as write] (net.js:276:12)\n at Protocol.<anonymous> (/srv/webplatform/auth-20140522/fxa-profile-server-webplatform-customizations/node_modules/mysql/lib/Connection.js:69:27)\n at Protocol.EventEmitter.emit (events.js:95:17)\n at
Protocol._emitPacket (/srv/webplatform/auth-20140522/fxa-profile-server-webplatform-customizations/node_modules/mysql/lib/protocol/Protocol.js:208:8)\n a
t Query.<anonymous> (/srv/webplatform/auth-20140522/fxa-profile-server-webplatform-customizations/node_modules/mysql/lib/protocol/Protocol.js:121:12)\n at
Query.EventEmitter.emit (events.js:95:17)\n at Query.start (/srv/webplatform/auth-20140522/fxa-profile-server-webplatform-customizations/node_modules/mys
ql/lib/protocol/sequences/Query.js:29:8)\n at Protocol._startSequence (/srv/webplatform/auth-20140522/fxa-profile-server-webplatform-customizations/node_modules/mysql/lib/protocol/Protocol.js:262:14)\n at Protocol._enqueue (/srv/webplatform/auth-20140522/fxa-profile-server-webplatform-customizations/node_modules/mysql/lib/protocol/Protocol.js:134:10)\n at Connection.query (/srv/webplatform/auth-20140522/fxa-profile-server-webplatform-customizations/node_modules/mysql/lib/Connection.js:165:25)\n --------------------\n at Protocol._enqueue (/srv/webplatform/auth-20140522/fxa-profile-server-webplatform-customizations/node_modules/mysql/lib/protocol/Protocol.js:110:48)\n at Connection.query (/srv/webplatform/auth-20140522/fxa-profile-server-webplatform-customizations/node_modules/mysql/lib/Connection.js:165:25)\n at Object.getMediawikiProfile (/srv/webplatform/auth-20140522/fxa-profile-server-webplatform-customizations/lib/db/mysql.js:133:20)\n at /srv/webplatform/auth-20140522/fxa-profile-server-webplatform-customizations/lib/db/index.js:34:29\n at tryCatch1 (/srv/webplatform/auth-20140522/fxa-profile-server-webplatform-customizations/node_modules/bluebird/js/main/util.js:63:19)\n at Promise$_callHandler [as _callHandler] (/srv/webplatform/auth-20140522/fxa-profile-server-webplatform-customizations/node_modules/bluebird/js/main/promise.js:695:13)\n at Promise$_settlePromiseFromHandler [as _settlePromiseFromHandler] (/srv/webplatform/auth-20140522/fxa-profile-server-webplatform-customizations/node_modules/bluebird/js/main/promise.js:711:18)\n at Promise$_settlePromiseAt [as _settlePromiseAt] (/srv/webplatform/auth-20140522/fxa-profile-server-webplatform-customizations/node_modules/bluebird/js/main/promise.js:868:14)\n at Promise.b (domain.js:183:18)\n at Async$_consumeFunctionBuffer [as _consumeFunctionBuffer] (/srv/webplatform/auth-20140522/fxa-profile-server-webplatform-customizations/node_modules/bluebird/js/main/async.js:74:12)"}
fxa.server.INFO: GET /v1/profile - 500 (120ms)
fxa.summary.INFO: {"code":200,"errno":0,"path":"/","agent":"monit/5.6","t":8}
fxa.server.INFO: GET / - 200 (15ms)
fxa.summary.INFO: {"code":200,"errno":0,"path":"/","agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36","t":17}
fxa.server.INFO: GET / - 200 (21ms)
fxa.summary.INFO: {"code":200,"errno":0,"path":"/","agent":"monit/5.6","t":9}
fxa.server.INFO: GET / - 200 (17ms)
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// NOTE: Will be used in https://github.com/webplatform/fxa-profile-server/tree/webplatform-customizations/lib/db/
// but as a separate file than mysql.js
const P = require('../promise');
var MysqlStore = require('./mysql'); // To extend https://github.com/webplatform/fxa-profile-server/blob/webplatform-customizations/lib/db/mysql.js
const USER_DATA_QUERY = 'SELECT HEX(uid) AS uid, username, fullName FROM accounts WHERE normalizedEmail=?';
// Get User Session data
MysqlStore.prototype.getSessionData = function getSessionData(email) {
var defer = P.defer();
logger.verbose('getSessionData', email);
this._connection.query(USER_DATA_QUERY, [email], function(err, rows) {
if (err) {
logger.error('getSessionData', err);
return defer.reject(err);
}
var result = rows[0];
if (result) {
defer.resolve({
uid: result.uid,
email: email,
fullName: result.fullName,
username: result.username
});
} else {
defer.resolve();
}
});
return defer.promise;
};
module.exports = MysqlStore;
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// NOTE: Will be used in https://github.com/webplatform/fxa-profile-server/tree/webplatform-customizations/lib/routes
// but as a separate file than profile.js
const db = require('../db/mysql_webplatform'); // We call our own mysql handler, extending the original.
module.exports = {
auth: {
strategy: 'oauth',
scope: ['session']
},
handler: function sessionRead(req, reply) {
var obj = {};
obj.email = req.auth.credentials.email;
obj.uid = req.auth.credentials.user;
db.getSessionData( req.auth.credentials.email )
.done( function ( row ) {
if(!!row.fullName) {
obj.fullName = row.fullName;
}
if(!!a.username) {
obj.username = row.username;
}
reply(obj);
}, reply);
}
};

Why sometimes NodeJS MySQL call fails

Description

The following error (see error.log below) happens and i'd like to understand what is wrong with the following code.

When we call profileserver/v1/session/read the file session_read.js below gets called, and executes MySQL code from mysql_webplatform.js, so we can isolate our own queries from the original FxA’s.

What is the problem?

Proposal

Maybe its that I should use .then() instead of .done() in session_read.js line 18?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment