Skip to content

Commit

Permalink
Merge pull request #3024 from michalgregor/master
Browse files Browse the repository at this point in the history
Added QMutex protection to write_output as a workaround for issue #2251
  • Loading branch information
Nodd committed Jun 30, 2016
2 parents 5052a28 + e9d19a2 commit c4ad952
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions spyderlib/widgets/externalshell/baseshell.py
Expand Up @@ -16,7 +16,7 @@

# Third party imports
from qtpy.QtCore import (QByteArray, QProcess, Qt, QTextCodec, QTimer,
Signal, Slot)
Signal, Slot, QMutex)
from qtpy.QtWidgets import (QHBoxLayout, QInputDialog, QLabel, QLineEdit,
QMenu, QToolButton, QVBoxLayout, QWidget)

Expand Down Expand Up @@ -62,6 +62,9 @@ def __init__(self, parent=None, fname=None, wdir=None,
QWidget.__init__(self, parent)

self.menu_actions = menu_actions
self.write_lock = QMutex()
self.buffer_lock = QMutex()
self.buffer = []

self.run_button = None
self.kill_button = None
Expand Down Expand Up @@ -293,10 +296,30 @@ def get_stderr(self):
return self.transcode(qba)

def write_output(self):
# if we are already writing something else,
# store the present message in a buffer
if not self.write_lock.tryLock():
self.buffer_lock.lock()
self.buffer.append(self.get_stdout())
self.buffer_lock.unlock()

if not self.write_lock.tryLock():
return

self.shell.write(self.get_stdout(), flush=True)
# Commenting the line below improves crashes on long
# output. See Issue 2251
# QApplication.processEvents()

while True:
self.buffer_lock.lock()
messages = self.buffer
self.buffer = []

if not messages:
self.write_lock.unlock()
self.buffer_lock.unlock()
return

self.buffer_lock.unlock()
self.shell.write("\n".join(messages), flush=True)

def send_to_process(self, qstr):
raise NotImplementedError
Expand Down

0 comments on commit c4ad952

Please sign in to comment.