Skip to content

Commit b613012

Browse files
committed
optionally choose new random ports on kernel restart
1 parent 4ed698a commit b613012

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

jupyter_client/connect.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,8 @@ def cleanup_ipc_files(self):
423423
def _record_random_port_names(self):
424424
"""Records which of the ports are randomly assigned.
425425
426-
Records on first invocation. Does nothing on later invocations."""
426+
Records on first invocation, if the transport is tcp.
427+
Does nothing on later invocations."""
427428

428429
if self.transport != 'tcp':
429430
return
@@ -435,6 +436,20 @@ def _record_random_port_names(self):
435436
if getattr(self, name) <= 0:
436437
self._random_port_names.append(name)
437438

439+
def cleanup_random_ports(self):
440+
"""Forgets randomly assigned port numbers and cleans up the connection file.
441+
442+
Does nothing if no port numbers have been randomly assigned.
443+
In particular, does nothing unless the transport is tcp.
444+
"""
445+
446+
if not self._random_port_names:
447+
return
448+
449+
for name in self._random_port_names:
450+
setattr(self, name, 0)
451+
452+
self.cleanup_connection_file()
438453

439454
def write_connection_file(self):
440455
"""Write connection info to JSON dict in self.connection_file."""

jupyter_client/manager.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -326,12 +326,9 @@ def shutdown_kernel(self, now=False, restart=False):
326326

327327
self.cleanup(connection_file=not restart)
328328

329-
def restart_kernel(self, now=False, **kw):
329+
def restart_kernel(self, now=False, newports=False, **kw):
330330
"""Restarts a kernel with the arguments that were used to launch it.
331331
332-
If the old kernel was launched with random ports, the same ports will be
333-
used for the new kernel. The same connection file is used again.
334-
335332
Parameters
336333
----------
337334
now : bool, optional
@@ -342,6 +339,14 @@ def restart_kernel(self, now=False, **kw):
342339
In all cases the kernel is restarted, the only difference is whether
343340
it is given a chance to perform a clean shutdown or not.
344341
342+
newports : bool, optional
343+
If the old kernel was launched with random ports, this flag decides
344+
whether the same ports and connection file will be used again.
345+
If False, the same ports and connection file are used. This is
346+
the default. If True, new random port numbers are chosen and a
347+
new connection file is written. It is still possible that the newly
348+
chosen random port numbers happen to be the same as the old ones.
349+
345350
`**kw` : optional
346351
Any options specified here will overwrite those used to launch the
347352
kernel.
@@ -353,6 +358,9 @@ def restart_kernel(self, now=False, **kw):
353358
# Stop currently running kernel.
354359
self.shutdown_kernel(now=now, restart=True)
355360

361+
if newports:
362+
self.cleanup_random_ports()
363+
356364
# Start new kernel.
357365
self._launch_args.update(kw)
358366
self.start_kernel(**self._launch_args)

jupyter_client/tests/test_connect.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,19 @@ def test_mixin_record_random_ports():
183183
with TemporaryDirectory() as d:
184184
dc = DummyConfigurable(data_dir=d, kernel_name='via-tcp', transport='tcp')
185185
dc.write_connection_file()
186+
186187
assert dc._connection_file_written
187188
assert os.path.exists(dc.connection_file)
188189
assert dc._random_port_names == connect.port_names
190+
191+
192+
def test_mixin_cleanup_random_ports():
193+
with TemporaryDirectory() as d:
194+
dc = DummyConfigurable(data_dir=d, kernel_name='via-tcp', transport='tcp')
195+
dc.write_connection_file()
196+
filename = dc.connection_file
197+
dc.cleanup_random_ports()
198+
199+
assert not os.path.exists(filename)
200+
for name in dc._random_port_names:
201+
assert getattr(dc, name) == 0

0 commit comments

Comments
 (0)