From aa6d74bdb0be4962883f7aac5304118e0a802850 Mon Sep 17 00:00:00 2001 From: Marcus Longmuir Date: Fri, 2 Aug 2024 21:26:49 +0100 Subject: [PATCH] Fix race condition of disconnecting during auth (#170) --- packages/3d-web-user-networking/src/UserNetworkingServer.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/3d-web-user-networking/src/UserNetworkingServer.ts b/packages/3d-web-user-networking/src/UserNetworkingServer.ts index 45d23899..148cdb19 100644 --- a/packages/3d-web-user-networking/src/UserNetworkingServer.ts +++ b/packages/3d-web-user-networking/src/UserNetworkingServer.ts @@ -121,6 +121,10 @@ export class UserNetworkingServer { if (!client.authenticatedUser) { if (parsed.type === USER_NETWORKING_USER_AUTHENTICATE_MESSAGE_TYPE) { this.handleUserAuth(client, parsed).then((authResult) => { + if (client.socket.readyState !== WebSocketOpenStatus) { + // The client disconnected before the authentication was completed + return; + } if (!authResult) { // If the user is not authorized, disconnect the client const serverError = JSON.stringify({ @@ -147,6 +151,7 @@ export class UserNetworkingServer { } const userData = authResult; + client.authenticatedUser = userData; // Give the client its own profile const userProfileMessage = JSON.stringify({ @@ -262,7 +267,6 @@ export class UserNetworkingServer { } console.log("Client authenticated", client.id, resolvedUserData); - client.authenticatedUser = resolvedUserData; return resolvedUserData; }