Skip to content

Commit 1648aaf

Browse files
rashedmytkrisctl
authored andcommitted
Fixes Jupyter server kernel communication issue when started with explicit IP address.
1 parent b5b5616 commit 1648aaf

File tree

3 files changed

+24
-15
lines changed

3 files changed

+24
-15
lines changed

src/jupyter_matlab_kernel/kernel.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010
import ipykernel.kernelbase
1111
import psutil
1212
import requests
13-
from jupyter_matlab_kernel import mwi_comm_helpers, mwi_logger
1413
from matlab_proxy import settings as mwi_settings
1514
from matlab_proxy import util as mwi_util
1615
from requests.exceptions import HTTPError
1716

17+
from jupyter_matlab_kernel import mwi_comm_helpers, mwi_logger
18+
1819
_MATLAB_STARTUP_TIMEOUT = mwi_settings.get_process_startup_timeout()
1920
_logger = mwi_logger.get()
2021

@@ -195,10 +196,13 @@ def start_matlab_proxy(logger=_logger):
195196
"""
196197
)
197198

198-
url = "{protocol}://localhost:{port}{base_url}matlab".format(
199-
protocol="https" if nb_server["secure"] else "http",
200-
port=nb_server["port"],
201-
base_url=nb_server["base_url"],
199+
# Using nb_server["url"] to construct matlab-proxy URL as it handles the following cases
200+
# 1. For normal usage of Jupyter, the URL returned by nb_server uses localhost
201+
# 2. For explicitly specified IP with Jupyter, the URL returned by nb_server
202+
# a. uses FQDN hostname when specified IP is 0.0.0.0
203+
# b. uses specified IP for all other cases
204+
matlab_proxy_url = "{jupyter_server_url}matlab".format(
205+
jupyter_server_url=nb_server["url"]
202206
)
203207

204208
available_tokens = {
@@ -213,11 +217,11 @@ def start_matlab_proxy(logger=_logger):
213217
else:
214218
headers = None
215219

216-
if _start_matlab_proxy_using_jupyter(url, headers, logger):
220+
if _start_matlab_proxy_using_jupyter(matlab_proxy_url, headers, logger):
217221
logger.debug(
218-
f"Started matlab-proxy using jupyter at {url} with headers: {headers}"
222+
f"Started matlab-proxy using jupyter at {matlab_proxy_url} with headers: {headers}"
219223
)
220-
return url, nb_server["base_url"], headers
224+
return matlab_proxy_url, nb_server["base_url"], headers
221225

222226
logger.error(
223227
f"MATLABKernel could not communicate with matlab-proxy through Jupyter server"

tests/unit/jupyter_matlab_kernel/mocks/mock_jupyter_server.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
1-
# Copyright 2023 The MathWorks, Inc.
1+
# Copyright 2023-2024 The MathWorks, Inc.
22
"""Mocks matlab-proxy integration with Jupyter Server.
33
44
This module provides a pytest fixture that mocks how matlab-proxy integrates
55
with Jupyter server.
66
"""
77

8-
98
import os
109

1110
import pytest
1211
import requests
1312
from jupyter_server import serverapp
1413

1514
PID = "server 1"
16-
PORT = "1234/"
17-
BASE_URL = "server_of_nb/"
15+
PORT = "1234"
16+
BASE_URL = "/server_of_nb/"
1817
SECURE = False
1918
TEST_TOKEN = "test_token"
2019
LICENSING = True
2120
AUTHORIZED_HEADERS = {"Authorization": "token test_token"}
2221
PASSWORD = ""
22+
HOSTNAME = "localhost"
23+
URL = f"http://{HOSTNAME}:{PORT}{BASE_URL}"
24+
SECURE_URL = f"https://{HOSTNAME}:{PORT}{BASE_URL}"
2325

2426

2527
@pytest.fixture
@@ -42,6 +44,8 @@ def fake_list_running_servers(*args, **kwargs):
4244
"secure": SECURE,
4345
"token": TEST_TOKEN,
4446
"password": PASSWORD,
47+
"hostname": HOSTNAME,
48+
"url": URL,
4549
}
4650
]
4751

tests/unit/jupyter_matlab_kernel/test_kernel.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
# Copyright 2023 The MathWorks, Inc.
1+
# Copyright 2023-2024 The MathWorks, Inc.
22

33
# This file contains tests for jupyter_matlab_kernel.kernel
44
import mocks.mock_jupyter_server as MockJupyterServer
55
import pytest
6+
from jupyter_matlab_kernel.kernel import MATLABConnectionError, start_matlab_proxy
67
from jupyter_server import serverapp
78
from mocks.mock_jupyter_server import MockJupyterServerFixture
89

9-
from jupyter_matlab_kernel.kernel import MATLABConnectionError, start_matlab_proxy
10-
1110

1211
def test_start_matlab_proxy_without_jupyter_server():
1312
"""
@@ -55,6 +54,8 @@ def fake_list_running_servers(*args, **kwargs):
5554
"secure": True,
5655
"token": MockJupyterServer.TEST_TOKEN,
5756
"password": MockJupyterServer.PASSWORD,
57+
"hostname": MockJupyterServer.HOSTNAME,
58+
"url": MockJupyterServer.SECURE_URL,
5859
}
5960
]
6061

0 commit comments

Comments
 (0)