{"id":19431,"url":"https://patchwork.libcamera.org/api/patches/19431/?format=json","web_url":"https://patchwork.libcamera.org/patch/19431/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20240121035948.4226-11-laurent.pinchart@ideasonboard.com>","date":"2024-01-21T03:59:46","name":"[libcamera-devel,10/12] test: timer-thread: Destroy Object from correct thread context","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"6419c73c8386624ff9b65883a50019ced84661d6","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/19431/mbox/","series":[{"id":4149,"url":"https://patchwork.libcamera.org/api/series/4149/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=4149","date":"2024-01-21T03:59:36","name":"libcamera: Hardening against thread race conditions","version":1,"mbox":"https://patchwork.libcamera.org/series/4149/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/19431/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/19431/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id EA8C2C32BF\n\tfor <parsemail@patchwork.libcamera.org>;\n\tSun, 21 Jan 2024 04:00:06 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 19E3C6294B;\n\tSun, 21 Jan 2024 05:00:06 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 2AB9B62957\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSun, 21 Jan 2024 04:59:59 +0100 (CET)","from pendragon.ideasonboard.com (89-27-53-110.bb.dnainternet.fi\n\t[89.27.53.110])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 24D938D4;\n\tSun, 21 Jan 2024 04:58:47 +0100 (CET)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1705809606;\n\tbh=+e/hHPQCKT9nxgeE3fXUKLUmAWXegOJC3OWkWcvvDI0=;\n\th=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:\n\tFrom;\n\tb=JyNy39FTP6z8hcmw3HhbSKYbPD6oC6gEBnRMdW8Lxhh2vueZjFCszr+gjpbD+OiY0\n\toMqCP2Ym3xy0JAWUzBZW1YnHGE4e1K51z+JXubBnOGMcJ4psJPukshN8uvt+rg8VF9\n\tk0Ykgdlzobxggb3W55p+d1qGKng7Dy8JNbeO67PYh/m7/PHU9kEuQ7oHx8CePTRPR4\n\tc84HfVx8HdkdKIS1J9DPdhTrcVZSKh0vtrUmbWFDeacRyp6hki0/T9e1Su3DdWh1b7\n\t+cfbwIJmZhMmUZ2ELZHyvgD52G1TqNhxz7+TswZGaKvxMmwmMsqvkD52E0X/Znhcgo\n\t2Pchs2XBKyQsQ==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1705809527;\n\tbh=+e/hHPQCKT9nxgeE3fXUKLUmAWXegOJC3OWkWcvvDI0=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=MLma6vBWxs0qR9co/qZ2GgswuB8OMAfk9ZdD2gRDX6cCf6lW160FhY0Y3dRt+VCGd\n\t0ndfMPzD0AuGLzaItTz0MxUrjl4YbmEjUqf7Cmse9clE5Snqm6px82ltTQVu74Xcb4\n\twojltPonRUG38ZXM0oFhrj3uUIbVNJfiHytZCdWs="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"MLma6vBW\"; dkim-atps=neutral","To":"libcamera-devel@lists.libcamera.org","Date":"Sun, 21 Jan 2024 05:59:46 +0200","Message-ID":"<20240121035948.4226-11-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.43.0","In-Reply-To":"<20240121035948.4226-1-laurent.pinchart@ideasonboard.com>","References":"<20240121035948.4226-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH 10/12] test: timer-thread: Destroy Object\n\tfrom correct thread context","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","From":"Laurent Pinchart via libcamera-devel\n\t<libcamera-devel@lists.libcamera.org>","Reply-To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"The TimeoutHandler used in the test is destroyed from the main thread,\nwhich is invalid for a thread-bound object bound to a different thread.\nFix it by destroying it with deleteLater().\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n test/timer-fail.cpp   | 16 +++++++++++-----\n test/timer-thread.cpp | 14 ++++++++++----\n 2 files changed, 21 insertions(+), 9 deletions(-)","diff":"diff --git a/test/timer-fail.cpp b/test/timer-fail.cpp\nindex e9a3b1b61bcb..b89c85f2ee72 100644\n--- a/test/timer-fail.cpp\n+++ b/test/timer-fail.cpp\n@@ -54,7 +54,9 @@ protected:\n \tint init()\n \t{\n \t\tthread_.start();\n-\t\ttimeout_.moveToThread(&thread_);\n+\n+\t\ttimeout_ = new TimeoutHandler();\n+\t\ttimeout_->moveToThread(&thread_);\n \n \t\treturn TestPass;\n \t}\n@@ -67,7 +69,7 @@ protected:\n \t\t * event dispatcher to make sure we don't succeed simply because\n \t\t * the event dispatcher hasn't noticed the timer restart.\n \t\t */\n-\t\ttimeout_.start();\n+\t\ttimeout_->start();\n \t\tthread_.eventDispatcher()->interrupt();\n \n \t\tthis_thread::sleep_for(chrono::milliseconds(200));\n@@ -77,7 +79,7 @@ protected:\n \t\t * TestPass in the unexpected (usually known as \"fail\"), case,\n \t\t * and TestFail otherwise.\n \t\t */\n-\t\tif (timeout_.timeout()) {\n+\t\tif (timeout_->timeout()) {\n \t\t\tcout << \"Timer start from wrong thread succeeded unexpectedly\"\n \t\t\t     << endl;\n \t\t\treturn TestPass;\n@@ -88,13 +90,17 @@ protected:\n \n \tvoid cleanup()\n \t{\n-\t\t/* Must stop thread before destroying timeout. */\n+\t\t/*\n+\t\t * Object class instances must be destroyed from the thread\n+\t\t * they live in.\n+\t\t */\n+\t\ttimeout_->deleteLater();\n \t\tthread_.exit(0);\n \t\tthread_.wait();\n \t}\n \n private:\n-\tTimeoutHandler timeout_;\n+\tTimeoutHandler *timeout_;\n \tThread thread_;\n };\n \ndiff --git a/test/timer-thread.cpp b/test/timer-thread.cpp\nindex 4caf4e33b2ba..8675e2480aa9 100644\n--- a/test/timer-thread.cpp\n+++ b/test/timer-thread.cpp\n@@ -50,7 +50,9 @@ protected:\n \tint init()\n \t{\n \t\tthread_.start();\n-\t\ttimeout_.moveToThread(&thread_);\n+\n+\t\ttimeout_ = new TimeoutHandler();\n+\t\ttimeout_->moveToThread(&thread_);\n \n \t\treturn TestPass;\n \t}\n@@ -63,7 +65,7 @@ protected:\n \t\t */\n \t\tthis_thread::sleep_for(chrono::milliseconds(200));\n \n-\t\tif (!timeout_.timeout()) {\n+\t\tif (!timeout_->timeout()) {\n \t\t\tcout << \"Timer expiration test failed\" << endl;\n \t\t\treturn TestFail;\n \t\t}\n@@ -73,13 +75,17 @@ protected:\n \n \tvoid cleanup()\n \t{\n-\t\t/* Must stop thread before destroying timeout. */\n+\t\t/*\n+\t\t * Object class instances must be destroyed from the thread\n+\t\t * they live in.\n+\t\t */\n+\t\ttimeout_->deleteLater();\n \t\tthread_.exit(0);\n \t\tthread_.wait();\n \t}\n \n private:\n-\tTimeoutHandler timeout_;\n+\tTimeoutHandler *timeout_;\n \tThread thread_;\n };\n \n","prefixes":["libcamera-devel","10/12"]}