[{"id":3156,"web_url":"https://patchwork.libcamera.org/comment/3156/","msgid":"<20191127150845.GH12879@bigcity.dyn.berto.se>","date":"2019-11-27T15:08:45","subject":"Re: [libcamera-devel] [PATCH 3/5] libcamera: thread: Fix race\n\tcondition when dispatching messages","submitter":{"id":5,"url":"https://patchwork.libcamera.org/api/people/5/","name":"Niklas Söderlund","email":"niklas.soderlund@ragnatech.se"},"content":"Hi Laurent,\n\nThanks for your patch.\n\nOn 2019-11-27 10:49:07 +0200, Laurent Pinchart wrote:\n> The Object class stores the number of pending messages that have been\n> posted for it, while the actual messages are stored in a per-thread list\n> in the ThreadData class. When dispatching messages, the message is\n> removed from the list, passed to the receiver, and the number of pending\n> messages is updated.\n> \n> In order to avoid too much contention on the list lock, the lock is\n> dropped to pass the message to the receiver and then taken again. This\n> creates a race condition window with Thread::removeMessages(), as the\n> number of pending messages for a receiver is briefly out of sync with\n> the pending messages list. The assertion at the end of removeMessages()\n> thus sometimes fails.\n> \n> Fix this by decrementing the pending messages counter before releasing\n> the lock in Thread::dispatchMessages(). This fixes the slow message\n> receiver test in MessageTest.\n> \n> Fixes: 01b930964acd (\"libcamera: thread: Add a messaging passing API\")\n> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\nReviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n\n> ---\n>  src/libcamera/thread.cpp | 4 ++--\n>  1 file changed, 2 insertions(+), 2 deletions(-)\n> \n> diff --git a/src/libcamera/thread.cpp b/src/libcamera/thread.cpp\n> index 029a0e8fddd5..18ebd16a7e2f 100644\n> --- a/src/libcamera/thread.cpp\n> +++ b/src/libcamera/thread.cpp\n> @@ -439,11 +439,11 @@ void Thread::dispatchMessages()\n>  \t\tObject *receiver = msg->receiver_;\n>  \t\tASSERT(data_ == receiver->thread()->data_);\n>  \n> +\t\treceiver->pendingMessages_--;\n> +\n>  \t\tlocker.unlock();\n>  \t\treceiver->message(msg.get());\n>  \t\tlocker.lock();\n> -\n> -\t\treceiver->pendingMessages_--;\n>  \t}\n>  }\n>  \n> -- \n> Regards,\n> \n> Laurent Pinchart\n> \n> _______________________________________________\n> libcamera-devel mailing list\n> libcamera-devel@lists.libcamera.org\n> https://lists.libcamera.org/listinfo/libcamera-devel","headers":{"Return-Path":"<niklas.soderlund@ragnatech.se>","Received":["from mail-lf1-x144.google.com (mail-lf1-x144.google.com\n\t[IPv6:2a00:1450:4864:20::144])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 431F960C33\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 27 Nov 2019 16:08:47 +0100 (CET)","by mail-lf1-x144.google.com with SMTP id l14so17429735lfh.10\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 27 Nov 2019 07:08:47 -0800 (PST)","from localhost (h-93-159.A463.priv.bahnhof.se. [46.59.93.159])\n\tby smtp.gmail.com with ESMTPSA id\n\ty192sm2305839lfa.63.2019.11.27.07.08.45\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tWed, 27 Nov 2019 07:08:45 -0800 (PST)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=ragnatech-se.20150623.gappssmtp.com; s=20150623;\n\th=date:from:to:cc:subject:message-id:references:mime-version\n\t:content-disposition:content-transfer-encoding:in-reply-to\n\t:user-agent; bh=vkO57mmwmFVpqGUG6BsWKmR//Xp8oJwlvAFoAAEhht8=;\n\tb=miC13kda6a2tM7LRIdxzvcUIErV5KSNuvS+mXrabaxSCaLA/Rx8zDZLm/1nV8/gzRU\n\tQ7UAhofUmq2EKXf6bkx44zBbu3tW/1wZ4zfSt+6wZxxn1Qqh1GaEKQ/+QfOZnDce80kZ\n\tpa0VP8IOQr+MA6uOsadge/Ubo4jple+kH9t1x5xxPB/bKtL4bnkDeoHSesyH7dvHZUpD\n\tnClNDYDfSmwTNKLEEqws5JFAmi0mIPQkpV225cFSy0QmqHpungZ++ajFwiEVTvgwpKht\n\tSLqYKrRHI1z6nXsA1Q6ObopZ2uHqGTlHhQEiv0BMAxjm861ku0JoBgUpfIzvKY1LkrOP\n\tb56A==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20161025;\n\th=x-gm-message-state:date:from:to:cc:subject:message-id:references\n\t:mime-version:content-disposition:content-transfer-encoding\n\t:in-reply-to:user-agent;\n\tbh=vkO57mmwmFVpqGUG6BsWKmR//Xp8oJwlvAFoAAEhht8=;\n\tb=f094HinRr+DvK3BEYbYqMhYROOK6iu0GhgKW3ZkpFKsylU/40rEpDBTWnIAd2dZSK9\n\twntlZBm3165pvm3UAm+ItkqWJJdAzeiSdx02uI+JfLQFReQD5qD79upjfuKvECtZpfWp\n\tYOLiKEe7ofwxT2Iw4ALZBweRqCnyfioVOYDYDnfOhwsX2n7AOGJDBNK3rvriBbSWPib2\n\t3NinASdz8j1IjR2DEee6RG0mbplg59toGuwA3gUN68bdovPP3G8Ntm+qIDuNfUyj6Jil\n\tvdVIUftWmx37ge0dHFm6X2Jz7FeWZsHCF4aL3EfYhyMWAJ3m3HTymbdnAtf1ph14ja86\n\tAxYg==","X-Gm-Message-State":"APjAAAXarkV7b+iSytrIncOBaQo4HDSi3+DrVn/q4tXRI7qW/NQNpuPp\n\tnpbwe+h8i0dC2hdpvPTGdDM+SKS5/H7gWg==","X-Google-Smtp-Source":"APXvYqysUtaQ9wlcH/Jt6WdIE//L3rlzuWX3fa7yLvLlmSFdrpB+gLbYqtzweeKvbuRofa6kg/f1kw==","X-Received":"by 2002:ac2:5505:: with SMTP id\n\tj5mr15354495lfk.128.1574867326653; \n\tWed, 27 Nov 2019 07:08:46 -0800 (PST)","Date":"Wed, 27 Nov 2019 16:08:45 +0100","From":"Niklas =?iso-8859-1?q?S=F6derlund?= <niklas.soderlund@ragnatech.se>","To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org","Message-ID":"<20191127150845.GH12879@bigcity.dyn.berto.se>","References":"<20191127084909.10612-1-laurent.pinchart@ideasonboard.com>\n\t<20191127084909.10612-3-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=iso-8859-1","Content-Disposition":"inline","Content-Transfer-Encoding":"8bit","In-Reply-To":"<20191127084909.10612-3-laurent.pinchart@ideasonboard.com>","User-Agent":"Mutt/1.12.1 (2019-06-15)","Subject":"Re: [libcamera-devel] [PATCH 3/5] libcamera: thread: Fix race\n\tcondition when dispatching messages","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>","X-List-Received-Date":"Wed, 27 Nov 2019 15:08:47 -0000"}}]