Prevent server crash in handleClientReady

The client might have disconnected between callbacks so don't try to
write to the session before checking this. The main callback of this
function now has a single check at its top.

Removed a redundant check halfway through the callback.

Also normalized use of client.id for the session index instead of a mix of
client.id and sessionId.

Added some explanatory comments.
This commit is contained in:
Richard Braakman 2012-09-28 23:03:42 +03:00
parent 413ddb393e
commit 2e72a1e489

View file

@ -882,6 +882,13 @@ function handleClientReady(client, message)
},
function(callback)
{
//Check that the client is still here. It might have disconnected between callbacks.
if(sessioninfos[client.id] === undefined)
{
callback();
return;
}
//Check if this author is already on the pad, if yes, kick the other sessions!
if(pad2sessions[padIds.padId])
{
@ -896,10 +903,9 @@ function handleClientReady(client, message)
}
//Save in sessioninfos that this session belonges to this pad
var sessionId=String(client.id);
sessioninfos[sessionId].padId = padIds.padId;
sessioninfos[sessionId].readOnlyPadId = padIds.readOnlyPadId;
sessioninfos[sessionId].readonly = padIds.readonly;
sessioninfos[client.id].padId = padIds.padId;
sessioninfos[client.id].readOnlyPadId = padIds.readOnlyPadId;
sessioninfos[client.id].readonly = padIds.readonly;
//check if there is already a pad2sessions entry, if not, create one
if(!pad2sessions[padIds.padId])
@ -908,7 +914,7 @@ function handleClientReady(client, message)
}
//Saves in pad2sessions that this session belongs to this pad
pad2sessions[padIds.padId].push(sessionId);
pad2sessions[padIds.padId].push(client.id);
//prepare all values for the wire
var atext = Changeset.cloneAText(pad.atext);
@ -973,9 +979,7 @@ function handleClientReady(client, message)
clientVars.userName = authorName;
}
if(sessioninfos[client.id] !== undefined)
{
//This is a reconnect, so we don't have to send the client the ClientVars again
//If this is a reconnect, we don't have to send the client the ClientVars again
if(message.reconnect == true)
{
//Save the revision in sessioninfos, we take the revision from the info the client send to us
@ -986,13 +990,11 @@ function handleClientReady(client, message)
{
//Send the clientVars to the Client
client.json.send({type: "CLIENT_VARS", data: clientVars});
//Save the revision in sessioninfos
//Save the current revision in sessioninfos, should be the same as in clientVars
sessioninfos[client.id].rev = pad.getHeadRevisionNumber();
}
//Save the revision and the author id in sessioninfos
sessioninfos[client.id].author = author;
}
//prepare the notification for the other users on the pad, that this user joined
var messageToTheOtherUsers = {