From nobody Sun Feb 8 04:11:54 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1491316310576131.53313808523126; Tue, 4 Apr 2017 07:31:50 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 0728D65CFF; Tue, 4 Apr 2017 14:31:49 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C9BF299EAD; Tue, 4 Apr 2017 14:31:48 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 8068318523C5; Tue, 4 Apr 2017 14:31:48 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v34EVi3x024995 for ; Tue, 4 Apr 2017 10:31:44 -0400 Received: by smtp.corp.redhat.com (Postfix) id 60E1090C78; Tue, 4 Apr 2017 14:31:44 +0000 (UTC) Received: from dhcp-17-113.lcy.redhat.com (unknown [10.42.17.113]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9CEF08FE4E; Tue, 4 Apr 2017 14:31:41 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 0728D65CFF Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 0728D65CFF From: "Daniel P. Berrange" To: libvir-list@redhat.com Date: Tue, 4 Apr 2017 15:31:28 +0100 Message-Id: <20170404143134.12141-2-berrange@redhat.com> In-Reply-To: <20170404143134.12141-1-berrange@redhat.com> References: <20170404143134.12141-1-berrange@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Cc: Wojtek Porczyk Subject: [libvirt] [PATCH v3 1/7] Allow for ff callbacks to be called by custom event implementations X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Tue, 04 Apr 2017 14:31:49 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" From: Wojtek Porczyk The documentation says: > If the opaque user data requires free'ing when the handle is > unregistered, then a 2nd callback can be supplied for this purpose. > This callback needs to be invoked from a clean stack. If 'ff' > callbacks are invoked directly from the virEventRemoveHandleFunc they > will likely deadlock in libvirt. And they did deadlock. In removeTimeout too. Now we supply a custom function to pick it from the opaque blob and fire. Signed-off-by: Wojtek Porczyk Signed-off-by: Daniel P. Berrange --- libvirt-override.c | 68 ++++++++++++++++++++++++++++++-------------------= ---- libvirt-override.py | 23 ++++++++++++++++++ sanitytest.py | 3 ++- 3 files changed, 63 insertions(+), 31 deletions(-) diff --git a/libvirt-override.c b/libvirt-override.c index 93c7ef0..a762941 100644 --- a/libvirt-override.c +++ b/libvirt-override.c @@ -5223,6 +5223,9 @@ libvirt_virEventAddHandleFunc(int fd, =20 VIR_PY_TUPLE_SET_GOTO(pyobj_args, 3, cb_args, cleanup); =20 + /* If changing contents of the opaque object, please also change + * virEventInvokeFreeCallback() in libvirt-override.py + */ VIR_PY_TUPLE_SET_GOTO(cb_args, 0, libvirt_virEventHandleCallbackWrap(c= b), cleanup); VIR_PY_TUPLE_SET_GOTO(cb_args, 1, libvirt_virVoidPtrWrap(opaque), clea= nup); VIR_PY_TUPLE_SET_GOTO(cb_args, 2, libvirt_virFreeCallbackWrap(ff), cle= anup); @@ -5279,10 +5282,7 @@ libvirt_virEventRemoveHandleFunc(int watch) { PyObject *result =3D NULL; PyObject *pyobj_args; - PyObject *opaque; - PyObject *ff; int retval =3D -1; - virFreeCallback cff; =20 LIBVIRT_ENSURE_THREAD_STATE; =20 @@ -5292,20 +5292,11 @@ libvirt_virEventRemoveHandleFunc(int watch) VIR_PY_TUPLE_SET_GOTO(pyobj_args, 0, libvirt_intWrap(watch), cleanup); =20 result =3D PyEval_CallObject(removeHandleObj, pyobj_args); - if (!result) { + if (result) { + retval =3D 0; + } else { PyErr_Print(); PyErr_Clear(); - } else if (!PyTuple_Check(result) || PyTuple_Size(result) !=3D 3) { - DEBUG("%s: %s must return opaque obj registered with %s" - "to avoid leaking libvirt memory\n", - __FUNCTION__, NAME(removeHandle), NAME(addHandle)); - } else { - opaque =3D PyTuple_GetItem(result, 1); - ff =3D PyTuple_GetItem(result, 2); - cff =3D PyvirFreeCallback_Get(ff); - if (cff) - (*cff)(PyvirVoidPtr_Get(opaque)); - retval =3D 0; } =20 cleanup: @@ -5350,6 +5341,9 @@ libvirt_virEventAddTimeoutFunc(int timeout, =20 VIR_PY_TUPLE_SET_GOTO(pyobj_args, 2, cb_args, cleanup); =20 + /* If changing contents of the opaque object, please also change + * virEventInvokeFreeCallback() in libvirt-override.py + */ VIR_PY_TUPLE_SET_GOTO(cb_args, 0, libvirt_virEventTimeoutCallbackWrap(= cb), cleanup); VIR_PY_TUPLE_SET_GOTO(cb_args, 1, libvirt_virVoidPtrWrap(opaque), clea= nup); VIR_PY_TUPLE_SET_GOTO(cb_args, 2, libvirt_virFreeCallbackWrap(ff), cle= anup); @@ -5403,10 +5397,7 @@ libvirt_virEventRemoveTimeoutFunc(int timer) { PyObject *result =3D NULL; PyObject *pyobj_args; - PyObject *opaque; - PyObject *ff; int retval =3D -1; - virFreeCallback cff; =20 LIBVIRT_ENSURE_THREAD_STATE; =20 @@ -5416,20 +5407,11 @@ libvirt_virEventRemoveTimeoutFunc(int timer) VIR_PY_TUPLE_SET_GOTO(pyobj_args, 0, libvirt_intWrap(timer), cleanup); =20 result =3D PyEval_CallObject(removeTimeoutObj, pyobj_args); - if (!result) { + if (result) { + retval =3D 0; + } else { PyErr_Print(); PyErr_Clear(); - } else if (!PyTuple_Check(result) || PyTuple_Size(result) !=3D 3) { - DEBUG("%s: %s must return opaque obj registered with %s" - "to avoid leaking libvirt memory\n", - __FUNCTION__, NAME(removeTimeout), NAME(addTimeout)); - } else { - opaque =3D PyTuple_GetItem(result, 1); - ff =3D PyTuple_GetItem(result, 2); - cff =3D PyvirFreeCallback_Get(ff); - if (cff) - (*cff)(PyvirVoidPtr_Get(opaque)); - retval =3D 0; } =20 cleanup: @@ -5558,6 +5540,31 @@ libvirt_virEventInvokeTimeoutCallback(PyObject *self= ATTRIBUTE_UNUSED, return VIR_PY_INT_SUCCESS; } =20 +static PyObject * +libvirt_virEventInvokeFreeCallback(PyObject *self ATTRIBUTE_UNUSED, + PyObject *args) +{ + PyObject *py_f; + PyObject *py_opaque; + virFreeCallback cb; + void *opaque; + + if (!PyArg_ParseTuple(args, (char *) "OO:virEventInvokeFreeCallback", + &py_f, &py_opaque)) + return NULL; + + cb =3D (virFreeCallback) PyvirEventHandleCallback_Get(py_f); + opaque =3D (void *) PyvirVoidPtr_Get(py_opaque); + + if (cb) { + LIBVIRT_BEGIN_ALLOW_THREADS; + cb(opaque); + LIBVIRT_END_ALLOW_THREADS; + } + + return VIR_PY_INT_SUCCESS; +} + static void libvirt_virEventHandleCallback(int watch, int fd, @@ -9572,6 +9579,7 @@ static PyMethodDef libvirtMethods[] =3D { {(char *) "virEventAddTimeout", libvirt_virEventAddTimeout, METH_VARAR= GS, NULL}, {(char *) "virEventInvokeHandleCallback", libvirt_virEventInvokeHandle= Callback, METH_VARARGS, NULL}, {(char *) "virEventInvokeTimeoutCallback", libvirt_virEventInvokeTimeo= utCallback, METH_VARARGS, NULL}, + {(char *) "virEventInvokeFreeCallback", libvirt_virEventInvokeFreeCall= back, METH_VARARGS, NULL}, {(char *) "virNodeListDevices", libvirt_virNodeListDevices, METH_VARAR= GS, NULL}, #if LIBVIR_CHECK_VERSION(0, 10, 2) {(char *) "virConnectListAllNodeDevices", libvirt_virConnectListAllNod= eDevices, METH_VARARGS, NULL}, diff --git a/libvirt-override.py b/libvirt-override.py index 63f8ecb..b0b24ef 100644 --- a/libvirt-override.py +++ b/libvirt-override.py @@ -211,3 +211,26 @@ def virEventAddTimeout(timeout, cb, opaque): ret =3D libvirtmod.virEventAddTimeout(timeout, cbData) if ret =3D=3D -1: raise libvirtError ('virEventAddTimeout() failed') return ret + + +# +# a caller for the ff callbacks for custom event loop implementations +# + +def virEventInvokeFreeCallback(opaque): + """ + Execute callback which frees the opaque buffer + + @opaque: the opaque object passed to addHandle or addTimeout + + WARNING: This function should not be called from any call by libvirt's + core. It will most probably cause deadlock in C-level libvirt code. + Instead it should be scheduled and called from implementation's stack. + + See https://libvirt.org/html/libvirt-libvirt-event.html#virEventAddHan= dleFunc + for more information. + + This function is not dependent on any event loop implementation. + """ + + libvirtmod.virEventInvokeFreeCallback(opaque[2], opaque[1]) diff --git a/sanitytest.py b/sanitytest.py index a140ba2..7183baa 100644 --- a/sanitytest.py +++ b/sanitytest.py @@ -349,7 +349,8 @@ for klass in gotfunctions: continue for func in sorted(gotfunctions[klass]): # These are pure python methods with no C APi - if func in ["connect", "getConnect", "domain", "getDomain"]: + if func in ["connect", "getConnect", "domain", "getDomain", + "virEventInvokeFreeCallback"]: continue =20 key =3D "%s.%s" % (klass, func) --=20 2.9.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list