Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hOzrO-0002GO-31 for pgsql-hackers@arkaria.postgresql.org; Fri, 10 May 2019 07:22:26 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hOzrJ-00066k-P6 for pgsql-hackers@arkaria.postgresql.org; Fri, 10 May 2019 07:22:21 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hOzrJ-00066d-5n for pgsql-hackers@lists.postgresql.org; Fri, 10 May 2019 07:22:21 +0000 Received: from mail-pg1-x541.google.com ([2607:f8b0:4864:20::541]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hOzrG-0000t1-BA for pgsql-hackers@lists.postgresql.org; Fri, 10 May 2019 07:22:19 +0000 Received: by mail-pg1-x541.google.com with SMTP id a3so2570061pgb.3 for ; Fri, 10 May 2019 00:22:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=leadboat.com; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=ndVUmB4kJaT3UUk+jAdOWq6rJ9PKHT4GZXjKaC75rpA=; b=J2iRlYoGeycfSsnAhEaYPcw8Q39NMHwB7AaCPSvphlrtAprAX3qwXmklhA0KKxBeLd PIbnvO7bmUhq0plZ1L29q8ScgQAQCoge0hohQ+YQJKPoXlyLMCb7s/3EF90uZavpDzDe Mo+38v3ucPPX7A4DzEatuRwmoadN52/pruIWI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=ndVUmB4kJaT3UUk+jAdOWq6rJ9PKHT4GZXjKaC75rpA=; b=fakzIl7f+ZNE+IkSDCr7wcnoZv9YctgNPwdCBeVKTyQgtxZYY1A+E9MpTG8RBL1CEa lfgxuAPWb9PTNr9GliNWsZVE5Qet3bLql98M43+3qhd7+al15A5dQHXbhp4UUh8dPTyg bK8vweupfKRbFDlbv1JnHLn8OSYJNfhsO5mO9XHAAdDY9kZF1L6N2a3WXAPNe59K9Rwp kt+BdllW7Gc0FtkxpC+8KbbhWdOaSrUDsKnwHyGZHwR/QROv01TlxS1Yhlz+/gHEoy8J Bv2JMkAv8L0yME0YZKFjuYYoJKWHiJMb+ERxpzwRc4CnG69U348YYpBz5+ilhy/L59aS DFiw== X-Gm-Message-State: APjAAAVcfb1iH6msCeS6LOi4M4nBMsLxc4QVhXA/WQJfeVB+mUMiO0wd 7CSwm4gd7x/EUDxK6yIiukW2yE0spyU= X-Google-Smtp-Source: APXvYqxnlgNqoCUfuwFftUt370KlEEOApaAKafWDLcM1ubXom4RlrokICe6sHGRipf/LKqfrfbmmVg== X-Received: by 2002:a62:7995:: with SMTP id u143mr11805449pfc.61.1557472936659; Fri, 10 May 2019 00:22:16 -0700 (PDT) Received: from rfd.leadboat.com (108-233-125-46.lightspeed.sntcca.sbcglobal.net. [108.233.125.46]) by smtp.gmail.com with ESMTPSA id n11sm4316030pgq.8.2019.05.10.00.22.15 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 10 May 2019 00:22:15 -0700 (PDT) Date: Fri, 10 May 2019 00:22:13 -0700 From: Noah Misch To: Tom Lane Cc: pgsql-hackers@lists.postgresql.org Subject: Re: Unexpected "shared memory block is still in use" Message-ID: <20190510072213.GA1098093@rfd.leadboat.com> References: <22224.1557340366@sss.pgh.pa.us> <20190509055414.GB1066859@rfd.leadboat.com> <2663.1557409654@sss.pgh.pa.us> <2226.1557439179@sss.pgh.pa.us> <17642.1557442078@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <17642.1557442078@sss.pgh.pa.us> User-Agent: Mutt/1.5.24 (2015-08-30) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Thu, May 09, 2019 at 06:47:58PM -0400, Tom Lane wrote: > I wrote: > > However, I have a new theory, after noticing that c09850992 moved the > > check for shm_nattch == 0. Previously, if a shmem segment had zero attach > > count, it was unconditionally considered not-a-threat. Now, we'll try > > shmat() anyway, and if that fails for any reason other than EACCES, we say > > SHMSTATE_ANALYSIS_FAILURE which leads to the described error report. > > So I suspect that what we hit was a race condition whereby some other > > parallel test was using the same shmem ID and we managed to see its > > segment successfully in shmctl but then it was gone by the time we did > > shmat. This leads me to think that EINVAL and EIDRM failures from > > shmat had better be considered SHMSTATE_ENOENT not > > SHMSTATE_ANALYSIS_FAILURE. > > In principle this is a longstanding race condition, but I wonder > > whether we made it more probable by moving the shm_nattch check. > > Hah --- this is a real race condition, and I can demonstrate it very > easily by inserting a sleep right there, as in the attached > for-testing-only patch. > > The particular parallelism level I use is > > make -s check-world -j4 PROVE_FLAGS='-j4 --quiet --nocolor --nocount' > > on a dual-socket 4-cores-per-socket Xeon machine. With that command and > this patch, I frequently get multiple failures per run, and they all > report either EINVAL or EIDRM. > > The patch generally reports that nattch had been 1, so my thought that > that change might've made it worse seems unfounded. But we have > absolutely got a hittable race condition here. The real fix should > be on the order of > > if (errno == EACCES) > return SHMSTATE_FOREIGN; > + else if (errno == EINVAL || errno == EIDRM) > + return SHMSTATE_ENOENT; > else > return SHMSTATE_ANALYSIS_FAILURE; > > (plus comments of course). Looks good. That is basically a defect in commit c09850992; the race passed from irrelevance to relevance when that commit subjected more segments to the test. Thanks for diagnosing it.