[Twisted-Python] RE: Write into a persistent connection before stopping

Boeuf, Jean-Francois jean-francois.boeuf at eads.com
Mon Jan 26 10:12:57 EST 2009


Hi,

As i didn't get any answer to my problem i writed a little test code to
reproduce it (I can't divulgate the production code, and then it would be to
much complicated to expose the matter). You can just run it using twistd -y
test.py and then connect to the 8080 tcp port with a browser. As we can see
in the browser the connection is active and periodicaly writed. The logs
indicate that the conections are notifyied before port and connection close,
but the browser never receives the notification.

Thanks for your help

##################test.py begins here#######################
from twisted.application import internet, service
from twisted.internet import reactor
from twisted.web import resource, server, static
import threading

application = service.Application("test")

LISTENING   = list()
CHECK       = threading.Event()

def writeToActiveConnections():
    while not CHECK.isSet():
        for request in LISTENING:
            request.write("active<br />")
        CHECK.wait(2)

def onStop():
    print "ON STOP CALLED"
    CHECK.set()
    for request in LISTENING:
        request.write("<strong>Connection closed because of server
shutdown</strong>")
    print "ALL CONNECTIONS HAVE BEEN NOTIFIED"

def onError(_error, _request):
    print "Connection Closed %s" % _error
    LISTENING.remove(_request)

class ListenRessource(resource.Resource):
    def render_GET(self, _request):
        _request.write("listen connection opened<br />")
        LISTENING.append(_request)
        d = _request.notifyFinish()
        d.addCallback(LISTENING.remove, _request)
        d.addErrback(onError, _request)
        return server.NOT_DONE_YET

class MainRessource(resource.Resource):
    def render_GET(self, _request):
        return """
<html>
    <head></head>
    <body>
        <h1>Test page</h1>
        <iframe src=/listen></iframe>
    </body>
</html>
        """

class TestSite(server.Site):
    def __init__(self):
        root = static.File("/tmp")
        root.putChild("test", MainRessource())
        root.putChild("listen", ListenRessource() )
        server.Site.__init__(self, root)

class TestService(internet.TCPServer):
    def __init__(self):
        internet.TCPServer.__init__(
            self,
            8080,
            TestSite()
        )
        reactor.callInThread(writeToActiveConnections)
        reactor.addSystemEventTrigger('before', 'shutdown', onStop)

TestService().setServiceParent(application)
##################test.py ends here#######################


> -----Message d'origine-----
> De : Boeuf, Jean-Francois 
> Envoyé : vendredi 16 janvier 2009 13:26
> À : 'twisted-python at twistedmatrix.com'
> Objet : Write into a persistent connection before stopping
> 
>  
> Hello,
> 
> I have an application using twisted in which the web browser 
> opens a persistent connection when a user logs in (COMET 
> model). I want to write data into active persistent 
> connections at server stop to notify client to logout properly.
> 
> To do that, I add a system event trigger to the reactor 
> calling the method that closes session before shutdown when I 
> start the webService. This method is called when twisted 
> receives a SIGTERM and I can see in twistd.log traces of 
> writing to the persistent connexion before services stops and 
> connexions are closed. But the web browser never receives 
> this content and listening the traffic between server and 
> client with wireshark, I can't see anything else than the 
> [FIN, ACK] packets that are sent when server ends all 
> connections (delayed for maximum clarity in the logs).
> 
> The same method is called to logout client properly when 
> session expires and does work fully.
> 
> You can see below the twistd.log file when the servers stops.
> 
> Thank you for your help.
> 
> Regards
> 
> Jean-François
> 
> 
> twistd.log
> ----------
> 2009-01-16 13:26:16+0100 [-] Received SIGTERM, shutting down.
> 2009-01-16 13:26:16+0100 [-] 10.37.68.212 - - 
> [16/Jan/2009:12:26:16 +0000] "GET 
> /sim/listen?sourceScreen=216 HTTP/1.1" 200 166 
> "http://peyo:8080/sim?id=216" "Mozilla/5.0 (Windows; U; 
> Windows NT 5.1; fr; rv:1.8.1.16) Gecko/20080702 Firefox/2.0.0.16"
> 2009-01-16 13:26:16+0100 [-] 10.37.68.212 - - 
> [16/Jan/2009:12:26:16 +0000] "GET 
> /sim/listen?sourceScreen=217 HTTP/1.1" 200 166 
> "http://peyo:8080/sim?id=217" "Mozilla/5.0 (Windows; U; 
> Windows NT 5.1; fr; rv:1.8.1.16) Gecko/20080702 Firefox/2.0.0.16"
> 2009-01-16 13:26:16+0100 [-] 10.37.68.212 - - 
> [16/Jan/2009:12:26:16 +0000] "GET 
> /sim/listen?sourceScreen=218 HTTP/1.1" 200 166 
> "http://peyo:8080/sim?id=218&screen0=216&screen1=217&nbScreen=
> 2" "Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.8.1.16) 
> Gecko/20080702 Firefox/2.0.0.16"
> 2009-01-16 13:26:25+0100 [-] (Port 8080 Closed)
> 2009-01-16 13:26:25+0100 [-] Stopping factory 
> <cockpitServer.userSessionManagement.cockpitSite.CockpitSite 
> object at 0xb6db9e6c>
> 2009-01-16 13:26:25+0100 [-] (Port 8082 Closed)
> 2009-01-16 13:26:25+0100 [-] Stopping factory 
> <cockpitServer.soapService.SoapSite object at 0xb7092d0c>
> 2009-01-16 13:26:25+0100 [-] Main loop terminated.
> 2009-01-16 13:26:25+0100 [-] Server Shut Down
> 




More information about the Twisted-Python mailing list