Handling message replies asynchronously in a separate thread produces a warning

Jamie McCracken jamiemcc at blueyonder.co.uk
Sat Nov 26 04:25:56 PST 2005


Havoc Pennington wrote:
> On Fri, 2005-11-25 at 12:52 +0000, Jamie McCracken wrote:
> 
>>I am being a bit naughty as the message handling func will return 
>>DBUS_HANDLER_RESULT_HANDLED even though the actual reply will be sent 
>>back much later on in a different thread (there is no way round this for 
>>me as its the only way to create a fast stateless multithreaded 
>>middleware server)
> 
> 
> should be fine afaik

okay great just as long as you never assume a message should be 
finalised or a reply has already been sent when the handler returns 
DBUS_HANDLER_RESULT_HANDLED (especially in future versions of Dbus!)

>>By calling dbus_message_ref (message) earlier on in the function the 
>>warning seems to disappear or occur less likely - why?.
> 
> 
> Just the usual unpredictable nature of threads I imagine. Race
> conditions are by definition unpredictable, no?

Okay I have worked it out.

I was originally calling dbus_message_ref *after* placing it on the 
async queue and this produces the warning about 1% of the time. 
Obviously this means there was a context switch to another thread just 
prior to calling  dbus_message_ref and that thread would have sent a 
reply and then called dbus_message_unref on the original message hence 
resulting in the message being finalised prematurely.

By calling dbus_message_ref first in the handler (or before placing it 
on the queue) the warning seems to disappear so everything should be 
okay then :)

> 
> libdbus is not heavily tested in threaded programs; you will need to be
> prepared to dig into it and do some debugging.

I have absolutely hammered it with my test programs with numerous 
threads and I can confirm the threaded behaviour of dbus is both correct 
and solid - I haven't seen one cock up there :)


-- 
Mr Jamie McCracken
http://www.advogato.org/person/jamiemcc/


More information about the dbus mailing list