Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dt6hT-0003rn-30 for pgsql-bugs@arkaria.postgresql.org; Sat, 16 Sep 2017 06:35:35 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1dsyDC-0004d7-PJ for pgsql-bugs@arkaria.postgresql.org; Fri, 15 Sep 2017 21:31:46 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dsyCU-0003Nl-4h for pgsql-bugs@postgresql.org; Fri, 15 Sep 2017 21:31:02 +0000 Received: from placid1.grammatech.com ([98.159.213.246] helo=placid.grammatech.com) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dsyCM-0007hU-CT for pgsql-bugs@postgresql.org; Fri, 15 Sep 2017 21:31:01 +0000 Received: from placid.grammatech.com (placid1 [192.168.219.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by placid.grammatech.com (Postfix) with ESMTPS id 1A1DE1A8002 for ; Fri, 15 Sep 2017 17:30:52 -0400 (EDT) Received: from [10.233.219.191] (barracuda.grammatech.com [192.168.219.10]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by placid.grammatech.com (Postfix) with ESMTPSA id 0D7B2B2098 for ; Fri, 15 Sep 2017 17:30:52 -0400 (EDT) To: "pgsql-bugs@postgresql.org" From: Dave Vitek Subject: PANIC during exit on behalf of FATAL semop error Message-ID: <075f128e-5e1b-694c-c947-d48cc56e4123@grammatech.com> Date: Fri, 15 Sep 2017 17:30:51 -0400 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.3.0 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="------------EA4F3EF6EC941E6BA21BCAC4" Content-Language: en-US List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org This is a multi-part message in MIME format. --------------EA4F3EF6EC941E6BA21BCAC4 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit We have an x86_64 linux machine running postgresql 9.6.2.  Our application uses LISTEN/NOTIFY.  We recently made a change so that our testing infrastructure would notice postgres crashes and out popped this crash: elog(PANIC, "queueing for lock while waiting on another one"); in this code: /*  * Add ourselves to the end of the queue.  *  * NB: Mode can be LW_WAIT_UNTIL_FREE here!  */ static void LWLockQueueSelf(LWLock *lock, LWLockMode mode) {         /*          * If we don't have a PGPROC structure, there's no way to wait. This          * should never occur, since MyProc should only be null during shared          * memory initialization.          */         if (MyProc == NULL)                 elog(PANIC, "cannot wait without a PGPROC structure");         if (MyProc->lwWaiting) ----->       elog(PANIC, "queueing for lock while waiting on another one"); <-------------------         LWLockWaitListLock(lock); ... Here's a stack trace and a more verbose stack trace. #0 0x00007ff27bb50c37 in __GI_raise (sig=sig@entry=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:56 #1 0x00007ff27bb54028 in __GI_abort () at abort.c:89 #2 0x0000000000978119 in errfinish (dummy=0) at elog.c:557 #3 0x000000000097a671 in elog_finish (elevel=22, fmt=0xc5a000 "queueing for lock while waiting on another one") at elog.c:1378 #4 0x000000000080eb73 in LWLockQueueSelf (lock=0x7ff272b06480, mode=LW_EXCLUSIVE) at lwlock.c:1035 #5 0x000000000080ee30 in LWLockAcquire (lock=0x7ff272b06480, mode=LW_EXCLUSIVE) at lwlock.c:1250 #6 0x00000000007fe416 in CleanupInvalidationState (status=1, arg=140679430974720) at sinvaladt.c:344 #7 0x00000000007f5132 in shmem_exit (code=1) at ipc.c:261 #8 0x00000000007f4f63 in proc_exit_prepare (code=1) at ipc.c:185 #9 0x00000000007f4eb3 in proc_exit (code=1) at ipc.c:102 #10 0x00000000009780e7 in errfinish (dummy=0) at elog.c:543 #11 0x000000000097a671 in elog_finish (elevel=21, fmt=0xc45b4a "semop(id=%d) failed: %m") at elog.c:1378 #12 0x00000000007881d9 in PGSemaphoreLock (sema=0x7ff27b7d6740) at pg_sema.c:391 #13 0x000000000080ee74 in LWLockAcquire (lock=0x7ff272b06f00, mode=LW_SHARED) at lwlock.c:1287 #14 0x0000000000604177 in asyncQueueReadAllNotifications () at async.c:1877 #15 0x000000000060464c in ProcessIncomingNotify () at async.c:2058 #16 0x0000000000603f04 in ProcessNotifyInterrupt () at async.c:1732 #17 0x000000000081da4d in ProcessClientReadInterrupt (blocked=1 '\001') at postgres.c:537 #18 0x00000000006d7a8d in secure_read (port=0x2bb90b0, ptr=0xfe8da0 , len=8192) at be-secure.c:177 #19 0x00000000006e3f82 in pq_recvbuf () at pqcomm.c:921 #20 0x00000000006e4022 in pq_getbyte () at pqcomm.c:964 #21 0x000000000081d495 in SocketBackend (inBuf=0x7ffe148694a0) at postgres.c:334 #22 0x000000000081d9db in ReadCommand (inBuf=0x7ffe148694a0) at postgres.c:507 #23 0x00000000008228ff in PostgresMain (argc=1, argv=0x2bbb308, dbname=0x2bbb2f0 "cshub", username=0x2bbb2d0 "cshubuser") at postgres.c:4021 #24 0x000000000079f903 in BackendRun (port=0x2bb90b0) at postmaster.c:4272 #25 0x000000000079ef9d in BackendStartup (port=0x2bb90b0) at postmaster.c:3946 #26 0x000000000079b645 in ServerLoop () at postmaster.c:1701 #27 0x000000000079ab97 in PostmasterMain (argc=3, argv=0x2b918b0) at postmaster.c:1309 #28 0x00000000006e8f8c in main (argc=3, argv=0x2b918b0) at main.c:228 So the PANIC is really happening because of the first problem: /*  * PGSemaphoreLock  *  * Lock a semaphore (decrement count), blocking if count would be < 0  */ void PGSemaphoreLock(PGSemaphore sema) {         int                     errStatus;         struct sembuf sops;         sops.sem_op = -1;                       /* decrement */         sops.sem_flg = 0;         sops.sem_num = sema->semNum;         /*          * Note: if errStatus is -1 and errno == EINTR then it means we returned          * from the operation prematurely because we were sent a signal.  So we          * try and lock the semaphore again.          *          * We used to check interrupts here, but that required servicing          * interrupts directly from signal handlers. Which is hard to do safely          * and portably.          */         do         {                 errStatus = semop(sema->semId, &sops, 1);         } while (errStatus < 0 && errno == EINTR);         if (errStatus < 0) ------>     elog(FATAL, "semop(id=%d) failed: %m", sema->semId);  <-------------- } Should this one use PANIC instead of FATAL given that the FATAL exit path causes a PANIC in some cases?  Is there an opportunity to repair the state of things enough that a FATAL exit is possible here? I'm not seeing either of the messages in the postgres logs, so I'm not sure what %m expanded to.  The logs are short and uninteresting--just startup and shutdown messages.  It is possible the machine was being stressed when this occurred. --------------EA4F3EF6EC941E6BA21BCAC4 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit We have an x86_64 linux machine running postgresql 9.6.2.  Our application uses LISTEN/NOTIFY.  We recently made a change so that our testing infrastructure would notice postgres crashes and out popped this crash:

elog(PANIC, "queueing for lock while waiting on another one");

in this code:

/*
 * Add ourselves to the end of the queue.
 *
 * NB: Mode can be LW_WAIT_UNTIL_FREE here!
 */
static void
LWLockQueueSelf(LWLock *lock, LWLockMode mode)
{
        /*
         * If we don't have a PGPROC structure, there's no way to wait. This
         * should never occur, since MyProc should only be null during shared
         * memory initialization.
         */
        if (MyProc == NULL)
                elog(PANIC, "cannot wait without a PGPROC structure");

        if (MyProc->lwWaiting)
----->       elog(PANIC, "queueing for lock while waiting on another one"); <-------------------

        LWLockWaitListLock(lock);
...


Here's a stack trace and a more verbose stack trace.

#0  0x00007ff27bb50c37 in __GI_raise (sig=sig@entry=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
#1  0x00007ff27bb54028 in __GI_abort () at abort.c:89
#2  0x0000000000978119 in errfinish (dummy=0) at elog.c:557
#3  0x000000000097a671 in elog_finish (elevel=22, fmt=0xc5a000 "queueing for lock while waiting on another one") at elog.c:1378
#4  0x000000000080eb73 in LWLockQueueSelf (lock=0x7ff272b06480, mode=LW_EXCLUSIVE) at lwlock.c:1035
#5  0x000000000080ee30 in LWLockAcquire (lock=0x7ff272b06480, mode=LW_EXCLUSIVE) at lwlock.c:1250
#6  0x00000000007fe416 in CleanupInvalidationState (status=1, arg=140679430974720) at sinvaladt.c:344
#7  0x00000000007f5132 in shmem_exit (code=1) at ipc.c:261
#8  0x00000000007f4f63 in proc_exit_prepare (code=1) at ipc.c:185
#9  0x00000000007f4eb3 in proc_exit (code=1) at ipc.c:102
#10 0x00000000009780e7 in errfinish (dummy=0) at elog.c:543
#11 0x000000000097a671 in elog_finish (elevel=21, fmt=0xc45b4a "semop(id=%d) failed: %m") at elog.c:1378
#12 0x00000000007881d9 in PGSemaphoreLock (sema=0x7ff27b7d6740) at pg_sema.c:391
#13 0x000000000080ee74 in LWLockAcquire (lock=0x7ff272b06f00, mode=LW_SHARED) at lwlock.c:1287
#14 0x0000000000604177 in asyncQueueReadAllNotifications () at async.c:1877
#15 0x000000000060464c in ProcessIncomingNotify () at async.c:2058
#16 0x0000000000603f04 in ProcessNotifyInterrupt () at async.c:1732
#17 0x000000000081da4d in ProcessClientReadInterrupt (blocked=1 '\001') at postgres.c:537
#18 0x00000000006d7a8d in secure_read (port=0x2bb90b0, ptr=0xfe8da0 <PqRecvBuffer>, len=8192) at be-secure.c:177
#19 0x00000000006e3f82 in pq_recvbuf () at pqcomm.c:921
#20 0x00000000006e4022 in pq_getbyte () at pqcomm.c:964
#21 0x000000000081d495 in SocketBackend (inBuf=0x7ffe148694a0) at postgres.c:334
#22 0x000000000081d9db in ReadCommand (inBuf=0x7ffe148694a0) at postgres.c:507
#23 0x00000000008228ff in PostgresMain (argc=1, argv=0x2bbb308, dbname=0x2bbb2f0 "cshub", username=0x2bbb2d0 "cshubuser") at postgres.c:4021
#24 0x000000000079f903 in BackendRun (port=0x2bb90b0) at postmaster.c:4272
#25 0x000000000079ef9d in BackendStartup (port=0x2bb90b0) at postmaster.c:3946
#26 0x000000000079b645 in ServerLoop () at postmaster.c:1701
#27 0x000000000079ab97 in PostmasterMain (argc=3, argv=0x2b918b0) at postmaster.c:1309
#28 0x00000000006e8f8c in main (argc=3, argv=0x2b918b0) at main.c:228


So the PANIC is really happening because of the first problem:

/*
 * PGSemaphoreLock
 *
 * Lock a semaphore (decrement count), blocking if count would be < 0
 */
void
PGSemaphoreLock(PGSemaphore sema)
{
        int                     errStatus;
        struct sembuf sops;

        sops.sem_op = -1;                       /* decrement */
        sops.sem_flg = 0;
        sops.sem_num = sema->semNum;

        /*
         * Note: if errStatus is -1 and errno == EINTR then it means we returned
         * from the operation prematurely because we were sent a signal.  So we
         * try and lock the semaphore again.
         *
         * We used to check interrupts here, but that required servicing
         * interrupts directly from signal handlers. Which is hard to do safely
         * and portably.
         */
        do
        {
                errStatus = semop(sema->semId, &sops, 1);
        } while (errStatus < 0 && errno == EINTR);

        if (errStatus < 0)
------>     elog(FATAL, "semop(id=%d) failed: %m", sema->semId);  <--------------
}

Should this one use PANIC instead of FATAL given that the FATAL exit path causes a PANIC in some cases?  Is there an opportunity to repair the state of things enough that a FATAL exit is possible here?

I'm not seeing either of the messages in the postgres logs, so I'm not sure what %m expanded to.  The logs are short and uninteresting--just startup and shutdown messages.  It is possible the machine was being stressed when this occurred.


--------------EA4F3EF6EC941E6BA21BCAC4--