From 1edba41c4c4efdca7b6f1fe5c4c616142413f067 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lorenz=20H=C3=BCbschle-Schneider?= Date: Sun, 10 Apr 2016 11:53:07 +0200 Subject: [PATCH] Add a lock on the connection This should fix #610 but needs more testing --- js/connection.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/js/connection.js b/js/connection.js index 319219f..55e714a 100644 --- a/js/connection.js +++ b/js/connection.js @@ -15,6 +15,9 @@ weechat.factory('connection', var connectionData = []; var reconnectTimer; + // Global connection lock to prevent multiple connections from being opened + var locked = false; + // Takes care of the connection and websocket hooks var connect = function (host, port, passwd, ssl, noCompression, successCallback, failCallback) { $rootScope.passwordError = false; @@ -120,6 +123,7 @@ weechat.factory('connection', */ $log.info("Disconnected from relay"); $rootScope.$emit('relayDisconnect'); + locked = false; if ($rootScope.userdisconnect || !$rootScope.waseverconnected) { handleClose(evt); $rootScope.userdisconnect = false; @@ -154,6 +158,7 @@ weechat.factory('connection', * the relay. */ $log.error("Relay error", evt); + locked = false; // release connection lock $rootScope.lastError = Date.now(); if (evt.type === "error" && this.readyState !== 1) { @@ -162,6 +167,13 @@ weechat.factory('connection', } }; + if (locked) { + // We already have an open connection + $log.debug("Aborting connection (lock in use)"); + } + // Kinda need a compare-and-swap here... + locked = true; + try { ngWebsockets.connect(url, protocol, @@ -173,6 +185,7 @@ weechat.factory('connection', 'onerror': onerror }); } catch(e) { + locked = false; $log.debug("Websocket caught DOMException:", e); $rootScope.lastError = Date.now(); $rootScope.errorMessage = true; @@ -250,6 +263,7 @@ weechat.factory('connection', // The connection can time out on its own ngWebsockets.failCallbacks('disconnection'); $rootScope.connected = false; + locked = false; // release the connection lock $rootScope.$emit('relayDisconnect'); $rootScope.$apply(); });