Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1W6YN1-0005d8-Io for pgsql-bugs@arkaria.postgresql.org; Fri, 24 Jan 2014 04:27:55 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1W6YN0-0000ic-US for pgsql-bugs@arkaria.postgresql.org; Fri, 24 Jan 2014 04:27:54 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1W6YN0-0000iW-1Q for pgsql-bugs@postgresql.org; Fri, 24 Jan 2014 04:27:54 +0000 Received: from mail-qc0-f179.google.com ([209.85.216.179]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1W6YMs-0007yt-Gb for pgsql-bugs@postgresql.org; Fri, 24 Jan 2014 04:27:53 +0000 Received: by mail-qc0-f179.google.com with SMTP id e16so3727079qcx.24 for ; Thu, 23 Jan 2014 20:27:45 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-type:content-disposition:in-reply-to :user-agent; bh=e8h8PT/vRNjMnd06smZbjbboFIlhQQffjocyWdq5A/k=; b=e/85r9wP8uVKykFDLPlDDUVurpsZyzkmlllvz7fK4HALh8DD6SCQpCivJwwPg4wSz6 LfSh1TO2mrorB5HEb9sCk4bQv+46pjJGbdxN7Mq0A0EjsZgsC1wZXqnRFtpPeNVylp3x tlMIB4Jl3FABSgqeRV923n2sOQO//IaKLsB6MiXV4C5iOOMfwUnd/8E4EcSWXTXkRK5i r6MO0hSxEN6zB8HNXfu8ohltmhetqBcNivt0BKkRWd+TnKq2E36E/qfDlN7uw1QBP0zP /8UA00KOcQlaPCsQtfN6ZS1hK6RWD2DmDcVtrWm0b7ahEHKUZNutdRhsM5MBCUdnMPU4 DLxQ== X-Gm-Message-State: ALoCoQmJhCl3j/j7HudAGaoTP5Sb8hffoAg6WCp4KpArr7pDa9mShKDWBqYjfrcCtQ+rpNYI5v+E X-Received: by 10.140.91.12 with SMTP id y12mr16288080qgd.26.1390537665311; Thu, 23 Jan 2014 20:27:45 -0800 (PST) Received: from tornado.leadboat.com (c-107-4-219-91.hsd1.va.comcast.net. [107.4.219.91]) by mx.google.com with ESMTPSA id r13sm8900565qan.7.2014.01.23.20.27.43 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Thu, 23 Jan 2014 20:27:44 -0800 (PST) Date: Thu, 23 Jan 2014 23:27:42 -0500 From: Noah Misch To: "Shiv Shivaraju Gowda (shivshi)" Cc: "pgsql-bugs@postgresql.org" , alvherre@2ndquadrant.com Subject: Re: PostgreSQL 6.2.5 Visual Studio Build does not pass the regression tests. Message-ID: <20140124042742.GA1975540@tornado.leadboat.com> References: MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="envbJBWh7q8WU6mo" Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) X-Pg-Spam-Score: -2.6 (--) 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 --envbJBWh7q8WU6mo Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Fri, Nov 22, 2013 at 01:59:58AM +0000, Shiv Shivaraju Gowda (shivshi) wrote: > There seems to be a has a dormant bug in PostgreSQL source code build using VisualStudio(VS) which shows up with the newer OS( not sure if it is the OS or some other thing in the environment ). Thanks for this report, and apologies for the delay. Please tell us as much as possible about the OS configurations that have exhibited the problem and the OS configurations that have *not* exhibited the problem with the same binaries. > MinGW build doesn't access this code path and thus doesn't hit this bug. A MinGW build certainly includes win32_sema.c, and I don't see why it wouldn't use that code path. How did you determine that? > SYMPTOM: > The following symptom is encountered in PostgreSQL build using Visual Studio: PostgreSQL regression tests fail with server crashing repeatedly with this message in the log file: "PANIC: could not lock semaphore: error code 0". The issue is encountered for VS 2005, VS 2008( 32bit and 64bit executables), VS 2010 and VS 2012 built executables. The issue was reproducible with PostgreSQL 6.2.3 and 6.2.5. We didn't encounter this issue in MinGW build or EnterpriseDB Packaged executables (which seems to have been built using VisualStudio 2010). I assume you mean 9.2.3 and 9.2.5? > CAUSE: > The PGSemaphoreLock function in postgresql-9.2.5\src\backend\port\win32_sema.c (https://github.com/postgres/postgres/blob/master/src/backend/port/win32_sema.c) uses "Ex" version of "WaitForMultipleObjects" Windows function (http://msdn.microsoft.com/en-us/library/windows/desktop/ms687028%28v=vs.85%29.aspx) however doesn't handle the additional awake calls from the "bAlertable" state. Specifically, it doesn't handle the WAIT_IO_COMPLETION return code when woken up by a User-mode Asynchronous Procedure Call(APC) or Async IO completion. > > The part I do NOT understand is why do the User-Mode APC or Async IO completion triggers get fired only in the executables we built and not in the ones built by EnterpriseDB and bits built by other users since no one has complained about it. Irrespective if it is triggered or not, the code should have handled all the return codes of the WaitForMultipleObjectsEX API and that is the reason I think it is a bug. > > I checked the source code for calls which will trigger the Async IO or user-mode APC(ReadFileEx, WriteFileEx, QueueUserAPC) and could not find any. I am not sure what triggers the WAIT_IO_COMPLETION return call. I could not find a way to figure out that information in the debug environment. Yes, that's weird. I could believe anti-malware software doing this. If any successful test has taken place on the same machine and user account as the unsuccessful test, that explanation is unlikely. It's also possible that you're building against an unusual version of a 3rd-party library that, unlike versions used in popular builds, does use APCs or async I/O. All that being said, it's clearly wrong to call WaitForMultipleObjectsEx(..., TRUE) and then treat WAIT_IO_COMPLETION like WAIT_FAILED. > (Posible) FIX: > Either of the following changes to the PGSemaphoreLock function work fine and pass the regression tests. > > 1) Replace the call to WaitForMultipleObjectsEx with WaitForMultipleObjects. I don't know how we decided which waits to make alertable, so I lean against changing this wait from alertable to normal. > 2) Handle the WAIT_IO_COMPLETION return code same as WAIT_OBJECT_0. There is a similar code like this in socket.c, so this change should be safe too. Sounds good. I looked through the other WaitFor*Object* callers for similar problems. pgwin32_select() also mishandles WAIT_IO_COMPLETION, returning zero as though the full timeout had elapsed. It should return -1 and set EINTR. This could affect CheckRADIUSAuth() and PgstatCollectorMain(), both of which distinguish full timeout expiration from EINTR. Would you verify that the attached patch fixes your builds? nm -- Noah Misch EnterpriseDB http://www.enterprisedb.com --envbJBWh7q8WU6mo Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="w32-io-completion-v1.patch" *** a/src/backend/port/win32/socket.c --- b/src/backend/port/win32/socket.c *************** *** 623,629 **** pgwin32_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, c return 0; } ! if (r == WAIT_OBJECT_0 + numevents) { pgwin32_dispatch_queued_signals(); errno = EINTR; --- 623,630 ---- return 0; } ! /* Signal-like events. */ ! if (r == WAIT_OBJECT_0 + numevents || r == WAIT_IO_COMPLETION) { pgwin32_dispatch_queued_signals(); errno = EINTR; *** a/src/backend/port/win32_sema.c --- b/src/backend/port/win32_sema.c *************** *** 118,125 **** PGSemaphoreReset(PGSemaphore sema) void PGSemaphoreLock(PGSemaphore sema, bool interruptOK) { - DWORD ret; HANDLE wh[2]; /* * Note: pgwin32_signal_event should be first to ensure that it will be --- 118,127 ---- void PGSemaphoreLock(PGSemaphore sema, bool interruptOK) { HANDLE wh[2]; + bool done = false; + + ImmediateInterruptOK = interruptOK; /* * Note: pgwin32_signal_event should be first to ensure that it will be *************** *** 135,168 **** PGSemaphoreLock(PGSemaphore sema, bool interruptOK) * no hidden magic about whether the syscall will internally service a * signal --- we do that ourselves. */ ! do { ! ImmediateInterruptOK = interruptOK; ! CHECK_FOR_INTERRUPTS(); ! ret = WaitForMultipleObjectsEx(2, wh, FALSE, INFINITE, TRUE); ! if (ret == WAIT_OBJECT_0) { ! /* Signal event is set - we have a signal to deliver */ ! pgwin32_dispatch_queued_signals(); ! errno = EINTR; } ! else if (ret == WAIT_OBJECT_0 + 1) ! { ! /* We got it! */ ! errno = 0; ! } ! else ! /* Otherwise we are in trouble */ ! errno = EIDRM; ! ! ImmediateInterruptOK = false; ! } while (errno == EINTR); ! if (errno != 0) ! ereport(FATAL, ! (errmsg("could not lock semaphore: error code %lu", GetLastError()))); } /* --- 137,180 ---- * no hidden magic about whether the syscall will internally service a * signal --- we do that ourselves. */ ! while (!done) { ! DWORD rc; ! CHECK_FOR_INTERRUPTS(); ! rc = WaitForMultipleObjectsEx(2, wh, FALSE, INFINITE, TRUE); ! switch (rc) { ! case WAIT_OBJECT_0: ! /* Signal event is set - we have a signal to deliver */ ! pgwin32_dispatch_queued_signals(); ! break; ! case WAIT_OBJECT_0 + 1: ! /* We got it! */ ! done = true; ! break; ! case WAIT_IO_COMPLETION: ! /* ! * The system interrupted the wait to execute an I/O ! * completion routine or asynchronous procedure call in this ! * thread. PostgreSQL does not provoke either of these, but ! * atypical loaded DLLs or even other processes might do so. ! * Now, resume waiting. ! */ ! break; ! case WAIT_FAILED: ! ereport(FATAL, ! (errmsg("could not lock semaphore: error code %lu", ! GetLastError()))); ! break; ! default: ! elog(FATAL, "unexpected return code from WaitForMultipleObjectsEx(): %lu", rc); ! break; } ! } ! ImmediateInterruptOK = false; } /* --envbJBWh7q8WU6mo Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs --envbJBWh7q8WU6mo--