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.92) (envelope-from ) id 1jHdlz-0006vV-8r for pgsql-hackers@arkaria.postgresql.org; Fri, 27 Mar 2020 01:26:59 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1jHdlx-0005Wd-Ln for pgsql-hackers@arkaria.postgresql.org; Fri, 27 Mar 2020 01:26:57 +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 1jHdlx-0005WW-9Y for pgsql-hackers@lists.postgresql.org; Fri, 27 Mar 2020 01:26:57 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jHdlq-0008Qy-H6 for pgsql-hackers@lists.postgresql.org; Fri, 27 Mar 2020 01:26:56 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id 02R1QnwY021520 for ; Thu, 26 Mar 2020 21:26:49 -0400 From: Tom Lane To: pgsql-hackers@lists.postgresql.org Subject: Race condition in SyncRepGetSyncStandbysPriority MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <21518.1585272409.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Thu, 26 Mar 2020 21:26:49 -0400 Message-ID: <21519.1585272409@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Twice in the past month [1][2], buildfarm member hoverfly has managed to reach the "unreachable" Assert(false) at the end of SyncRepGetSyncStandbysPriority. What seems likely to me, after quickly eyeballing the code, is that hoverfly is hitting the blatantly-obvious race condition in that function. Namely, that the second loop supposes that the state of the walsender array hasn't changed since the first loop. The minimum fix for this, I suppose, would have the first loop capture the sync_standby_priority value for each walsender along with what it's already capturing. But I wonder if the whole function shouldn't be rewritten from scratch, because it seems like the algorithm is both expensively brute-force and unintelligible, which is a sad combination. It's likely that the number of walsenders would never be high enough that efficiency could matter, but then couldn't we use an algorithm that is less complicated and more obviously correct? (Because the alternative conclusion, if you reject the theory that a race is happening, is that the algorithm is just flat out buggy; something that's not too easy to disprove either.) Another fairly dubious thing here is that whether or not *am_sync gets set depends not only on whether MyWalSnd is claiming to be synchronous but on how many lower-numbered walsenders are too. Is that really the right thing? But worse than any of that is that the return value seems to be a list of walsender array indexes, meaning that the callers cannot use it without making even stronger assumptions about the array contents not having changed since the start of this function. It sort of looks like the design is based on the assumption that the array contents can't change while SyncRepLock is held ... but if that's the plan then why bother with the per-walsender spinlocks? In any case this assumption seems to be failing, suggesting either that there's a caller that's not holding SyncRepLock when it calls this function, or that somebody is failing to take that lock while modifying the array. regards, tom lane [1] https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=3Dhoverfly&dt=3D= 2020-02-29%2001%3A34%3A55 [2] https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=3Dhoverfly&dt=3D= 2020-03-26%2013%3A51%3A15