Twisted reactor.listenTCP intermittently cannot connect properly

753 Views Asked by At

I'm running a TCP server on linux using reactor.listenTCP.

All works well for hours (or days), then something happens and clients can connect but they are immediately disconnected by the server.

There is nothing in my protocol implementation that commands a disconnect. There is nothing in the client implementations that would cause such a disconnect.

The server intermittently sends some data to all users that it finds in the user list using sendLine(data).

I'm wondering if I am meant to perform some operation when a client disconnects that I've missed amd this is causing socket issues.

class MsgHandler(Protocol):
    def __init__(self, factory):
        self.factory = factory
    def connectionMade(self):
        self.factory.users.append(self)
    def connectionLost(self, reason):
        self.factory.users.remove(self)
    def dataReceived(self, data):
        pass

class outputTcpServerFactory(Factory):
    def __init__(self, users):
        self.users = users
    def buildProtocol(self, addr):
        return MsgHandler(self) 

class Mainloop(object):
    def __init__(self):
        self.users = []
        s = reactor.listenTCP(port, outputTcpServerFactory(self.users))
        reactor.run()

---> Elsewhere in the mainLoop class is code that periodically runs:

for client in self.users:
    client.sendLine(data)
1

There are 1 best solutions below

1
On

You should always enable logging so that you can see unhandled exceptions that the application encounters. These usually make it obvious why something is broken.