Issue with socket multithreading example

Hey i don’t know what happens and how to debug my problem since my debugger somehow cannot debug the subthreads.

I wrote a simple application to test multithreading with sockets using Qt with PySide6 bindings in a QtQuick application.
There is a simple GUI and a controller class. The controller sends data to my sender thread using a signal.
When I execute my app I see periodically empty messages. So idle works as expected.
When I send real messages I can see that the signal is emitted properly, the slot is called properly. But after a short time I get an error:

“…Sender.py”, line 40, in run
data = client.recv(self.buffersize)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ConnectionResetError: [Errno 54] Connection reset by peer.

I ensured that the string does not exceed buffersize.

So here is my Python code of sender:

from PySide6 import QtCore
from PySide6.QtQml import QmlElement
import socket

QML_IMPORT_NAME = "io.qt.textproperties"
QML_IMPORT_MAJOR_VERSION = 1

u/QmlElement
class Sender(QtCore.QThread):
    dataReceived = QtCore.Signal(str)
    dataSent = QtCore.Signal()
    bound = QtCore.Signal()
    def __init__(self):
        super().__init__(None)
        self.host = '0.0.0.0'
        self.port = 9999
        self.buffersize = 1024
        self.data_to_send = None
        self.ready_to_send = False
        print("Sender: initialization finished")


    def run(self):
        print("Sender: run started")
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server:
            print("Sender: server created")
            server.bind((self.host, self.port))
            print("Sender: server bound")
            self.bound.emit()
            print("Sender: server listening")
            server.listen()
            print("Sender: server accepting")
            client, addr = server.accept()
            while True:
                if self.isInterruptionRequested():
                    print("Sender: interruption requested, breaking loop")
                    return
                print("Sender: server accepted, receiving data")
                data = client.recv(self.buffersize)
                self.dataReceived.emit(data)
                if self.ready_to_send:
                    print(f"Sender: sending data: {self.data_to_send}")
                    client.sendall(bytes(self.data_to_send, 'utf-8'))
                    self.data_to_send = None
                    self.ready_to_send = False
                    print("Sender: data sent")
                    self.dataSent.emit()
                self.sleep(2)

    QtCore.Slot(str)
    def handle_new_message(self, msg:str):
        print(f"Sender: Slot handle_new_message called with: {msg}")
        self.data_to_send = msg
        self.ready_to_send = True

And here is my Python code from sender:

from PySide6 import QtCore
import socket

class Receiver(QtCore.QThread):
    dataReceived = QtCore.Signal(str)
    dataSent = QtCore.Signal()
    def __init__(self):
        super().__init__(None)
        self.host = '127.0.0.1'
        self.port = 9999
        self.buffersize = 1024


    def run(self):
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client:
            client.connect((self.host, self.port))
            while True:
                if self.isInterruptionRequested:
                    return
                data = client.recv(self.buffersize).decode('utf-8')
                client.sendall(bytes(data))
                self.dataSent.emit()
                self.sleep(0.5)

So my sent message should be echoed.
Platform: MacOS Ventura (MB Pro 2018 with intel chip)
Python: 3.11

Hey i don’t know what happens and how to debug my problem since my
debugger somehow cannot debug the subthreads.

There are always print() calls if you need to trace things. Ah, I see
you’re do that already.

I wrote a simple application to test multithreading with sockets using Qt with PySide6 bindings in a QtQuick application.
There is a simple GUI and a controller class. The controller sends data to my sender thread using a signal.
When I execute my app I see periodically empty messages. So idle works as expected.
When I send real messages I can see that the signal is emitted properly, the slot is called properly. But after a short time I get an error:

“…Sender.py”, line 40, in run
data = client.recv(self.buffersize)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ConnectionResetError: [Errno 54] Connection reset by peer.

I ensured that the string does not exceed buffersize.

This error is nothing to do with the buffer size. It means that the
client has closed its side of the TCP connection. You should handle this
exception and exit your loop. With warning message of some kind if that
is unexpected. Something shaped like:

 try:
     data = client.recv(self.buffersize)
 except ConnectionResetError as e:
     # complain (if this is considered abnormal)
     warning("client no longer connected: %s", e)
     # leave the loop
     break

Cheers,
Cameron Simpson cs@cskk.id.au

Yes… but my goal is not to handle the exception because the exception occurs immediately when the sender wants to retreive data from receiver.

I tried the code ln my windows machine with the same issue. So it must be an basic error which i still dont get.