Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR: Fix kernel restarts after a crash for external interpreters #13988

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions spyder/plugins/ipythonconsole/tests/test_ipythonconsole.py
Original file line number Diff line number Diff line change
Expand Up @@ -1608,35 +1608,55 @@ def test_conda_env_activation(ipyconsole, qtbot):


@flaky(max_runs=3)
@pytest.mark.parametrize("external_interpreter", [True, False])
@pytest.mark.skipif(os.name == 'nt', reason="no SIGTERM on Windows")
def test_kernel_kill(ipyconsole, qtbot):
def test_kernel_kill(ipyconsole, qtbot, external_interpreter):
"""
Test that the kernel correctly restarts after a kill.
"""
if external_interpreter:
if bool(os.environ.get('CI')):
CONF.set('main_interpreter', 'default', False)
CONF.set('main_interpreter', 'executable', get_conda_test_env())
ipyconsole.create_new_client()
else:
# We can't check this locally
return

shell = ipyconsole.get_current_shellwidget()
qtbot.waitUntil(lambda: shell._prompt_html is not None,
timeout=SHELL_TIMEOUT)
# Wait for the restarter to start
qtbot.wait(3000)
crash_string = 'import os, signal; os.kill(os.getpid(), signal.SIGTERM)'

# Check only one comm is open
old_open_comms = list(shell.spyder_kernel_comm._comms.keys())
assert len(old_open_comms) == 1
with qtbot.waitSignal(shell.sig_prompt_ready, timeout=30000):
shell.execute(crash_string)
assert crash_string in shell._control.toPlainText()
assert "Restarting kernel..." in shell._control.toPlainText()

if not external_interpreter:
assert "Restarting kernel..." in shell._control.toPlainText()

# Check a new comm replaced the old one
new_open_comms = list(shell.spyder_kernel_comm._comms.keys())
assert len(new_open_comms) == 1
assert old_open_comms[0] != new_open_comms[0]

# Wait until the comm replies
qtbot.waitUntil(
lambda: shell.spyder_kernel_comm._comms[new_open_comms[0]][
'status'] == 'ready')
assert shell.spyder_kernel_comm._comms[new_open_comms[0]][
'status'] == 'ready'

# Reset interpreter
if external_interpreter:
CONF.set('main_interpreter', 'default', True)
CONF.set('main_interpreter', 'executable', '')


@flaky(max_runs=3)
def test_wrong_std_module(ipyconsole, qtbot):
Expand Down
23 changes: 17 additions & 6 deletions spyder/plugins/ipythonconsole/widgets/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,6 @@ def configure_shellwidget(self, give_focus=True):
# To sync with working directory toolbar
self.shellwidget.executed.connect(self.shellwidget.update_cwd)

# To apply style
self.set_color_scheme(self.shellwidget.syntax_style, reset=False)

def add_to_history(self, command):
"""Add command to history"""
if self.shellwidget.is_debugging():
Expand All @@ -340,10 +337,19 @@ def _when_prompt_is_ready(self):
# To show if special console is valid
self._check_special_console_error()

# To apply style at kernel startup or kernel crashes
self.set_color_scheme(self.shellwidget.syntax_style, reset=False)

# Don't call this method for any new prompt
self.shellwidget.sig_prompt_ready.disconnect(
self._when_prompt_is_ready)
self.shellwidget.sig_remote_execute.disconnect(
self._when_prompt_is_ready)

# This signal is not connected in a kernel crash
try:
self.shellwidget.sig_remote_execute.disconnect(
self._when_prompt_is_ready)
except TypeError:
pass

def enable_stop_button(self):
self.stop_button.setEnabled(True)
Expand Down Expand Up @@ -641,8 +647,13 @@ def _finalise_restart(self, reset=False):
"""Finishes the restarting of the kernel."""
sw = self.shellwidget

if self._abort_kernel_restart():
if self.get_stderr_contents():
sw.spyder_kernel_comm.close()
self.stop_button.setDisabled(True)

# This covers the case of kernel crashes that generate stderr
# content, but still can recover and start a new kernel.
sw.sig_prompt_ready.connect(self._when_prompt_is_ready)
return

if self.restart_thread and self.restart_thread.error is not None:
Expand Down