[PATCH] rpc: avoid leak of GSource in use for interrupting main loop

Daniel P. Berrangé posted 1 patch 6 months, 2 weeks ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/20240517135816.1145119-1-berrange@redhat.com
src/rpc/virnetclient.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
[PATCH] rpc: avoid leak of GSource in use for interrupting main loop
Posted by Daniel P. Berrangé 6 months, 2 weeks ago
We never release the reference on the GSource created for
interrupting the main loop, nor do we remove it from the
main context if our thread is woken up prior to the wakeup
callback firing.

This can result in a leak of GSource objects, along with an
ever growing list of GSources attached to the main context,
which will gradually slow down execution of the loop, as
several operations are O(N) for the number of attached GSource
objects.

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
 src/rpc/virnetclient.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/src/rpc/virnetclient.c b/src/rpc/virnetclient.c
index 147b0d661a..6d424eb599 100644
--- a/src/rpc/virnetclient.c
+++ b/src/rpc/virnetclient.c
@@ -1946,7 +1946,7 @@ static int virNetClientIO(virNetClient *client,
     /* Check to see if another thread is dispatching */
     if (client->haveTheBuck) {
         /* Force other thread to wakeup from poll */
-        GSource *wakeup = g_idle_source_new();
+        g_autoptr(GSource) wakeup = g_idle_source_new();
         g_source_set_callback(wakeup, virNetClientIOWakeup, client->eventLoop, NULL);
         g_source_attach(wakeup, client->eventCtx);
 
@@ -1968,6 +1968,7 @@ static int virNetClientIO(virNetClient *client,
             return -1;
         }
 
+        g_source_destroy(wakeup);
         VIR_DEBUG("Woken up from sleep head=%p call=%p",
                   client->waitDispatch, thiscall);
         /* Three reasons we can be woken up
-- 
2.43.0
Re: [PATCH] rpc: avoid leak of GSource in use for interrupting main loop
Posted by Michal Prívozník 6 months, 2 weeks ago
On 5/17/24 15:58, Daniel P. Berrangé wrote:
> We never release the reference on the GSource created for
> interrupting the main loop, nor do we remove it from the
> main context if our thread is woken up prior to the wakeup
> callback firing.
> 
> This can result in a leak of GSource objects, along with an
> ever growing list of GSources attached to the main context,
> which will gradually slow down execution of the loop, as
> several operations are O(N) for the number of attached GSource
> objects.
> 
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> ---
>  src/rpc/virnetclient.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
>

Reviewed-by: Michal Privoznik <mprivozn@redhat.com>

Michal