Skip to content

Commit 1bd7b65

Browse files
author
Dorian Birraux
committed
recv also check for termination event
This allows terminate to actually kill the kernel and stop all pending commands. Terminate also queue a STOP event but does not necessary wait for it to be handled.
1 parent d82d3a9 commit 1bd7b65

1 file changed

Lines changed: 14 additions & 7 deletions

File tree

wolframclient/evaluation/kernel/kernelcontroller.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ def _kernel_start(self):
434434
# on the kernel side.
435435
response = self.kernel_socket_in.recv_abortable(
436436
timeout=self.get_parameter("STARTUP_TIMEOUT"),
437-
abort_event=_StartEvent(self.kernel_proc, self.trigger_termination_requested),
437+
abort_event=self._new_running_event(),
438438
)
439439
if response == self._KERNEL_OK:
440440
if logger.isEnabledFor(logging.INFO):
@@ -486,12 +486,12 @@ def terminate(self):
486486
# only enqueue task if the Event is not triggered.
487487
# when trigger_termination_requested is set, the run function is
488488
# already dealing with the exception and is about to terminate.
489-
if self.trigger_termination_requested.is_set():
490-
future.set_result(True)
491-
else:
489+
if not self.trigger_termination_requested.is_set():
492490
self.enqueue_task(self.STOP, future, None)
491+
self.trigger_termination_requested.set()
492+
493+
future.set_result(True)
493494
self._state_terminated = True
494-
self.trigger_termination_requested.set()
495495
return future
496496

497497
def join(self, timeout=None):
@@ -502,6 +502,13 @@ def join(self, timeout=None):
502502
def evaluate_future(self, wxf, future, result_update_callback=None, **kwargs):
503503
self.enqueue_task(wxf, future, result_update_callback)
504504

505+
def _new_running_event(self):
506+
"""
507+
Create a new event that triggers when the kernel process has terminated or when termination was requested.
508+
:return:
509+
"""
510+
return _ProcessAliveNotAbortedEvent(self.kernel_proc, self.trigger_termination_requested)
511+
505512
def _recv_check_process(self, copy=False):
506513
"""
507514
Call recv on the kernel input socket. Regularly check that the kernel process
@@ -510,7 +517,7 @@ def _recv_check_process(self, copy=False):
510517
:return:
511518
"""
512519
try:
513-
return self.kernel_socket_in.recv_abortable(copy=copy, abort_event=_KernelProcessDied(self.kernel_proc))
520+
return self.kernel_socket_in.recv_abortable(copy=copy, abort_event=self._new_running_event())
514521
except SocketAborted:
515522
logger.info("Kernel process is not running anymore.")
516523
raise WolframKernelException("Kernel is not running anymore.")
@@ -623,7 +630,7 @@ def __repr__(self):
623630
return '<%s[%s ❌], "%s">' % (self.__class__.__name__, self.name, self.kernel)
624631

625632

626-
class _StartEvent(object):
633+
class _ProcessAliveNotAbortedEvent(object):
627634
def __init__(self, subprocess, abort_event):
628635
self.subprocess = subprocess
629636
self.abort_event = abort_event

0 commit comments

Comments
 (0)