2011/8/16 Andy Joslin <libvirt(a)bandjos.com>:
The documentation says something that concerns me:
http://libvirt.org/html/libvirt-libvirt.html#virConnectClose
"It is possible for the last virConnectClose to return a positive
value if some
other object still has a temporary reference to the connection, but
the
application should not try to further use a connection after the
virConnectClose that matches the initial open."
The application I am working on requires that I know when the last thread
using this connection goes away, so I can properly cleanup some overhead.
If I can't trust the return value of this call, how will I know that the
connection is indeed torn down?
Under what circumstances will the virConnectClose behave this way?
Simplified example (omitting all error handling) that makes
virConnectClose return 0:
// opens a connection, ref count is 1
virConnectPtr conn = virConnectOpen("qemu:///system", 0);
// gets domain 17, conn ref count is 2, because dom depends on conn
virDomainPtr dom = virDomainLookupByID(conn, 17);
...
// free dom, conn ref count is 1 again, because dom removes ref from conn
virDomainFree(dom);
// remove a ref from conn, ref count is 0, really close the connection, ret is 0
int ret = virConnectClose(conn);
If the order of the last two calls is changed (it's totally okay to do
this) than virConnectClose returns 1:
...
// remove a ref from conn, ref count is 1, don't close the connection, ret is 1
int ret = virConnectClose(conn);
// free dom, conn ref count is 0, really close the connection dom depended on
virDomainFree(dom);
There is no way yet (like a callback or something) libvirt could tell
you when the connection is really closed in cases when virConnectClose
returned > 0.
You could achieve that virConnectClose is the one that really closes
the connection when you ensure that all objects (domains, storage
pools and volume, networks etc) obtained using that connection are
freed before calling virConnectClose. Then virConnectClose will be the
one that removes the last reference from the connection and closes it.
--
Matthias Bolte
http://photron.blogspot.com