From 14eed232cffd2b9cf8dd375b5a8f8158ce12a4c7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Alexandre=20K=C3=BChn?= <aku@odoo.com>
Date: Thu, 25 Feb 2021 11:09:12 +0000
Subject: [PATCH] [FIX] mail: this.env.services.bus_service is undefined
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Before this commit, a page reload or a redirect could raise following
error:

```
Uncaught (in promise) TypeError: this.env.services.bus_service is undefined
```

This happens due to `Messaging` model relying on `bus_service` in its
teardown method `_willDelete`. This is sometimes unsafe because the
bus service may not have been deployed yet.

Task-2468469

closes odoo/odoo#66857

X-original-commit: a444f9c89bb8d5790b8a068a8f120f6ea9d1b17f
Signed-off-by: Sébastien Theys (seb) <seb@odoo.com>
Signed-off-by: Alexandre Kühn (aku) <aku@odoo.com>
---
 addons/mail/static/src/models/messaging/messaging.js        | 4 +++-
 .../messaging_notification_handler.js                       | 6 ++++--
 2 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/addons/mail/static/src/models/messaging/messaging.js b/addons/mail/static/src/models/messaging/messaging.js
index 6f9caca64d16..b3e66f63b486 100644
--- a/addons/mail/static/src/models/messaging/messaging.js
+++ b/addons/mail/static/src/models/messaging/messaging.js
@@ -12,7 +12,9 @@ function factory(dependencies) {
          * @override
          */
         _willDelete() {
-            this.env.services['bus_service'].off('window_focus', null, this._handleGlobalWindowFocus);
+            if (this.env.services['bus_service']) {
+                this.env.services['bus_service'].off('window_focus', null, this._handleGlobalWindowFocus);
+            }
             return super._willDelete(...arguments);
         }
 
diff --git a/addons/mail/static/src/models/messaging_notification_handler/messaging_notification_handler.js b/addons/mail/static/src/models/messaging_notification_handler/messaging_notification_handler.js
index 4ab9cfc98c38..dcd8b09f688e 100644
--- a/addons/mail/static/src/models/messaging_notification_handler/messaging_notification_handler.js
+++ b/addons/mail/static/src/models/messaging_notification_handler/messaging_notification_handler.js
@@ -16,8 +16,10 @@ function factory(dependencies) {
          * @override
          */
         _willDelete() {
-            this.env.services['bus_service'].off('notification');
-            this.env.services['bus_service'].stopPolling();
+            if (this.env.services['bus_service']) {
+                this.env.services['bus_service'].off('notification');
+                this.env.services['bus_service'].stopPolling();
+            }
             return super._willDelete(...arguments);
         }
 
-- 
GitLab