[Twisted-Python] connectionLost not always getting called?

Andrew Bennetts andrew-twisted at puzzling.org
Sun Jan 11 20:59:06 EST 2009

Rob Bredow wrote:
> web-browser does not always trigger a connectionLost call.  I tracked down one
> specific instance yesterday where the server thought the client was still
> connected even though the client had been gone for 30+ minutes--but after a few
> hours the server noticed that the client was gone and then connectionLost was
> called like normal.  That made me think that it might be a problem on the
> server side and maybe not client related.
> I'm starting on a workaround now where the client will issue a "PING" every 30
> seconds and the server will use the ping information to manually trigger
> loseConnection when needed.  Are there a better workarounds for this, better
> practices or some error checking I can do to avoid these intermidant laggy
> connectionLost calls?

This is a property of TCP.  If a peer drops off the network suddenly, your side
never receives a RST packet, and if your side never tries sending to a dead
connection like this then TCP won't notice that the peer is now unreachable.

You can probably enable something called "TCP keepalives" in your OS, but
they'll still tend to take a few hours to notice an unreachable peer.  So your
workaround is about as good as it gets.


More information about the Twisted-Python mailing list