pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Noah Misch <noah@leadboat.com>
To: Shiv Shivaraju Gowda (shivshi) <shivshi@cisco.com>
Cc: pgsql-bugs@postgresql.org <pgsql-bugs@postgresql.org>
Cc: alvherre@2ndquadrant.com
Subject: Re: PostgreSQL 6.2.5 Visual Studio Build does not pass the regression tests.
Date: Thu, 23 Jan 2014 23:27:42 -0500
Message-ID: <20140124042742.GA1975540@tornado.leadboat.com> (raw)
In-Reply-To: <CEB3FA9D.6450%shivshi@cisco.com>
References: <CEB3FA9D.6450%shivshi@cisco.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

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<http://msdn.microsoft.com/en-us/library/windows/desktop/aa365468(v=vs.85).aspx;, WriteFileEx<http://msdn.microsoft.com/en-us/library/windows/desktop/aa365748(v=vs.85).aspx;, QueueUserAPC<http://msdn.microsoft.com/en-us/library/windows/desktop/ms684954(v=vs.85).aspx;) 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

-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

Attachments:

w32-io-completion-v1.patch (text/plain, 3.1K) download *** 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; } /*

view thread (11+ messages)  latest in thread

Message-ID: <20140124042742.GA1975540@tornado.leadboat.com>
Permalink:  ../20140124042742.GA1975540@tornado.leadboat.com/
Also on:    postgresql.org/message-id/20140124042742.GA1975540@tornado.leadboat.com

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: noah@leadboat.com, shivshi@cisco.com, alvherre@2ndquadrant.com
  Subject: Re: PostgreSQL 6.2.5 Visual Studio Build does not pass the regression tests.
  In-Reply-To: <20140124042742.GA1975540@tornado.leadboat.com>
  References: <CEB3FA9D.6450%shivshi@cisco.com> <20140124042742.GA1975540@tornado.leadboat.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox