Skip to content

Commit 2d099e2

Browse files
authored
Merge pull request #2872 from c3d1c06c-bf26-477e-b0eb-c50ef4477ba6/master
The Problem: The current code creates a new WebSocket connection in the reconnection callback but stores it in a local variable (newWs), leaving the global ws variable pointing to the closed connection. This causes the polling fallback to continue running even after a successful WebSocket reconnection, resulting in duplicate metric updates. The Solution: By directly assigning to the global ws variable, the fix ensures that: - The polling check at line 367 (if (!ws || ws.readyState !== WebSocket.OPEN)) works correctly - Only one update mechanism (WebSocket or polling) runs at a time - The dashboard doesn't receive duplicate updates.
2 parents 5536568 + ed93a09 commit 2d099e2

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

dnscrypt-proxy/static/js/monitoring.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -286,23 +286,23 @@ function connectWebSocket() {
286286
console.log('WebSocket URL:', wsUrl);
287287

288288
// Create WebSocket connection
289-
var ws = new WebSocket(wsUrl);
289+
var newWs = new WebSocket(wsUrl);
290290

291291
// Connection opened
292-
ws.onopen = function() {
292+
newWs.onopen = function() {
293293
console.log('WebSocket connected successfully');
294294
wsReconnectAttempts = 0; // Reset reconnect attempts on successful connection
295295

296296
// Send a ping to verify connection
297297
try {
298-
ws.send(JSON.stringify({type: 'ping'}));
298+
newWs.send(JSON.stringify({type: 'ping'}));
299299
} catch (e) {
300300
console.error('Error sending ping:', e);
301301
}
302302
};
303303

304304
// Listen for messages
305-
ws.onmessage = function(event) {
305+
newWs.onmessage = function(event) {
306306
try {
307307
if (!event) {
308308
console.warn('Received invalid WebSocket event');
@@ -323,12 +323,12 @@ function connectWebSocket() {
323323
};
324324

325325
// Handle errors
326-
ws.onerror = function(error) {
326+
newWs.onerror = function(error) {
327327
console.error('WebSocket error occurred:', error);
328328
};
329329

330330
// Connection closed
331-
ws.onclose = function(event) {
331+
newWs.onclose = function(event) {
332332
console.log('WebSocket disconnected, code:', event.code, 'reason:', event.reason || 'No reason provided');
333333

334334
// Try to reconnect with exponential backoff
@@ -338,10 +338,8 @@ function connectWebSocket() {
338338
console.log('Attempting to reconnect in ' + delay + 'ms (attempt ' + wsReconnectAttempts + '/' + maxReconnectAttempts + ')');
339339

340340
setTimeout(function() {
341-
var newWs = connectWebSocket();
342-
if (newWs) {
343-
// We can't update the global ws variable from here
344-
// Instead, we'll rely on the polling fallback
341+
ws = connectWebSocket();
342+
if (ws) {
345343
console.log('New WebSocket connection established');
346344
}
347345
}, delay);
@@ -350,7 +348,7 @@ function connectWebSocket() {
350348
}
351349
};
352350

353-
return ws;
351+
return newWs;
354352
} catch (error) {
355353
console.error('Failed to create WebSocket connection:', error);
356354
return null;
@@ -459,4 +457,4 @@ setTimeout(function() {
459457
loadingIndicator.style.display = 'none';
460458
}, 5000);
461459
}
462-
}, 20000);
460+
}, 20000);

0 commit comments

Comments
 (0)