dbus_connection_read_write_dispatch sometimes causing 100% cpu usage

Burton Samograd burton at userful.com
Fri Sep 10 13:04:25 PDT 2010

Havoc Pennington <hp at pobox.com> writes:

> Hi,
> On Fri, Sep 10, 2010 at 2:56 PM, Burton Samograd <burton at userful.com> wrote:
>> For the first part, I am having trouble finding how this code will ever
>> exit the loop, since connection->dispatch_acquired is never set
>> anywhere.  This seems like a standard infinite loop, but maybe someone
>> could shed some light on how this is supposed to exit.
> dispatch_acquired does get set, see
> _dbus_connection_acquire_dispatch() and
> _dbus_connection_release_dispatch().

I can set they're set there, but I'm just not sure how they would be set
in the loop that I provided given that I was not running in a
multi-threaded environment...

>> but since thread_functions.condvar_wait is 0, the code is never executed
>> (since I don't call dbus_init_threads (or whatever that function is
>> called) in my code since it is single threaded)...
> This makes me suspect you're recursing and re-entering dispatch
> (trying to _dbus_connection_acquire_dispatch() twice without releasing
> it)

Not sure how that's happening, but it might be due to a setjmp/longjmp
being called from an X IO error handler (which I'm not sure is being
called asynchronously or not...the X docs aren't clear on that).

>> Here is a stack trace from where I am in the loop:
>> #0  _dbus_connection_acquire_dispatch (connection=0x1053c00) at dbus-connection.c:3864
>> #1  0x00007f96c57726bb in dbus_connection_dispatch (connection=0x1053c00) at dbus-connection.c:4303
>> #2  0x00007f96c5770654 in _dbus_connection_read_write_dispatch (connection=0x1053c00,
>>    timeout_milliseconds=100, dispatch=1) at dbus-connection.c:3431
>> #3  0x00007f96c5770a22 in dbus_connection_read_write_dispatch (connection=0x1053c00,
>>    timeout_milliseconds=100) at dbus-connection.c:3513
> It looks like you somehow have read_write_dispatch from inside
> read_write_dispatch, and the inner read_write_dispatch is waiting for
> the outer one to give up dispatch_acquired (i.e.
> _dbus_connection_release_dispatch())

I see that now, but like I said, I have no idea why the function is
being called twice since I only call it once in the main loop of my code
and not anywhere else.

I did put in a call to dbus_init_threads_default and it seems to have
caused the problem no to happen in my testing, but I'm a bit confused as
to why it should since my program is single threaded.  Not that it
matters if it works, but it's just a bit odd.

Thanks for the help.

Burton Samograd

More information about the dbus mailing list