From 2e6d98c6f3fb84bd7caf722a387ff163ab8aad15 Mon Sep 17 00:00:00 2001 From: Jeremy Tuloup Date: Wed, 20 May 2020 14:57:19 +0200 Subject: [PATCH 1/2] Send kernel shutdown request manually on beforeunload --- share/jupyter/voila/templates/base/static/main.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/share/jupyter/voila/templates/base/static/main.js b/share/jupyter/voila/templates/base/static/main.js index d08be7435..4ed422a90 100644 --- a/share/jupyter/voila/templates/base/static/main.js +++ b/share/jupyter/voila/templates/base/static/main.js @@ -51,7 +51,9 @@ require([window.voila_js_url || 'static/voila'], function(voila) { async function init() { // it seems if we attach this to early, it will not be called window.addEventListener('beforeunload', function (e) { - kernel.shutdown(); + const xhttp = new XMLHttpRequest(); + xhttp.open("DELETE", `/api/kernels/${kernel.id}`, false); + xhttp.send(); kernel.dispose(); }); await widgetManager.build_widgets(); From 074618fb4fd0f450b5a851c0144852e8aef19cad Mon Sep 17 00:00:00 2001 From: Jeremy Tuloup Date: Fri, 22 May 2020 17:21:07 +0200 Subject: [PATCH 2/2] Send X-XSRFToken header --- share/jupyter/voila/templates/base/static/main.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/share/jupyter/voila/templates/base/static/main.js b/share/jupyter/voila/templates/base/static/main.js index 4ed422a90..324e6023f 100644 --- a/share/jupyter/voila/templates/base/static/main.js +++ b/share/jupyter/voila/templates/base/static/main.js @@ -50,9 +50,12 @@ require([window.voila_js_url || 'static/voila'], function(voila) { async function init() { // it seems if we attach this to early, it will not be called + const matches = document.cookie.match('\\b_xsrf=([^;]*)\\b'); + const xsrfToken = (matches && matches[1]) || ''; window.addEventListener('beforeunload', function (e) { const xhttp = new XMLHttpRequest(); xhttp.open("DELETE", `/api/kernels/${kernel.id}`, false); + xhttp.setRequestHeader('X-XSRFToken', xsrfToken); xhttp.send(); kernel.dispose(); });