[Twisted-Python] Re: How can I disconnect ssl connection from the client side?

Johann Borck johann.borck at densedata.com
Mon Feb 2 08:45:40 EST 2009

Atsuo Ishimoto wrote:

> class Client(basic.LineReceiver):
>     def connectionMade(self):
>         class dmyfile:
>             def read(self, n):
>                 if CANCELED:
>                     return
>                 else:
>                     return '1'
>         s = basic.FileSender()
>         print "start sending"
>         def f1(lastChunk):
>             print "finished"
> def cancel():
>     print "cancel", conn.state
>     global CANCELED
>     CANCELED = True
>     conn.disconnect()
I think the problem is that you call conn.disconnect before the 
FileSender has a chance to call unregisterProducer on the transport (in 
real life before it has sent the whole file). If the producer is not 
unregistered, the doWrite() method called from the reactor doesn't even 
check if the connection is in 'disconnecting' state, assumes there's 
more data to come, and since no further events occur on the channel, the 
connection just remains open. You can check that by manually writing one 
more byte to the transport in f1, after FileSender has unregistered 
itself. The correct way is to call 
conn.disconnect()/transport.loseConnection() in f1 instead of in cancel.

from t.i.abstract.FileDescriptor.loseConnection.__doc__:
"... If you have a producer registered, the connection won't be closed 
until the producer is finished. Therefore, make sure you unregister your 
producer when it's finished, or the connection willnever close ..."

hth, Johann
> reactor.callLater(2, cancel) # disconnect 2 sec later
> reactor.run()
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python

More information about the Twisted-Python mailing list