Skip to content

Commit 573adc3

Browse files
committed
Gateway only: Ensure launch and request timeouts are in sync (#5317)
Prior to this change, the request timeout for a Gateway request was synchronized with KERNEL_LAUNCH_TIMEOUT only if KLT was greater. However, the two are closely associated and KLT should be adjusted if the configurable request_timeout is greater. This change ensures that the two values are synchronized to the greater value. It changes the two configurable timeouts to default to 40 (to match that of KLT) and removes the 2-second pad, since that wasn't helpful and only confused the situation. These changes were prompted by this issue: jupyter-server/enterprise_gateway#792
1 parent fcf3070 commit 573adc3

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

jupyter_server/gateway/managers.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def _kernelspecs_endpoint_default(self):
102102
def _kernelspecs_resource_endpoint_default(self):
103103
return os.environ.get(self.kernelspecs_resource_endpoint_env, self.kernelspecs_resource_endpoint_default_value)
104104

105-
connect_timeout_default_value = 60.0
105+
connect_timeout_default_value = 40.0
106106
connect_timeout_env = 'JUPYTER_GATEWAY_CONNECT_TIMEOUT'
107107
connect_timeout = Float(default_value=connect_timeout_default_value, config=True,
108108
help="""The time allowed for HTTP connection establishment with the Gateway server.
@@ -112,7 +112,7 @@ def _kernelspecs_resource_endpoint_default(self):
112112
def connect_timeout_default(self):
113113
return float(os.environ.get('JUPYTER_GATEWAY_CONNECT_TIMEOUT', self.connect_timeout_default_value))
114114

115-
request_timeout_default_value = 60.0
115+
request_timeout_default_value = 40.0
116116
request_timeout_env = 'JUPYTER_GATEWAY_REQUEST_TIMEOUT'
117117
request_timeout = Float(default_value=request_timeout_default_value, config=True,
118118
help="""The time allowed for HTTP request completion. (JUPYTER_GATEWAY_REQUEST_TIMEOUT env var)""")
@@ -226,18 +226,20 @@ def gateway_enabled(self):
226226

227227
# Ensure KERNEL_LAUNCH_TIMEOUT has a default value.
228228
KERNEL_LAUNCH_TIMEOUT = int(os.environ.get('KERNEL_LAUNCH_TIMEOUT', 40))
229-
os.environ['KERNEL_LAUNCH_TIMEOUT'] = str(KERNEL_LAUNCH_TIMEOUT)
230-
231-
LAUNCH_TIMEOUT_PAD = int(os.environ.get('LAUNCH_TIMEOUT_PAD', 2))
232229

233230
def init_static_args(self):
234231
"""Initialize arguments used on every request. Since these are static values, we'll
235232
perform this operation once.
236233
237234
"""
238-
# Ensure that request timeout is at least "pad" greater than launch timeout.
239-
if self.request_timeout < float(GatewayClient.KERNEL_LAUNCH_TIMEOUT + GatewayClient.LAUNCH_TIMEOUT_PAD):
240-
self.request_timeout = float(GatewayClient.KERNEL_LAUNCH_TIMEOUT + GatewayClient.LAUNCH_TIMEOUT_PAD)
235+
# Ensure that request timeout and KERNEL_LAUNCH_TIMEOUT are the same, taking the
236+
# greater value of the two.
237+
if self.request_timeout < float(GatewayClient.KERNEL_LAUNCH_TIMEOUT):
238+
self.request_timeout = float(GatewayClient.KERNEL_LAUNCH_TIMEOUT)
239+
elif self.request_timeout > float(GatewayClient.KERNEL_LAUNCH_TIMEOUT):
240+
GatewayClient.KERNEL_LAUNCH_TIMEOUT = int(self.request_timeout)
241+
# Ensure any adjustments are reflected in env.
242+
os.environ['KERNEL_LAUNCH_TIMEOUT'] = str(GatewayClient.KERNEL_LAUNCH_TIMEOUT)
241243

242244
self._static_args['headers'] = json.loads(self.headers)
243245
if 'Authorization' not in self._static_args['headers'].keys():

tests/test_gateway.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,24 +152,28 @@ async def test_gateway_env_options(init_gateway, jp_serverapp):
152152
assert jp_serverapp.gateway_config.connect_timeout == jp_serverapp.gateway_config.request_timeout
153153
assert jp_serverapp.gateway_config.connect_timeout == 44.4
154154

155+
GatewayClient.instance().init_static_args()
156+
assert GatewayClient.instance().KERNEL_LAUNCH_TIMEOUT == int(jp_serverapp.gateway_config.request_timeout)
157+
155158

156159
async def test_gateway_cli_options(jp_configurable_serverapp):
157160
argv = [
158161
'--gateway-url=' + mock_gateway_url,
159162
'--GatewayClient.http_user=' + mock_http_user,
160163
'--GatewayClient.connect_timeout=44.4',
161-
'--GatewayClient.request_timeout=44.4'
164+
'--GatewayClient.request_timeout=96.0'
162165
]
163166

164-
165167
GatewayClient.clear_instance()
166168
app = jp_configurable_serverapp(argv=argv)
167169

168170
assert app.gateway_config.gateway_enabled is True
169171
assert app.gateway_config.url == mock_gateway_url
170172
assert app.gateway_config.http_user == mock_http_user
171-
assert app.gateway_config.connect_timeout == app.gateway_config.request_timeout
172173
assert app.gateway_config.connect_timeout == 44.4
174+
assert app.gateway_config.request_timeout == 96.0
175+
GatewayClient.instance().init_static_args()
176+
assert GatewayClient.instance().KERNEL_LAUNCH_TIMEOUT == 96 # Ensure KLT gets set from request-timeout
173177
GatewayClient.clear_instance()
174178

175179

0 commit comments

Comments
 (0)