Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g1IrD-0003Hq-Mv for pgsql-hackers@arkaria.postgresql.org; Sat, 15 Sep 2018 22:16:03 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1g1Ir8-0006kp-WC for pgsql-hackers@arkaria.postgresql.org; Sat, 15 Sep 2018 22:15:58 +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.89) (envelope-from ) id 1g1Ir8-0006kZ-Go for pgsql-hackers@lists.postgresql.org; Sat, 15 Sep 2018 22:15:58 +0000 Received: from mail-pl1-x644.google.com ([2607:f8b0:4864:20::644]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1g1Ir3-0006oF-RX for pgsql-hackers@postgresql.org; Sat, 15 Sep 2018 22:15:57 +0000 Received: by mail-pl1-x644.google.com with SMTP id f6-v6so5732010plo.1 for ; Sat, 15 Sep 2018 15:15:53 -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=3kBEv4NBX+ejyI5qHy/XNaVgaVAcbio+T0OqiY/Vsrs=; b=Mx+6f2zIrW4puUb+nQsh1RFIveXpxeXUxmgSNpSB1zGeGtiF+z3aVC4GIoor4ayrmW +x4Vbyl8I+/FTLY9QiBd1AC0EHAOUSIbVy5WQVqWxmPF0tDcS3Km3beKrWICXQ4fLjZy KJ71OEXA/+obRprjnn2I8iRzkecjpyNSLIoGw= 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=3kBEv4NBX+ejyI5qHy/XNaVgaVAcbio+T0OqiY/Vsrs=; b=aVrW7ENnaqv4NN0VlgL4uM7TL++41wyKo6PVqm0t3z9ljnhjk8eCkvFECMhkGyIdv+ 0sLgEXTKa+xdGTDP1eHOT46nKD71szDr3P7R0g9zAL7+5srxd12jb9C9Jh7O69pO+wHe hklKgCETzmTu/+ffKU/4yVrsPqsOCMKXSRyrihv+N1om0YOfWOnVObQXlqOH1e+MOnV7 NsIh6R/JnAp2CBRhALSl2Tx+49zaeadqh+F0jOuQw/tWAQCSiBq59zDSGb5gbNTCc2cv RInKw5rc/LCF76lI9QZYQYayh96VBjTGbz8CLyIuukax6SOIQEcsZ94pCZPSYUlZEsRc rk8Q== X-Gm-Message-State: APzg51CADgxSZp6vJGtVnwRf8WZYK45BXu9QqBDGFREWZVcWDnuEveqC 9V4AIulzw+K54zV2p+6RA5fKKQ== X-Google-Smtp-Source: ANB0Vdb4/22sbEueTkAGvwX+Xc2mF+r5xbQmuJloOtd8TqYODRFurTQEUet7dbWHsRx3uCg42RdUYA== X-Received: by 2002:a17:902:7614:: with SMTP id k20-v6mr18170119pll.170.1537049750419; Sat, 15 Sep 2018 15:15:50 -0700 (PDT) Received: from rfd.leadboat.com (c-73-158-248-138.hsd1.ca.comcast.net. [73.158.248.138]) by smtp.gmail.com with ESMTPSA id i25-v6sm12601986pgi.31.2018.09.15.15.15.47 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 15 Sep 2018 15:15:48 -0700 (PDT) Date: Sat, 15 Sep 2018 15:15:46 -0700 From: Noah Misch To: Robert Haas Cc: Andres Freund , Tom Lane , Andrew Dunstan , "pgsql-hackers@postgresql.org" Subject: Re: Something fishy happening on frogmouth Message-ID: <20180915221546.GA3159382@rfd.leadboat.com> References: <6557.1383073940@sss.pgh.pa.us> <20131030124734.GA11429@awork2.anarazel.de> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="tKW2IUtsqtDRztdT" Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.24 (2015-08-30) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --tKW2IUtsqtDRztdT Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Wed, Oct 30, 2013 at 09:07:43AM -0400, Robert Haas wrote: > On Wed, Oct 30, 2013 at 8:47 AM, Andres Freund wrote: > > On 2013-10-30 08:45:03 -0400, Robert Haas wrote: > >> If I'm reading this correctly, the last three runs on frogmouth have > >> all failed, and all of them have failed with a complaint about, > >> specifically, Global/PostgreSQL.851401618. Now, that really shouldn't > >> be happening, because the code to choose that number looks like this: > >> > >> dsm_control_handle = random(); > > Could it be that we haven't primed the random number generator with the > > time or something like that yet? > > Yeah, I think that's probably what it is. I experienced a variation of this, namely a RHEL 7 system where initdb always says "selecting dynamic shared memory implementation ... sysv". Each initdb is rejecting posix shm by probing the same ten segments: $ strace initdb -D scratch 2>&1 | grep /dev/shm/P open("/dev/shm/PostgreSQL.1804289383", O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW|O_CLOEXEC, 0600) = -1 EEXIST (File exists) open("/dev/shm/PostgreSQL.846930886", O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW|O_CLOEXEC, 0600) = -1 EEXIST (File exists) open("/dev/shm/PostgreSQL.1681692777", O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW|O_CLOEXEC, 0600) = -1 EEXIST (File exists) open("/dev/shm/PostgreSQL.1714636915", O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW|O_CLOEXEC, 0600) = -1 EEXIST (File exists) open("/dev/shm/PostgreSQL.1957747793", O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW|O_CLOEXEC, 0600) = -1 EEXIST (File exists) open("/dev/shm/PostgreSQL.424238335", O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW|O_CLOEXEC, 0600) = -1 EEXIST (File exists) open("/dev/shm/PostgreSQL.719885386", O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW|O_CLOEXEC, 0600) = -1 EEXIST (File exists) open("/dev/shm/PostgreSQL.1649760492", O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW|O_CLOEXEC, 0600) = -1 EEXIST (File exists) open("/dev/shm/PostgreSQL.596516649", O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW|O_CLOEXEC, 0600) = -1 EEXIST (File exists) open("/dev/shm/PostgreSQL.1189641421", O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW|O_CLOEXEC, 0600) = -1 EEXIST (File exists) Regular postmaster runs choose a random segment, but initdb, bootstrap postgres, and single-user postgres all start with the same segment. These segments are months old. Perhaps I was testing something that caused a bootstrap postgres to crash. After ten such crashes, future initdb runs considered posix shm unusable. > There's PostmasterRandom() > to initialize the random-number generator on first use, but that > doesn't help if some other module calls random(). I wonder if we > ought to just get rid of PostmasterRandom() and instead have the > postmaster run that initialization code very early in startup. Usually, the first srandom() call happens early in PostmasterMain(). I plan to add one to InitStandaloneProcess(), which substitutes for several tasks otherwise done in PostmasterMain(). That seems like a good thing even if DSM weren't in the picture. Also, initdb needs an srandom() somewhere; choose_dsm_implementation() itself seems fine. Attached. With this, "make -j20 check-world" selected posix shm and passed even when I forced DSM creation to fail on unseeded random(): --- a/src/backend/storage/ipc/dsm_impl.c +++ b/src/backend/storage/ipc/dsm_impl.c @@ -249,2 +249,5 @@ dsm_impl_posix(dsm_op op, dsm_handle handle, Size request_size, + if (handle == 1804289383) + elog(ERROR, "generated handle with no randomness"); + snprintf(name, 64, "/PostgreSQL.%u", handle); --tKW2IUtsqtDRztdT Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="dsm-standalone-random-v1.patch" diff --git a/src/backend/utils/init/miscinit.c b/src/backend/utils/init/miscinit.c index 865119d..f003831 100644 --- a/src/backend/utils/init/miscinit.c +++ b/src/backend/utils/init/miscinit.c @@ -325,6 +325,8 @@ InitStandaloneProcess(const char *argv0) MyStartTime = time(NULL); /* set our start time in case we call elog */ + srandom((unsigned int) (MyProcPid ^ MyStartTime)); + /* Initialize process-local latch support */ InitializeLatchSupport(); MyLatch = &LocalLatchData; diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c index 32746c7..83f4b0b 100644 --- a/src/bin/initdb/initdb.c +++ b/src/bin/initdb/initdb.c @@ -922,6 +922,8 @@ choose_dsm_implementation(void) #ifdef HAVE_SHM_OPEN int ntries = 10; + srandom((unsigned int) (getpid() ^ time(NULL))); + while (ntries > 0) { uint32 handle; --tKW2IUtsqtDRztdT--