From 2f16d6bfe397b7d0c8ff31bfe7eb0c326f2740e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Thu, 12 Apr 2018 11:16:30 +0200 Subject: [PATCH] [FIX] web: prevent destroyed widget from calling start Whenever a widget is destroyed, it should be as inactive as possible. Before this commit, it could happen that a widget was destroyed after the willstart method, but before the start method. In that case, bad things could happen. For example, the discuss client action tries to look up some information into the session, with the getSession method. This causes a crash whenever the widget is destroyed. --- addons/web/static/src/js/core/widget.js | 3 +++ addons/web/static/tests/core/widget_tests.js | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/addons/web/static/src/js/core/widget.js b/addons/web/static/src/js/core/widget.js index 5e23c3f2c47..b25cd99587b 100644 --- a/addons/web/static/src/js/core/widget.js +++ b/addons/web/static/src/js/core/widget.js @@ -391,6 +391,9 @@ var Widget = core.Class.extend(mixins.PropertiesMixin, ServicesMixin, { _widgetRenderAndInsert: function (insertion, target) { var self = this; return this.willStart().then(function () { + if (self.__parentedDestroyed) { + return; + } self.renderElement(); insertion(target); return self.start(); diff --git a/addons/web/static/tests/core/widget_tests.js b/addons/web/static/tests/core/widget_tests.js index 43b9d0dc334..19e72cb97b6 100644 --- a/addons/web/static/tests/core/widget_tests.js +++ b/addons/web/static/tests/core/widget_tests.js @@ -388,6 +388,26 @@ QUnit.module('core', {}, function () { assert.ok(true, "there should be no crash when calling _rpc on a destroyed widget"); }); + + QUnit.test('start is not called when widget is destroyed', function (assert) { + assert.expect(0); + var slowWillStartDef = $.Deferred(); + var $fix = $( "#qunit-fixture"); + + var widget = new (Widget.extend({ + willStart: function () { + return slowWillStartDef; + }, + start: function () { + throw new Error('Should not call start method'); + }, + }))(); + + widget.appendTo($fix); + widget.destroy(); + slowWillStartDef.resolve(); + }); + }); }); \ No newline at end of file