[Twisted-web] Xml-Rpc server troubles

Eric Mangold teratorn at world-net.net
Tue Nov 16 14:35:50 MST 2004

Andrea Nicolini wrote:

> Hi All,
> I'm trying to debug a XML-RPC server I wrote because sometimes it gets  
> stuck and I don't know why.
> I'm not a twisted expertise so I hope someone in this list can help me.
> I use the server to give access to a MySql database via XML-RPC, the  
> client are written in python and java.
> The server runs fine for some days and then suddenly it hangs.
> When the server hangs I can connect to the server port but when I call a  
> remote method the server doesn't respond.
> The server is not throwing any exception.
> I'm running the server on a RH9 using Python 2.2 and Twisted 1.3.
> Here's a snippet of code:
> def xmlrpc_createjob(self,hash):
>                 tscmfuncs.call_debug(debug,foreground)
>                 d = self.dbpool.runInteraction(self.createjob,hash)
>                 return d.addCallback(self.returnData)

Where is your Errback? If this doesn't work then you'll never know until
the Deferred gets GC'ed, if it ever does, and even then only sometimes.

Deferred's have __del__ defined to hopefully, maybe, warn the user if there
were errors that never got handled. This is of course not guarenteed to  

Always, always, add Errbacks.

> def createjob(self,cur,hash):
>                 """Method to create a new job -> returns the new job's  
> id"""
>                 try:
>                         qparam = ()
>                         cur.execute('SELECT * FROM jobtable')
>                         for field in cur.description:
>                                 f = field[0]
>                                 if hash.has_key(f):
>                                         if type(hash[f]) == type(u''):
>                                                 qparam += (hash 
> [f].encode('iso-8859-1'),)
>                                         else:
>                                                 qparam += (hash[f],)
>                                 else:
>                                         qparam += (None,)
>                         cur.execute("INSERT INTO jobtable VALUES (%s,% 
> s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,% 
> s)",qparam)
>                         cur.execute("SELECT LAST_INSERT_ID() FROM  
> jobtable")
>                         curid = cur.fetchone()[0]
>                         cur.close()
>                 except Exception,err:
>                         return 0
>                 else:
>                         return curid
> I don't have any idea of what can cause this behaviour and how to debug  
> the server.
> Any idea?

I don't know much about Twisted's RDBMS stuff.  I'm assuming createjob  
gets run in a thread.
Maybe these threads are getting stuck? *shrug*.  Apparently the Deferred's  
aren't firing, but you
could have a timeout function to see if they really are getting fired but  
lost before the
result gets send back by xml-rpc.

Perhaps something like this (untested):

def xmlrpc_createjob(self,hash):
     d = self.dbpool.runInteraction(self.createjob,hash)
     reactor.callLater(5, checkIfDeferredFired, d)
     return d.addCallback(self.returnData)

def checkIfDeferredFired(d):
     if not d.called:
         log.msg('Deferred not fired after 5 seconds')

     #omit this if it will fill your logs too much
         log.msg('Deferred fired OK')

Good luck,

More information about the Twisted-web mailing list