Skip to content

Commit

Permalink
IPython console: Prevent segfault when closing and a client is debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
ccordoba12 committed Feb 10, 2024
1 parent 91abbf9 commit 476bfcb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
23 changes: 18 additions & 5 deletions spyder/plugins/ipythonconsole/widgets/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

# Third party imports (qtpy)
from qtpy.QtCore import QUrl, QTimer, Signal, Slot
from qtpy.QtWidgets import QVBoxLayout, QWidget
from qtpy.QtWidgets import QApplication, QVBoxLayout, QWidget

# Local imports
from spyder.api.translations import _
Expand Down Expand Up @@ -121,7 +121,11 @@ def __init__(self, parent, id_,
self.allow_rename = True
self.error_text = None
self.give_focus = give_focus

# Attrs used when closing
self.__is_last_client = False
self.__debugging_on_close = False
self.__close_now = False

css_path = self.get_conf('css_path', section='appearance')
if css_path is None:
Expand Down Expand Up @@ -554,10 +558,11 @@ def set_color_scheme(self, color_scheme, reset=True):
except AttributeError:
pass

def close_client(self, is_last_client):
def close_client(self, is_last_client, now=False):
"""Close the client."""
self.__is_last_client = is_last_client
debugging = False
self.__close_now = now
self.__debugging_on_close = False

# Needed to handle a RuntimeError. See spyder-ide/spyder#5568.
try:
Expand All @@ -566,15 +571,15 @@ def close_client(self, is_last_client):
# debugging mode before closing, but also when a kernel restart is
# requested while debugging.
if self.shellwidget.is_debugging():
debugging = True
self.__debugging_on_close = True
self.shellwidget.sig_prompt_ready.connect(self.finish_close)
self.shellwidget.stop_debugging()
else:
self.interrupt_kernel()
except RuntimeError:
pass

if not debugging:
if not self.__debugging_on_close:
self.finish_close()

def finish_close(self):
Expand All @@ -587,6 +592,14 @@ def finish_close(self):
except (RuntimeError, TypeError):
pass

# This is a hack to prevent segfaults when closing Spyder and the
# client was debugging before doing it.
# It's a side effect of spyder-ide/spyder#21788
if self.__debugging_on_close and self.__close_now:
for __ in range(3):
time.sleep(0.08)
QApplication.processEvents()

self.shutdown(self.__is_last_client)

# Prevent errors in our tests
Expand Down
5 changes: 3 additions & 2 deletions spyder/plugins/ipythonconsole/widgets/main_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -1767,8 +1767,9 @@ def close_all_clients(self):
open_clients = self.clients.copy()
for client in self.clients:
is_last_client = (
len(self.get_related_clients(client, open_clients)) == 0)
client.close_client(is_last_client)
len(self.get_related_clients(client, open_clients)) == 0
)
client.close_client(is_last_client, now=True)
open_clients.remove(client)

# Wait for all KernelHandler threads to shutdown.
Expand Down

0 comments on commit 476bfcb

Please sign in to comment.