Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rY3yW-00EAKm-5u for pgsql-hackers@arkaria.postgresql.org; Thu, 08 Feb 2024 12:57:57 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1rY3yU-00HY3W-HW for pgsql-hackers@arkaria.postgresql.org; Thu, 08 Feb 2024 12:57:54 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rY3yU-00HY3M-3s for pgsql-hackers@lists.postgresql.org; Thu, 08 Feb 2024 12:57:54 +0000 Received: from lahtoruutu.iki.fi ([2a0b:5c81:1c1::37]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rY3yR-0067tG-A1 for pgsql-hackers@postgresql.org; Thu, 08 Feb 2024 12:57:53 +0000 Received: from [192.168.1.115] (dsl-hkibng22-54f8db-125.dhcp.inet.fi [84.248.219.125]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: hlinnaka) by lahtoruutu.iki.fi (Postfix) with ESMTPSA id 4TVxq03fVsz49Pv3; Thu, 8 Feb 2024 14:57:48 +0200 (EET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=lahtoruutu; t=1707397069; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=jQ4b4Tx9aaWCn4e8xqT7wLjSlPp5nGKRr2YdpSO3dJI=; b=jRDC35OmWuYl750BguGi8xf4Ez05t0Eq8klXbr/ipH9w3wyDTMNnjBwzLi/doFjmHGHKKH 9plu5gBUBY1tN4FaYxSmQGiSUf5h5Qk8R1Cq8/Y8GUs7Fh6qDKUGqXTbZYwWbf0N9N6HqP 1NXNTBh/Vx+xgk6AcpSyGCvQyQugpdbnaoCvx25n0a8ZDoUSjB/j4RcHXn1zC7fRrWcxex aBw6YZImmqb+UzsI9XKWsJ2F8TbPODRhYOS30WvMTuRO5AOhyzqhZjsDi4k5Pp2FUALQb1 U4Oy2wFnsael58L/h8c98uAiAmtE0aWg57guh9Vs/iZo/lZn2H6m1ZF6CvehSQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=lahtoruutu; t=1707397069; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=jQ4b4Tx9aaWCn4e8xqT7wLjSlPp5nGKRr2YdpSO3dJI=; b=XLJxcEWT+XgrjoLbHeDE9k6ZzGanzgPr/EfbxyEF9yuPCZsoWtBaQyTSLy7PbWWCHsdMKN VwlRPkHHBjN9PcdomKt7LF2e4wfpp0CYNsKbN5cAA5O5fXGLeLjhaMwfu6e3CDy5DYT9/R fcYlZ2qbCPwQw0xHXiX3GkJva22WQufdt4WL5wQ4weT+auE3E3t6IX4LvelQR+QJKYvpXm 7RvOkwAqJD3/gZLx3sJfax9+IYl091nAPwYUMw/QuPHl8WG4RK7BCHUIpnnFjzpuVGrgPT V5yBu7B7MjpXkAQsnAYUUAx2n2nnLE5auDh4zWIceFgJ44XNIXYrTkss3l1d4g== ARC-Seal: i=1; s=lahtoruutu; d=iki.fi; t=1707397069; a=rsa-sha256; cv=none; b=wgGffDpZx0TmgF/25cJiqSVzMzXT4n5Kkl8OXBttC5CgRSvbr2+7grLg3sGvQMDaz8W7Bi IndeF4nzzCipd3tagbduWp05P9OxqkIMBLCYx0zWDht7mrs+lKkPu4QgLDYW69AdWjNY4W YP3sOLveWtzyP5Np3NGPKue5g/lwztjfiFaQBzLlxu1EYR0NGqxn1zmjZFn51EIkeMiEt9 aHpg+8uLdmgbEi3pEJvrGt9MV5GJcftznLfZUzgNL6Nj3ePI47oJE309olAApDa83TOvpr 2UC84UWv7v3PVQg09SQQdqJaeutHXnu7q7R9ugS9Sy5AQcvqHl846CYrF3TvQw== ARC-Authentication-Results: i=1; ORIGINATING; auth=pass smtp.auth=hlinnaka smtp.mailfrom=hlinnaka@iki.fi Message-ID: <30a17cd9-9ae1-4aaf-b08c-f8d1a8e4287d@iki.fi> Date: Thu, 8 Feb 2024 14:57:47 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: "ERROR: latch already owned" on gharial Content-Language: en-US To: Soumyadeep Chakraborty , Alvaro Herrera Cc: Sandeep Thakkar , Andrew Dunstan , pgsql-hackers , buildfarm-admins@lists.postgresql.org, CM Team References: <20220713082852.iyyxfilvrkiqdz2e@alvherre.pgsql> From: Heikki Linnakangas In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 08/02/2024 04:08, Soumyadeep Chakraborty wrote: > A possible ordering of events: > > (1) DisownLatch() is called by pid Y during ProcKill() and the write for > latch->owner_pid = 0 is NOT yet flushed to shmem. > > (2) The PGPROC object for pid Y is returned to the free list. > > (3) Pid X sees the same PGPROC object on the free list and grabs it. > > (4) Pid X does sanity check inside OwnLatch during InitProcess and > still sees the > old value of latch->owner_pid = Y (and not = 0), and trips the ERROR. > > The above sequence of operations should apply to PG HEAD as well. > > Suggestion: > > Should we do a pg_memory_barrier() at the end of DisownLatch(), like in > ResetLatch(), like the one introduced in [3]? This would ensure that the write > latch->owner_pid = 0; is flushed to shmem. The attached patch does this. Hmm, there is a pair of SpinLockAcquire() and SpinLockRelease() in ProcKill(), before step 3 can happen. Comment in spin.h about SpinLockAcquire/Release: > * Load and store operations in calling code are guaranteed not to be > * reordered with respect to these operations, because they include a > * compiler barrier. (Before PostgreSQL 9.5, callers needed to use a > * volatile qualifier to access data protected by spinlocks.) That talks about a compiler barrier, though, not a memory barrier. But looking at the implementations in s_lock.h, I believe they do act as memory barrier, too. So you might indeed have that problem on 9.4, but AFAICS not on later versions. -- Heikki Linnakangas Neon (https://neon.tech)