Skip to content

Commit e4bf162

Browse files
Add back support for kernel launch timeout pad (#910)
* Re-add LAUNCH_TIMEOUT_PAD support * Move option next to other configs - Update request_timeout default value - Update pad_timeout default value - Rename kernel_launch_timeout_pad => launch_timeout_pad * Update help with suggestion * Make the launch_timeout_pad flow for consistency with other timeouts defined in this file * Forgot to make default value also float. Co-authored-by: Kevin Bates <[email protected]>
1 parent 1ec1aee commit e4bf162

File tree

2 files changed

+68
-12
lines changed

2 files changed

+68
-12
lines changed

jupyter_server/gateway/gateway_client.py

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def connect_timeout_default(self):
128128
os.environ.get("JUPYTER_GATEWAY_CONNECT_TIMEOUT", self.connect_timeout_default_value)
129129
)
130130

131-
request_timeout_default_value = 40.0
131+
request_timeout_default_value = 42.0
132132
request_timeout_env = "JUPYTER_GATEWAY_REQUEST_TIMEOUT"
133133
request_timeout = Float(
134134
default_value=request_timeout_default_value,
@@ -341,6 +341,25 @@ def gateway_retry_max_default(self):
341341
os.environ.get("JUPYTER_GATEWAY_RETRY_MAX", self.gateway_retry_max_default_value)
342342
)
343343

344+
launch_timeout_pad_default_value = 2.0
345+
launch_timeout_pad_env = "JUPYTER_GATEWAY_LAUNCH_TIMEOUT_PAD"
346+
launch_timeout_pad = Float(
347+
default_value=launch_timeout_pad_default_value,
348+
config=True,
349+
help="""Timeout pad to be ensured between KERNEL_LAUNCH_TIMEOUT and request_timeout
350+
such that request_timeout >= KERNEL_LAUNCH_TIMEOUT + launch_timeout_pad.
351+
(JUPYTER_GATEWAY_LAUNCH_TIMEOUT_PAD env var)""",
352+
)
353+
354+
@default("launch_timeout_pad")
355+
def launch_timeout_pad_default(self):
356+
return float(
357+
os.environ.get(
358+
"JUPYTER_GATEWAY_LAUNCH_TIMEOUT_PAD",
359+
self.launch_timeout_pad_default_value,
360+
)
361+
)
362+
344363
@property
345364
def gateway_enabled(self):
346365
return bool(self.url is not None and len(self.url) > 0)
@@ -353,12 +372,18 @@ def init_static_args(self):
353372
perform this operation once.
354373
355374
"""
356-
# Ensure that request timeout and KERNEL_LAUNCH_TIMEOUT are the same, taking the
357-
# greater value of the two.
358-
if self.request_timeout < float(GatewayClient.KERNEL_LAUNCH_TIMEOUT):
359-
self.request_timeout = float(GatewayClient.KERNEL_LAUNCH_TIMEOUT)
360-
elif self.request_timeout > float(GatewayClient.KERNEL_LAUNCH_TIMEOUT):
361-
GatewayClient.KERNEL_LAUNCH_TIMEOUT = int(self.request_timeout)
375+
# Ensure that request timeout and KERNEL_LAUNCH_TIMEOUT are in sync, taking the
376+
# greater value of the two and taking into account the following relation:
377+
# request_timeout = KERNEL_LAUNCH_TIME + padding
378+
minimum_request_timeout = (
379+
float(GatewayClient.KERNEL_LAUNCH_TIMEOUT) + self.launch_timeout_pad
380+
)
381+
if self.request_timeout < minimum_request_timeout:
382+
self.request_timeout = minimum_request_timeout
383+
elif self.request_timeout > minimum_request_timeout:
384+
GatewayClient.KERNEL_LAUNCH_TIMEOUT = int(
385+
self.request_timeout - self.launch_timeout_pad
386+
)
362387
# Ensure any adjustments are reflected in env.
363388
os.environ["KERNEL_LAUNCH_TIMEOUT"] = str(GatewayClient.KERNEL_LAUNCH_TIMEOUT)
364389

tests/test_gateway.py

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ def init_gateway(monkeypatch):
186186
monkeypatch.setenv("JUPYTER_GATEWAY_HTTP_USER", mock_http_user)
187187
monkeypatch.setenv("JUPYTER_GATEWAY_REQUEST_TIMEOUT", "44.4")
188188
monkeypatch.setenv("JUPYTER_GATEWAY_CONNECT_TIMEOUT", "44.4")
189+
monkeypatch.setenv("JUPYTER_GATEWAY_LAUNCH_TIMEOUT_PAD", "1.1")
189190
yield
190191
GatewayClient.clear_instance()
191192

@@ -198,11 +199,10 @@ async def test_gateway_env_options(init_gateway, jp_serverapp):
198199
jp_serverapp.gateway_config.connect_timeout == jp_serverapp.gateway_config.request_timeout
199200
)
200201
assert jp_serverapp.gateway_config.connect_timeout == 44.4
202+
assert jp_serverapp.gateway_config.launch_timeout_pad == 1.1
201203

202204
GatewayClient.instance().init_static_args()
203-
assert GatewayClient.instance().KERNEL_LAUNCH_TIMEOUT == int(
204-
jp_serverapp.gateway_config.request_timeout
205-
)
205+
assert GatewayClient.instance().KERNEL_LAUNCH_TIMEOUT == 43
206206

207207

208208
async def test_gateway_cli_options(jp_configurable_serverapp):
@@ -211,6 +211,7 @@ async def test_gateway_cli_options(jp_configurable_serverapp):
211211
"--GatewayClient.http_user=" + mock_http_user,
212212
"--GatewayClient.connect_timeout=44.4",
213213
"--GatewayClient.request_timeout=96.0",
214+
"--GatewayClient.launch_timeout_pad=5.1",
214215
]
215216

216217
GatewayClient.clear_instance()
@@ -221,10 +222,40 @@ async def test_gateway_cli_options(jp_configurable_serverapp):
221222
assert app.gateway_config.http_user == mock_http_user
222223
assert app.gateway_config.connect_timeout == 44.4
223224
assert app.gateway_config.request_timeout == 96.0
225+
assert app.gateway_config.launch_timeout_pad == 5.1
224226
GatewayClient.instance().init_static_args()
225227
assert (
226-
GatewayClient.instance().KERNEL_LAUNCH_TIMEOUT == 96
227-
) # Ensure KLT gets set from request-timeout
228+
GatewayClient.instance().KERNEL_LAUNCH_TIMEOUT == 90
229+
) # Ensure KLT gets set from request-timeout - launch_timeout_pad
230+
GatewayClient.clear_instance()
231+
232+
233+
@pytest.mark.parametrize(
234+
"request_timeout,kernel_launch_timeout,expected_request_timeout,expected_kernel_launch_timeout",
235+
[(50, 10, 50, 45), (10, 50, 55, 50)],
236+
)
237+
async def test_gateway_request_timeout_pad_option(
238+
jp_configurable_serverapp,
239+
monkeypatch,
240+
request_timeout,
241+
kernel_launch_timeout,
242+
expected_request_timeout,
243+
expected_kernel_launch_timeout,
244+
):
245+
argv = [
246+
f"--GatewayClient.request_timeout={request_timeout}",
247+
"--GatewayClient.launch_timeout_pad=5",
248+
]
249+
250+
GatewayClient.clear_instance()
251+
app = jp_configurable_serverapp(argv=argv)
252+
253+
monkeypatch.setattr(GatewayClient, "KERNEL_LAUNCH_TIMEOUT", kernel_launch_timeout)
254+
GatewayClient.instance().init_static_args()
255+
256+
assert app.gateway_config.request_timeout == expected_request_timeout
257+
assert GatewayClient.instance().KERNEL_LAUNCH_TIMEOUT == expected_kernel_launch_timeout
258+
228259
GatewayClient.clear_instance()
229260

230261

0 commit comments

Comments
 (0)