[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: [libvirt] virConnectClose question



2010/10/19 Radek Hladik <radek eadresa cz>:
> Hello,
>        I did find out that in some cases virConnectClose does not close the
> connection at all. It only returns -1 which by the documentation means
> error:
> Returns: 0 in case of success or -1 in case of error.

Check virGetLastError() for the error.

> But there is no other indication of what error it is. I am almost sure that
> it is because some open domains or other libvirt objects. If this is the
> case it would be good to add notice like: "You need to free all
> domains,....,etc before calling virConnectClose". And is there any
> possibility how to force closing the connection? I know that it is a good
> programming manner to close all objects before closing the master object but
> I did run into this in php-libvrit development and it is quite painful.
> PHP is keeping a list of all resources (objects from libvirt) and is calling
> my destructors to close it. Unfortunately there are some problems with this:
> * when garbage collector clears my resource my destructor is not called. It
> is probably my mistake that I do not set it correctly somewhere but I am not
> able to find out where.
> * I can not influence the order of calls. It seems that there were some
> discussions and that for quite some time PHP is calling destructors in
> reverse order of creation. That would be good but I am not sure whether it
> is granted.
> The problem with this all is that if I for some reason fail to free some
> domain object the connection is not closed. But as PHP does reuse threads
> the connection will be open forever and connection limit for libvirtd get
> drained very fast.
> So basically my question is: should I implement some list of all objects I
> did create and free them before calling virConnectClose or is there any
> other way how to "terminate" libvirt connection for good?
>
> Radek
>

Okay, all libvirt objects like connection, domain, storage pool etc
are reference counted. When you lookup a domain over a connection then
the domain adds an additional reference to the connection. This
ensures that the connection is open as long as there are objects that
have been looked up over it.

You don't need to free all objects before you can close the
connection. You just need to match all virConnectOpen calls by a
virConnectClose call and all lookup calls for other objects by the
matching free calls.

virConnectPtr conn = virConnectOpen("qemu:///system", 0); // conn.refs = 1
virDomainPtr domain = virDomainLookupByName(conn, "vm1"); // conn.refs
= 2, domain.refs = 1
...
virConnectClose(conn); // conn.refs = 1, but conn is still open
because domain has a reference to it
virDomainFree(domain); // conn.refs = 0, domain.refs = 0, domain gets
freed and releases its reference to conn, no conn gets closed

So virConnectClose returning -1 has nothing to do with the order of
close/free calls.

I think you can only make virConnectClose return -1 when you call it
on an invalid or already closed connection.

Matthias


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]