thread crashes with SIGSEGV or SIGABRT

24 Views Asked by At

I am writing a python wrapper for rsync (one more ;-) and use Qt to display the output of rsync. I realize that when have verbose output for each file my program crashes with SIGSEGV or SIGABRT. I have done a lot of googling, also here and came across a small sample that worked quit well with QtCore.QThread.sleep(1) and 10 iterations in the thread. But when I increase the loop to 100 and reduce the sleep to 0.1 is also crashes like my project. Anybody an idea what goes wrong?

import sys
from PySide6 import QtCore, QtGui, QtWidgets


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Countdown")

        self.edit_field = QtWidgets.QLineEdit()
        self.edit_field.setReadOnly(True)

        self.button = QtWidgets.QPushButton("Start")
        self.button.clicked.connect(self.start_countdown)

        layout = QtWidgets.QVBoxLayout()
        layout.addWidget(self.edit_field)
        layout.addWidget(self.button)

        central_widget = QtWidgets.QWidget()
        central_widget.setLayout(layout)

        self.setCentralWidget(central_widget)

    def start_countdown(self):
        self.thread = QtCore.QThread()
        self.worker = Worker(self.edit_field)
        self.worker.moveToThread(self.thread)
        self.worker.finished.connect(self.on_finished)
        self.thread.started.connect(self.worker.run)
        self.thread.start()
        self.button.setEnabled(False)

    def on_finished(self):
        self.button.setEnabled(True)
        self.thread.quit()


class Worker(QtCore.QObject):
    finished = QtCore.Signal()

    def __init__(self, edit_field):
        super().__init__()
        self.edit_field = edit_field

    def run(self):
        for i in range(1, 100):
            self.edit_field.setText(str(i))
            self.edit_field.update()
            QtCore.QThread.sleep(0.1)
        self.finished.emit()


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec())

I have seen some other post here with answers about "non-reentrant python code", "only inherit from one QWidget" and so on, but don't see the point here in my sample. The only critical point I see here is the thread is using the reference to the edit-line and accesses it directly. Is that a "non-reentrant code problem" ??

0

There are 0 best solutions below