[systemd-devel] sd-bus connections & authentication timeout

Lennart Poettering lennart at poettering.net
Thu Mar 30 13:57:21 UTC 2017

On Mon, 20.03.17 16:37, Stanislav Angelovič (angelovic.s at gmail.com) wrote:

> Hi,
> We use sd-bus for DBus IPC in our own applications. Some applications of
> ours cause the dbus daemon to issue "Connection has not authenticated soon
> enough, closing it" message, leading to the
> org.freedesktop.DBus.Error.Timeout error at the peer side when the
> connection is used for an IPC call.
> The situation is the following:
> 1. We have a DBus client application.
> 2. A connection is created using sd_bus_open_system() on application
> startup.
> 3. The connection is kept open and unused (i.e. no other sd-bus functions
> invoked on it).
> 4. After 30 seconds, we get the above-mentioned authentication timeout
> message from the dbus daemon, and any attempt to issue calls against a
> service via this connection fails with immediate timeout error as mentioned
> above. However, if the call is made before the 30 seconds authentication
> timeout, the dbus daemon prints nothing and the call succeeds, just like
> any subsequent call, even if issued after 1 hour from the first one.
> Is that correct behavior? We would like to open dbus connections at
> application startup, and use it for communication at any time later,
> depending on user actions. Is there something we are missing?

So, as you already found out, if you explicitly flush the connection
after creating it you can make sure the timeout is not hit.

But do note that creating a connection and not processing it for
indefinite time is problematic: other peers might send you messages,
and if you don't read them in time and your input buffer hence runs
over for too long, then dbus-daemon will assume you#re stuck and
disconnect you.

Hence: if your app runs for a longer time, then either process the
connection from time to time, or disconnect it whenever you don't need
it anymore.

Or to say this differently: if you rely on keeping bg connection
objects around you can do this safely only as long as you do this for
"short-lived" operations.

> And a side suggestion: If we want to close a connection that has not been
> used for any IPC call at all, after some struggling we have realized that
> sd_bus_unref() is not enough, as there is a lingering DBus.Hello reply
> which keeps the reference count on that connection, leading to lingering
> connections. sd_bus_flush_close_unref() is needed instead. That behavior
> could be mentioned in the documentation, e.g. at
> http://0pointer.net/blog/the-new-sd-bus-api-of-systemd.html in the
> reference client implementation, to save others from pain.

Yeah, this has come up before, and we really need better docs for
this, indeed.


Lennart Poettering, Red Hat

More information about the systemd-devel mailing list