Skip to content

Commit

Permalink
Use ensure_async to shut down kernel
Browse files Browse the repository at this point in the history
  • Loading branch information
trungleduc committed Sep 21, 2021
1 parent 4b44d1f commit 3fceb57
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion share/jupyter/voila/templates/base/static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ require([window.voila_js_url || 'static/voila'], function(voila) {
// 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]) || '';
const configData = JSON.parse(document.getElementById('jupyter-config-data').textContent);
const configData = JSON.parse(document.getElementById('jupyter-config-data').textContent);
const baseUrl = configData.baseUrl;
window.addEventListener('beforeunload', function (e) {
const data = new FormData();
Expand Down
12 changes: 12 additions & 0 deletions tests/app/shutdown_kernel_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import re


async def test_shutdown_handler(http_server_client, base_url):
response = await http_server_client.fetch(base_url)
html_text = response.body.decode('utf-8')
pattern = r"""kernelId": ["']([0-9a-zA-Z-]+)["']"""
groups = re.findall(pattern, html_text)
kernel_id = groups[0]
shutdown_url = f'{base_url}voila/api/shutdown/{kernel_id}'
shutdown_response = await http_server_client.fetch(shutdown_url, method='POST', body=b'')
assert shutdown_response.code == 204
3 changes: 2 additions & 1 deletion voila/shutdown_kernel_handler.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import tornado
from jupyter_server.base.handlers import APIHandler
from nbclient.util import ensure_async


class VoilaShutdownKernelHandler(APIHandler):
Expand All @@ -8,6 +9,6 @@ class VoilaShutdownKernelHandler(APIHandler):

@tornado.web.authenticated
async def post(self, kernel_id):
await self.kernel_manager.shutdown_kernel(kernel_id)
await ensure_async(self.kernel_manager.shutdown_kernel(kernel_id))
self.set_status(204)
self.finish()

0 comments on commit 3fceb57

Please sign in to comment.