<div dir="auto"><p dir="ltr">Thank you Lennart for detailed explanation. I realized a few things in sd-bus behavior (and in my wrong approach to default bus -- yes I forgot about hello message) when I debugged the thing earlier today, and now your explanation cleared that up pretty nicely to me.<br></p>
<div align="left" dir="auto"><p dir="ltr">I see again that sd-bus design is well thought-out, I'm glad to work with it, and I hope to keep up to high design quality in c++ binding on top of it (the quality is also increasing the more I understand some details of sd-bus).<br>
</p>
</div><p dir="ltr"></p>
<div align="left" dir="auto"><p dir="ltr">Perhaps more detailed docs on sd-bus would help a lot.. <br>
</p>
</div><p dir="ltr"></p>
<div align="left" dir="auto"><p dir="ltr">I will fix the approach to default busses on my side. This is a very special corner case in sdbus-c++ used in order to be able to create plain empty messages, which are then used as storage of Variant class, without user having to provide an existing bus connection themselves. Perhaps that could be refactored in future -- but disadvantage is that users won't be able to create self-contained Variants (they will have to provide their bus connection explicitly to create a Variant).<br>
</p>
</div><p dir="ltr"></p>
<div align="left" dir="auto"><p dir="ltr">Thanks a lot! Take care.</p></div><br><div class="gmail_quote" dir="auto"><div dir="ltr" class="gmail_attr">On Thu, May 9, 2019, 17:02 Lennart Poettering <<a href="mailto:lennart@poettering.net">lennart@poettering.net</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On Mi, 08.05.19 22:50, Stanislav Angelovič (<a href="mailto:angelovic.s@gmail.com" target="_blank" rel="noreferrer">angelovic.s@gmail.com</a>) wrote:<br>
<br>
> Heya,<br>
><br>
> when writing sdbus-c++, we've observed that sd_bus_default_system function<br>
> called in a fresh new thread returns a bus with initial ref count 2. We<br>
> built our code upon that assumption -- we had to unref the bus twice when<br>
> the thread local storage got freed, otherwise we'd have gotten<br>
> memory leaks.<br>
<br>
Uh, no, this was never the right thing to do.<br>
<br>
> Now it broke, however, because in systemd v242, the initial ref count is 1.<br>
> Is that a conscious change? Can we build upon the guarantee that it will<br>
> always be 1? (1 actually seems reasonable, unlike 2).<br>
<br>
Originally, in sd-bus each bus message (i.e. each sd_bus_message<br>
object) keeps a ref on the bus connection (i.e. each sd_bus object) it<br>
belongs to. Moreover, if a message is queued into a bus connection it<br>
is referenced by it. Thus you have a ref cycle, as long as a mesage is<br>
queued into a bus and not processed yet. The intention then was that<br>
whenever you want to get rid of a bus at the end of your<br>
thread/process you'd flush out the bus anyway (because otherwise you<br>
lose queued but unwritten messages), at which point the cycle would be<br>
cleared up as soon as all messages are written.<br>
<br>
Also note, that when a bus connection is set up a "Hello" message is<br>
immediately enqueued into it (this is required by the spec). Thus, by<br>
default there'd be two references onto the bus connection object: the<br>
one you own yourself as the caller, and the one from the Hello bus<br>
message queued onto it.<br>
<br>
To flush out messages you should use the sd_bus_flush() call. it's<br>
blocking, and all it does is force out all queued outgoing messages<br>
that are not written yet, thus emptying the write queue. Then, call<br>
sd_bus_close(), to explicitly terminate the connection. This will<br>
empty the read queue too. At this point there are no messages queued<br>
anymore, i.e. all refs on the bus object must be held by you and not<br>
internally. Finally, get rid of the whole thing by doing<br>
sd_bus_unref(). Since these three calls are often combined, there's<br>
sd_bus_flush_close_unref() to combine them into one.<br>
<br>
Calling sd_bus_flush() is entirely optional btw, it's sufficient to just<br>
call sd_bus_close(). However in that case and pending unwritten<br>
messages are just forgotten, and not written to the connection socket,<br>
and never seen by the receiving side.<br>
<br>
Now you might wonder, why doesn't sd_bus_unref() always implicitly<br>
call sd_bus_flush()? Reffing/unreffing should not be blocking, that's<br>
why. Moreover, the concept of "default" busses of a thread is that<br>
various bits and pieces running in the thread could quickly ask for<br>
the default bus connection, do some stuff with it, enqueue a message<br>
or two, then unref it again. Other code might then pick it up again,<br>
enqueue some more messages on them, unref it again, and so on. Then,<br>
when the thread is about to end, there might be a number of messages<br>
queued: before exiting from the thread they should be flushed out, but<br>
only then, there's no need to do so earlier. Thus, in the earlier<br>
uses of the bus connection your'd just unref the bus at the end, but<br>
when you are finally done with the default connection altogether youd'<br>
use the more powerful sd_bus_flush_close_unref() instead, and do all<br>
the work synchronously, that the earlier users didn't bother to wait<br>
for.<br>
<br>
Note that when an sd_bus connection is attached to an sd_event event<br>
loop, the sd_bus_flush() + sd_bus_close() is done automatically when<br>
the "exit" phase of the event loop is entered.<br>
<br>
Now, as it turns out, many users of sd-bus didn't get all of the above<br>
right: they simply unreffed the bus after use, and enver bothered to<br>
flush out any queued messages not written yet, and thus leaked<br>
memory. With 1b3f9dd759ca0ea215e7b89f8ce66d1b724497b9 that was<br>
addressed: now sd-bus tries to be a bit smarter than before: if it<br>
notices, that a bus connection is only referenced by a message queued<br>
into it but nothing else, and that there's no chance that it can be<br>
referenced again, we'll detect this and release the entire object<br>
automatically, knowing that it could never correctly be flushed out<br>
anyway, since noone owns a ref to it anymore. Or in other words: the<br>
ref cycles are now detected, and dealt with.<br>
<br>
What does this mean for users? Well, if you are using sd-bus correctly<br>
not much changes: make sure you flush/close your connections at the<br>
end of each thread, to make sure you don't lose messages you enqueued<br>
but that were never written out. If you don't do that correctly though<br>
it's not a memory leak anymore as before if you forget to do that.<br>
<br>
Lennart<br>
<br>
--<br>
Lennart Poettering, Berlin<br>
</blockquote></div></div>