Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lOKnm-0004l7-H2 for pgsql-hackers@arkaria.postgresql.org; Mon, 22 Mar 2021 13:41:02 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lOKnl-0007z6-FN for pgsql-hackers@arkaria.postgresql.org; Mon, 22 Mar 2021 13:41:01 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lOKnl-0007yz-8h for pgsql-hackers@lists.postgresql.org; Mon, 22 Mar 2021 13:41:01 +0000 Received: from oss.nttdata.com ([49.212.34.109]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lOKni-0008Qy-CV for pgsql-hackers@postgresql.org; Mon, 22 Mar 2021 13:41:00 +0000 Received: from hnk.local (p2350207-ipbf2126funabasi.chiba.ocn.ne.jp [114.167.184.207]) by oss.nttdata.com (Postfix) with ESMTPSA id 814E662600; Mon, 22 Mar 2021 22:40:54 +0900 (JST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.102.3 at oss.nttdata.com Subject: Re: Failed assertion on standby while shutdown To: Maxim Orlov , Pgsql Hackers References: From: Fujii Masao Message-ID: <9c7b69c2-fb17-1780-e475-7dd2c6bbe18b@oss.nttdata.com> Date: Mon, 22 Mar 2021 22:40:54 +0900 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:78.0) Gecko/20100101 Thunderbird/78.8.1 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/mixed; boundary="------------774718B88240D5725827601A" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------774718B88240D5725827601A Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit On 2021/03/20 2:25, Maxim Orlov wrote: > Hi, haсkers! > > Recently, I was doing some experiments with primary/standby instances interaction. In certain conditions I’ve got and was able to reproduce crash on failed assertion. > > The scenario is the following: > 1. start primary server > 2. start standby server by pg_basebackup -P -R -X stream -c fast -p5432 -D data > 3. apply some load to the primary server by pgbench -p5432 -i -s 150 postgres > 4. kill primary server (with kill -9) and keep it down > 5. stop standby server by pg_ctl > 6. run standby server > > Then any standby server termination will result in a failed assertion. > > The log with a backtrace is following: > > 2021-03-19 18:54:25.352 MSK [3508443] LOG:  received fast shutdown request > 2021-03-19 18:54:25.379 MSK [3508443] LOG:  aborting any active transactions > TRAP: FailedAssertion("SHMQueueEmpty(&(MyProc->myProcLocks[i]))", File: "/home/ziva/projects/pgpro/build-secondary/../postgrespro/src/backend/storage/lmgr/proc.c", Line: 592, PID: 3508452) > postgres: walreceiver (ExceptionalCondition+0xd0)[0x555555d0526f] > postgres: walreceiver (InitAuxiliaryProcess+0x31c)[0x555555b43e31] > postgres: walreceiver (AuxiliaryProcessMain+0x54f)[0x55555574ae32] > postgres: walreceiver (+0x530bff)[0x555555a84bff] > postgres: walreceiver (+0x531044)[0x555555a85044] > postgres: walreceiver (+0x530959)[0x555555a84959] > /lib/x86_64-linux-gnu/libpthread.so.0(+0x153c0)[0x7ffff7a303c0] > /lib/x86_64-linux-gnu/libc.so.6(__select+0x1a)[0x7ffff72a40da] > postgres: walreceiver (+0x52bea4)[0x555555a7fea4] > postgres: walreceiver (PostmasterMain+0x129f)[0x555555a7f7c1] > postgres: walreceiver (+0x41ff1f)[0x555555973f1f] > /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0x7ffff71b30b3] > postgres: walreceiver (_start+0x2e)[0x55555561abfe] > > After a brief investigation I found out that I can get this assert with 100% probability if I insert a sleep for about 5 sec into InitAuxiliaryProcess(void) in src/backend/storage/lmgr/proc.c: > > diff --git a/src/backend/storage/lmgr/proc.c b/src/backend/storage/lmgr/proc.c > index 897045ee272..b5f365f426d 100644 > --- a/src/backend/storage/lmgr/proc.c > +++ b/src/backend/storage/lmgr/proc.c > @@ -525,7 +525,7 @@ InitAuxiliaryProcess(void) > >         if (MyProc != NULL) >                 elog(ERROR, "you already exist"); > - > +       pg_usleep(5000000L); >         /* >          * We use the ProcStructLock to protect assignment and releasing of >          * AuxiliaryProcs entries. Thanks for the report! I could reproduce this issue by adding that sleep into InitAuxiliaryProcess(). > Maybe, this kinda behaviour would appear if a computer hosting instances is under significant side load, which cause delay to start db-instances under a heavy load. > > Configuration for a primary server is default with "wal_level = logical" > > Configuration for a standby server is default with "wal_level = logical" and "primary_conninfo = 'port=5432'" > > I'm puzzled with this behavor. I'm pretty sure it is not what should be. Any ideas how this can be fixed? ISTM that the cause of this issue is that the startup process exits without releasing the locks that it was holding when shutdown is requested. To address this issue, IMO the startup process should call ShutdownRecoveryTransactionEnvironment() at its exit. Attached is the POC patch that changes the startup process that way. I've not tested the patch enough yet.. Regards, -- Fujii Masao Advanced Computing Technology Center Research and Development Headquarters NTT DATA CORPORATION --------------774718B88240D5725827601A Content-Type: text/plain; charset=UTF-8; x-mac-type="0"; x-mac-creator="0"; name="fix_assertion_failure_walreceiver.patch" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="fix_assertion_failure_walreceiver.patch" ZGlmZiAtLWdpdCBhL3NyYy9iYWNrZW5kL3Bvc3RtYXN0ZXIvc3RhcnR1cC5jIGIvc3JjL2Jh Y2tlbmQvcG9zdG1hc3Rlci9zdGFydHVwLmMKaW5kZXggMjIxMzVkNWUwNy4uNjkwNzdiZDIw NyAxMDA2NDQKLS0tIGEvc3JjL2JhY2tlbmQvcG9zdG1hc3Rlci9zdGFydHVwLmMKKysrIGIv c3JjL2JhY2tlbmQvcG9zdG1hc3Rlci9zdGFydHVwLmMKQEAgLTYyLDYgKzYy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