{"id":2230549,"url":"http://patchwork.ozlabs.org/api/1.1/patches/2230549/?format=json","web_url":"http://patchwork.ozlabs.org/project/qemu-devel/patch/20260430-qemu-vnc-v3-14-be96757428d0@redhat.com/","project":{"id":14,"url":"http://patchwork.ozlabs.org/api/1.1/projects/14/?format=json","name":"QEMU Development","link_name":"qemu-devel","list_id":"qemu-devel.nongnu.org","list_email":"qemu-devel@nongnu.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20260430-qemu-vnc-v3-14-be96757428d0@redhat.com>","date":"2026-04-29T21:02:47","name":"[v3,14/26] ui/vnc: make the worker thread per-VncDisplay","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"e2cea385b0d85ba6eb2fcc885d0e094726275533","submitter":{"id":66774,"url":"http://patchwork.ozlabs.org/api/1.1/people/66774/?format=json","name":"Marc-André Lureau","email":"marcandre.lureau@redhat.com"},"delegate":null,"mbox":"http://patchwork.ozlabs.org/project/qemu-devel/patch/20260430-qemu-vnc-v3-14-be96757428d0@redhat.com/mbox/","series":[{"id":502150,"url":"http://patchwork.ozlabs.org/api/1.1/series/502150/?format=json","web_url":"http://patchwork.ozlabs.org/project/qemu-devel/list/?series=502150","date":"2026-04-29T21:02:34","name":"ui: add standalone VNC server over D-Bus","version":3,"mbox":"http://patchwork.ozlabs.org/series/502150/mbox/"}],"comments":"http://patchwork.ozlabs.org/api/patches/2230549/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/2230549/checks/","tags":{},"headers":{"Return-Path":"<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@legolas.ozlabs.org","Authentication-Results":["legolas.ozlabs.org;\n\tdkim=pass (1024-bit key;\n unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256\n header.s=mimecast20190719 header.b=JlTKN5Fn;\n\tdkim-atps=neutral","legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org\n (client-ip=209.51.188.17; helo=lists1p.gnu.org;\n envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org;\n receiver=patchwork.ozlabs.org)"],"Received":["from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17])\n\t(using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits))\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4g5VGK2kPBz1yHv\n\tfor <incoming@patchwork.ozlabs.org>; Thu, 30 Apr 2026 07:06:17 +1000 (AEST)","from localhost ([::1] helo=lists1p.gnu.org)\n\tby lists1p.gnu.org with esmtp (Exim 4.90_1)\n\t(envelope-from <qemu-devel-bounces@nongnu.org>)\n\tid 1wIC6S-00070N-IG; Wed, 29 Apr 2026 17:05:53 -0400","from eggs.gnu.org ([2001:470:142:3::10])\n by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)\n (Exim 4.90_1) (envelope-from <marcandre.lureau@redhat.com>)\n id 1wIC5K-0006Il-4j\n for qemu-devel@nongnu.org; Wed, 29 Apr 2026 17:04:43 -0400","from us-smtp-delivery-124.mimecast.com ([170.10.129.124])\n by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)\n (Exim 4.90_1) (envelope-from <marcandre.lureau@redhat.com>)\n id 1wIC5H-0003TX-U2\n for qemu-devel@nongnu.org; Wed, 29 Apr 2026 17:04:41 -0400","from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com\n (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by\n relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3,\n cipher=TLS_AES_256_GCM_SHA384) id us-mta-183-blbw-yUNOneR9_FZY0jX-w-1; Wed,\n 29 Apr 2026 17:04:37 -0400","from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com\n (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4])\n (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest\n SHA256)\n (No client certificate requested)\n by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS\n id 8C9921956095\n for <qemu-devel@nongnu.org>; Wed, 29 Apr 2026 21:04:36 +0000 (UTC)","from localhost (unknown [10.44.22.2])\n by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP\n id 41438300019F; Wed, 29 Apr 2026 21:04:34 +0000 (UTC)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;\n s=mimecast20190719; t=1777496679;\n h=from:from:reply-to:subject:subject:date:date:message-id:message-id:\n to:to:cc:cc:mime-version:mime-version:content-type:content-type:\n content-transfer-encoding:content-transfer-encoding:\n in-reply-to:in-reply-to:references:references;\n bh=LQG0qN6rFQpchqEjkKkpkK6wYq5eQ1JvZPMXVcU8sd0=;\n b=JlTKN5FnD31WpRQ7kxWu9h7d6WDLZSKkqdKA7cPa3rTzPh1/WHntycHSxkYDr7hR1xcTxx\n wkOWOBoO88Hxbih/nMaJpmdKNtBpIfJlunrQLiK9yigr6rcNHfKGkeX5ImotgewiW8Ys9J\n vZqD8WKKE2OWPziPt9lgGbIHLxrL+iU=","X-MC-Unique":"blbw-yUNOneR9_FZY0jX-w-1","X-Mimecast-MFC-AGG-ID":"blbw-yUNOneR9_FZY0jX-w_1777496676","From":"=?utf-8?q?Marc-Andr=C3=A9_Lureau?= <marcandre.lureau@redhat.com>","Date":"Thu, 30 Apr 2026 01:02:47 +0400","Subject":"[PATCH v3 14/26] ui/vnc: make the worker thread per-VncDisplay","MIME-Version":"1.0","Content-Type":"text/plain; charset=\"utf-8\"","Content-Transfer-Encoding":"8bit","Message-Id":"<20260430-qemu-vnc-v3-14-be96757428d0@redhat.com>","References":"<20260430-qemu-vnc-v3-0-be96757428d0@redhat.com>","In-Reply-To":"<20260430-qemu-vnc-v3-0-be96757428d0@redhat.com>","To":"qemu-devel@nongnu.org","Cc":"=?utf-8?q?Marc-Andr=C3=A9_Lureau?= <marcandre.lureau@redhat.com>","X-Developer-Signature":"v=1; a=openpgp-sha256; l=6513;\n i=marcandre.lureau@redhat.com; h=from:subject:message-id;\n bh=XCDp9S7HaL4bLvRrQfhchRC/cgPquwTPMALLwTkY/1Y=;\n b=owEBbQKS/ZANAwAKAdro4Ql1lpzlAcsmYgBp8nIMP0Sg7vWQoEo0cWOvJtxIFY1cyJY8gVql1\n BItB7/gZb2JAjMEAAEKAB0WIQSHqb2TP4fGBtJ29i3a6OEJdZac5QUCafJyDAAKCRDa6OEJdZac\n 5WkrD/4pyTQhgKTK/q5u2m/HYnH7/mbTqz5Dih3S4FfLbOKhRuwK7yrVPULAJpYjX8gSYoml+72\n G9FsQN8hX9Ctw/brPbTmVZsfxOOSyL5xMxzyEMvmNwOo0pIS5OYnLpe88mzEDu8O4eqDzY1AVkJ\n CyMianmfUFhmp2vPJodB2TqrLaTc7M5C1TxlYR8X7fv2G+QCFRHcBMYLYcG/bp067NefkvrzdZI\n KrAHGzn+wsf2hRxcmAMZs1wvt4/yGyj0FHv4mK3kqT/IqArZ4uVEuEhXd+Z5MTF+JKEMRdKXiNl\n 0HAxImgN6F8XDwrYV6KFI4LPhzhu1oWgeOdiVVPBOHK1NwBgtXH3JYcw9QnLh0Uks1TLm6r7D/H\n rF53OUwyVoFy0MZtRnN+Rvkw1LBtsAfUFeuicu9PmapHEoO/j7gdIbrW67sdTKjuy1Qw073JziC\n wM5BmyYdDi5ixxKhOPz92j+0SIiGf4cXtvrxQ3dyP+WcAvorNGM5Yr5t2RyoIuPtk696swqHxvO\n lr/ndM13sygc6nPuGSqzDO8wdwJRAvx9sztvoWBgl32i/uWPzYRDEKLVXCZ+khUMwBnmxy/1l2e\n VklNKY3ou6Rpf8GrbBdFw3WkyZ1L1JJCDWwvRY5SjlZEgzD2lAVXzVkSooCfllvWgijvwNIgPb5\n Ju8J/h07pXRIAtg==","X-Developer-Key":"i=marcandre.lureau@redhat.com; a=openpgp;\n fpr=87A9BD933F87C606D276F62DDAE8E10975969CE5","X-Scanned-By":"MIMEDefang 3.4.1 on 10.30.177.4","Received-SPF":"pass client-ip=170.10.129.124;\n envelope-from=marcandre.lureau@redhat.com;\n helo=us-smtp-delivery-124.mimecast.com","X-Spam_score_int":"12","X-Spam_score":"1.2","X-Spam_bar":"+","X-Spam_report":"(1.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001,\n DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1,\n RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001,\n RCVD_IN_SBL_CSS=3.335, SPF_HELO_PASS=-0.001,\n SPF_PASS=-0.001 autolearn=no autolearn_force=no","X-Spam_action":"no action","X-BeenThere":"qemu-devel@nongnu.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"qemu development <qemu-devel.nongnu.org>","List-Unsubscribe":"<https://lists.nongnu.org/mailman/options/qemu-devel>,\n <mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>","List-Archive":"<https://lists.nongnu.org/archive/html/qemu-devel>","List-Post":"<mailto:qemu-devel@nongnu.org>","List-Help":"<mailto:qemu-devel-request@nongnu.org?subject=help>","List-Subscribe":"<https://lists.nongnu.org/mailman/listinfo/qemu-devel>,\n <mailto:qemu-devel-request@nongnu.org?subject=subscribe>","Errors-To":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org","Sender":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org"},"content":"The VNC encoding worker thread was using a single global queue shared\nacross all VNC displays, with no way to stop it. This made it impossible\nto properly clean up resources when a VncDisplay is freed.\n\nMove the VncJobQueue from a file-scoped global to a per-VncDisplay\nmember, so each display owns its worker thread and queue. Add\nvnc_stop_worker_thread() to perform an orderly shutdown: signal the\nthread to exit, join it, and destroy the queue. The thread is now\ncreated as QEMU_THREAD_JOINABLE instead of QEMU_THREAD_DETACHED.\n\nSigned-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>\n---\n ui/vnc-jobs.h |  3 ++-\n ui/vnc.h      |  2 ++\n ui/vnc-jobs.c | 62 ++++++++++++++++++++++++++++++++++++++---------------------\n ui/vnc.c      |  3 ++-\n 4 files changed, 46 insertions(+), 24 deletions(-)","diff":"diff --git a/ui/vnc-jobs.h b/ui/vnc-jobs.h\nindex 59f66bcc353..e5ab55c1da6 100644\n--- a/ui/vnc-jobs.h\n+++ b/ui/vnc-jobs.h\n@@ -37,7 +37,8 @@ void vnc_job_push(VncJob *job);\n void vnc_jobs_join(VncState *vs);\n \n void vnc_jobs_consume_buffer(VncState *vs);\n-void vnc_start_worker_thread(void);\n+void vnc_start_worker_thread(VncDisplay *vd);\n+void vnc_stop_worker_thread(VncDisplay *vd);\n \n /* Locks */\n static inline int vnc_trylock_display(VncDisplay *vd)\ndiff --git a/ui/vnc.h b/ui/vnc.h\nindex 472a55f7b5f..780fd39469f 100644\n--- a/ui/vnc.h\n+++ b/ui/vnc.h\n@@ -62,6 +62,7 @@\n \n typedef struct VncState VncState;\n typedef struct VncJob VncJob;\n+typedef struct VncJobQueue VncJobQueue;\n typedef struct VncRect VncRect;\n typedef struct VncRectEntry VncRectEntry;\n \n@@ -158,6 +159,7 @@ struct VncDisplay\n     int ledstate;\n     QKbdState *kbd;\n     QemuMutex mutex;\n+    VncJobQueue *queue;\n \n     int cursor_msize;\n     uint8_t *cursor_mask;\ndiff --git a/ui/vnc-jobs.c b/ui/vnc-jobs.c\nindex 5b17ef54091..90b68bf4cb9 100644\n--- a/ui/vnc-jobs.c\n+++ b/ui/vnc-jobs.c\n@@ -29,8 +29,6 @@\n #include \"qemu/osdep.h\"\n #include \"vnc.h\"\n #include \"vnc-jobs.h\"\n-#include \"qemu/sockets.h\"\n-#include \"qemu/main-loop.h\"\n #include \"trace.h\"\n \n /*\n@@ -56,17 +54,10 @@ struct VncJobQueue {\n     QemuCond cond;\n     QemuMutex mutex;\n     QemuThread thread;\n+    bool exit;\n     QTAILQ_HEAD(, VncJob) jobs;\n };\n \n-typedef struct VncJobQueue VncJobQueue;\n-\n-/*\n- * We use a single global queue, but most of the functions are\n- * already reentrant, so we can easily add more than one encoding thread\n- */\n-static VncJobQueue *queue;\n-\n static void vnc_lock_queue(VncJobQueue *queue)\n {\n     qemu_mutex_lock(&queue->mutex);\n@@ -125,12 +116,15 @@ static void vnc_job_free(VncJob *job)\n  */\n void vnc_job_push(VncJob *job)\n {\n+    VncJobQueue *queue = job->vs->vd->queue;\n+\n     assert(!QTAILQ_IN_USE(job, next));\n \n     if (QLIST_EMPTY(&job->rectangles)) {\n         vnc_job_free(job);\n     } else {\n         vnc_lock_queue(queue);\n+        assert(!queue->exit);\n         QTAILQ_INSERT_TAIL(&queue->jobs, job, next);\n         qemu_cond_broadcast(&queue->cond);\n         vnc_unlock_queue(queue);\n@@ -139,6 +133,7 @@ void vnc_job_push(VncJob *job)\n \n static bool vnc_has_job_locked(VncState *vs)\n {\n+    VncJobQueue *queue = vs->vd->queue;\n     VncJob *job;\n \n     QTAILQ_FOREACH(job, &queue->jobs, next) {\n@@ -151,6 +146,8 @@ static bool vnc_has_job_locked(VncState *vs)\n \n void vnc_jobs_join(VncState *vs)\n {\n+    VncJobQueue *queue = vs->vd->queue;\n+\n     vnc_lock_queue(queue);\n     while (vnc_has_job_locked(vs)) {\n         qemu_cond_wait(&queue->cond, &queue->mutex);\n@@ -252,9 +249,13 @@ static int vnc_worker_thread_loop(VncJobQueue *queue)\n     int saved_offset;\n \n     vnc_lock_queue(queue);\n-    while (QTAILQ_EMPTY(&queue->jobs)) {\n+    while (QTAILQ_EMPTY(&queue->jobs) && !queue->exit) {\n         qemu_cond_wait(&queue->cond, &queue->mutex);\n     }\n+    if (queue->exit) {\n+        vnc_unlock_queue(queue);\n+        return 1;\n+    }\n     job = QTAILQ_FIRST(&queue->jobs);\n     vnc_unlock_queue(queue);\n \n@@ -340,7 +341,7 @@ disconnected:\n     return 0;\n }\n \n-static VncJobQueue *vnc_queue_init(void)\n+static VncJobQueue *vnc_queue_new(void)\n {\n     VncJobQueue *queue = g_new0(VncJobQueue, 1);\n \n@@ -350,29 +351,46 @@ static VncJobQueue *vnc_queue_init(void)\n     return queue;\n }\n \n+static void vnc_queue_free(VncJobQueue *queue)\n+{\n+    qemu_cond_destroy(&queue->cond);\n+    qemu_mutex_destroy(&queue->mutex);\n+    g_free(queue);\n+}\n+\n static void *vnc_worker_thread(void *arg)\n {\n     VncJobQueue *queue = arg;\n \n     while (!vnc_worker_thread_loop(queue)) ;\n-    g_assert_not_reached();\n+\n     return NULL;\n }\n \n-static bool vnc_worker_thread_running(void)\n+void vnc_start_worker_thread(VncDisplay *vd)\n {\n-    return queue; /* Check global queue */\n+    assert(vd->queue == NULL);\n+\n+    vd->queue = vnc_queue_new();\n+    qemu_thread_create(&vd->queue->thread, \"vnc_worker\", vnc_worker_thread, vd->queue,\n+                       QEMU_THREAD_JOINABLE);\n }\n \n-void vnc_start_worker_thread(void)\n+void vnc_stop_worker_thread(VncDisplay *vd)\n {\n-    VncJobQueue *q;\n+    VncJobQueue *queue = vd->queue;\n \n-    if (vnc_worker_thread_running())\n+    if (!queue) {\n         return;\n+    }\n+\n+    /* all VNC clients must have finished before we can stop the worker thread */\n+    vnc_lock_queue(queue);\n+    assert(QTAILQ_EMPTY(&queue->jobs));\n+    queue->exit = true;\n+    qemu_cond_broadcast(&queue->cond);\n+    vnc_unlock_queue(queue);\n \n-    q = vnc_queue_init();\n-    qemu_thread_create(&q->thread, \"vnc_worker\", vnc_worker_thread, q,\n-                       QEMU_THREAD_DETACHED);\n-    queue = q; /* Set global queue */\n+    qemu_thread_join(&queue->thread);\n+    g_clear_pointer(&vd->queue, vnc_queue_free);\n }\ndiff --git a/ui/vnc.c b/ui/vnc.c\nindex c87d1f61a0a..3a908670ab9 100644\n--- a/ui/vnc.c\n+++ b/ui/vnc.c\n@@ -3457,7 +3457,7 @@ void vnc_display_init(const char *id, Error **errp)\n     vd->share_policy = VNC_SHARE_POLICY_ALLOW_EXCLUSIVE;\n     vd->connections_limit = 32;\n \n-    vnc_start_worker_thread();\n+    vnc_start_worker_thread(vd);\n \n     register_displaychangelistener(&vd->dcl);\n     vd->kbd = qkbd_state_init(vd->dcl.con);\n@@ -3513,6 +3513,7 @@ static void vnc_display_free(VncDisplay *vd)\n \n     assert(QTAILQ_EMPTY(&vd->clients));\n \n+    vnc_stop_worker_thread(vd);\n     vnc_display_close(vd);\n     unregister_displaychangelistener(&vd->dcl);\n     qkbd_state_free(vd->kbd);\n","prefixes":["v3","14/26"]}