Skip to content

Commit a56c70b

Browse files
committed
cmds: Avoid PyQt4 threading issues
Keep a reference to the QRunnable so that we do not segfault PyQt4 when the task goes out of scope. This problem occurs with older versions of PyQt4 (e.g. 4.6.2). Newer versions of PyQt4 do not have this problem. Signed-off-by: David Aguilar <davvid@gmail.com>
1 parent 7d8c1fe commit a56c70b

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

cola/cmds.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,16 +1048,23 @@ def runner(*local_args, **local_opts):
10481048
return runner
10491049

10501050

1051+
# Holds a reference to background tasks to avoid PyQt4 segfaults
1052+
ALL_TASKS = set()
1053+
1054+
10511055
def background(parent, cls, *args, **opts):
10521056
cmd = cls(*args, **opts)
10531057
task = AsyncCommand(parent, cmd)
1058+
ALL_TASKS.add(task)
10541059
QtCore.QThreadPool.globalInstance().start(task)
10551060

10561061

1062+
10571063
class RunCommand(QtCore.QObject):
1058-
def __init__(self, cmd, parent):
1064+
def __init__(self, cmd, task, parent):
10591065
QtCore.QObject.__init__(self, parent)
10601066
self.cmd = cmd
1067+
self.task = task
10611068
self.connect(self, SIGNAL('command_ready'), self.do)
10621069

10631070
def run(self):
@@ -1066,12 +1073,16 @@ def run(self):
10661073

10671074
def do(self):
10681075
do_cmd(self.cmd)
1076+
try:
1077+
ALL_TASKS.remove(self.task)
1078+
except:
1079+
pass
10691080

10701081

10711082
class AsyncCommand(QtCore.QRunnable):
10721083
def __init__(self, parent, cmd):
10731084
QtCore.QRunnable.__init__(self)
1074-
self.runner = RunCommand(cmd, parent)
1085+
self.runner = RunCommand(cmd, self, parent)
10751086

10761087
def run(self):
10771088
self.runner.run()

0 commit comments

Comments
 (0)