From 9bf36e00a45654f6f2bd5ab91e8e3b646c99746d Mon Sep 17 00:00:00 2001 From: tsm-odoo Date: Mon, 6 Nov 2023 16:25:31 +0100 Subject: [PATCH] [FIX] bus: fix websocket outdated on db change MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When the bus service is loaded, it connects to the websocket worker and passes some information that are used to determine whether the websocket connection is outdated. Amongst them, the user id is used to detect when the user changed and when the websocket connection should be refreshed. This is not enough to ensure the websocket connection is still relevant: the user id could be the same but the db could have changed. The websocket would then be linked to the previous database. This commit fixes the issue by sending the db name alongside the user id. This will ensure the connection is refreshed when it is outdated. Steps to reproduce the issue: - Log in on db1 on 2 tabs - Log out - Log in on db2 - Try to star a message in the discuss app - Nothing happens, the messages are sent from db2 but the socket listens to db1 closes odoo/odoo#141263 Signed-off-by: Alexandre Kühn (aku) --- addons/bus/static/src/services/bus_service.js | 1 + addons/bus/static/src/workers/websocket_worker.js | 10 +++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/addons/bus/static/src/services/bus_service.js b/addons/bus/static/src/services/bus_service.js index 5bbee08b603..c11e01c5a50 100644 --- a/addons/bus/static/src/services/bus_service.js +++ b/addons/bus/static/src/services/bus_service.js @@ -98,6 +98,7 @@ export const busService = { } send("initialize_connection", { websocketURL: `${params.serverURL.replace("http", "ws")}/websocket`, + db: session.db, debug: odoo.debug, lastNotificationId: multiTab.getSharedValue("last_notification_id", 0), uid, diff --git a/addons/bus/static/src/workers/websocket_worker.js b/addons/bus/static/src/workers/websocket_worker.js index 35f4db78ab9..86ead52e1b8 100644 --- a/addons/bus/static/src/workers/websocket_worker.js +++ b/addons/bus/static/src/workers/websocket_worker.js @@ -53,6 +53,7 @@ export class WebsocketWorker { this.newestStartTs = undefined; this.websocketURL = ""; this.currentUID = null; + this.currentDB = null; this.isWaitingForNewUID = true; this.channelsByClient = new Map(); this.connectRetryDelay = this.INITIAL_RECONNECT_DELAY; @@ -216,6 +217,7 @@ export class WebsocketWorker { * Initialize a client connection to this worker. * * @param {Object} param0 + * @param {string} [param0.db] Database name. * @param {String} [param0.debug] Current debugging mode for the * given client. * @param {Number} [param0.lastNotificationId] Last notification id @@ -227,7 +229,7 @@ export class WebsocketWorker { * - undefined: not available (e.g. livechat support page) * @param {Number} param0.startTs Timestamp of start of bus service sender. */ - _initializeConnection(client, { debug, lastNotificationId, uid, websocketURL, startTs }) { + _initializeConnection(client, { db, debug, lastNotificationId, uid, websocketURL, startTs }) { if (this.newestStartTs && this.newestStartTs > startTs) { this.debugModeByClient[client] = debug; this.isDebug = Object.values(this.debugModeByClient).some( @@ -248,8 +250,9 @@ export class WebsocketWorker { this.isWaitingForNewUID = false; this.currentUID = uid; } - if (this.currentUID !== uid && isCurrentUserKnown) { + if ((this.currentUID !== uid && isCurrentUserKnown) || this.currentDB !== db) { this.currentUID = uid; + this.currentDB = db; if (this.websocket) { this.websocket.close(WEBSOCKET_CLOSE_CODES.CLEAN); } @@ -390,7 +393,8 @@ export class WebsocketWorker { */ _retryConnectionWithDelay() { this.connectRetryDelay = - Math.min(this.connectRetryDelay * 1.5, MAXIMUM_RECONNECT_DELAY) + this.RECONNECT_JITTER * Math.random(); + Math.min(this.connectRetryDelay * 1.5, MAXIMUM_RECONNECT_DELAY) + + this.RECONNECT_JITTER * Math.random(); this.connectTimeout = setTimeout(this._start.bind(this), this.connectRetryDelay); }