Skip to content

Commit

Permalink
Handle QApplication.aboutToQuit signal to fix RuntimeError "wrapped C…
Browse files Browse the repository at this point in the history
…/C++ object of has been deleted"
  • Loading branch information
kozlovsky committed Dec 6, 2021
1 parent fe3b928 commit 29af36e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
23 changes: 19 additions & 4 deletions src/tribler-gui/tribler_gui/core_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ def __init__(self, root_state_dir, api_port, api_key, error_handler):
self.last_core_stderr_output: str = ''

connect(self.events_manager.tribler_started, self.on_core_connected)
app = QApplication.instance()
if app is not None:
# app can be None in tests where Qt application is not created
connect(app.aboutToQuit, self.on_about_to_quit)

def on_about_to_quit(self):
self.quitting_app = True

def on_core_connected(self, _):
if not self.core_finished:
Expand Down Expand Up @@ -101,24 +108,32 @@ def on_core_started(self):
self.core_running = True

def on_core_stdout_read_ready(self):
if self.quitting_app:
# Reading at this stage can lead to the error "wrapped C/C++ object of type QProcess has been deleted"
return

raw_output = bytes(self.core_process.readAllStandardOutput())
self.last_core_stdout_output = raw_output.decode("utf-8").strip()

try:
print(self.last_core_stdout_output) # print core output # noqa: T001
except OSError:
# Possible reason - cannot write to stdout as it was already closed during the application shutdown
if not self.quitting_app:
raise
pass

def on_core_stderr_read_ready(self):
if self.quitting_app:
# Reading at this stage can lead to the error "wrapped C/C++ object of type QProcess has been deleted"
return

raw_output = bytes(self.core_process.readAllStandardError())
self.last_core_stderr_output = raw_output.decode("utf-8").strip()

try:
print(self.last_core_stderr_output, file=sys.stderr) # print core output # noqa: T001
except OSError:
# Possible reason - cannot write to stdout as it was already closed during the application shutdown
if not self.quitting_app:
raise
pass

def stop(self, quit_app_on_core_finished=True):
if quit_app_on_core_finished:
Expand Down
9 changes: 1 addition & 8 deletions src/tribler-gui/tribler_gui/tests/test_core_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,6 @@ def test_on_core_stdout_stderr_read_ready_os_error():
core_manager = CoreManager(MagicMock(), MagicMock(), MagicMock(), MagicMock())
core_manager.core_process = MagicMock(read_all=MagicMock(return_value=''))

with pytest.raises(OSError):
core_manager.on_core_stdout_read_ready()

with pytest.raises(OSError):
core_manager.on_core_stderr_read_ready()

core_manager.quitting_app = True
# no exception during shutting down
# check that OSError exception is suppressed when writing to stdout and stderr
core_manager.on_core_stdout_read_ready()
core_manager.on_core_stderr_read_ready()

0 comments on commit 29af36e

Please sign in to comment.