pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Ian Lance Taylor <ian@airs.com>
To: tgl@sss.pgh.pa.us
Cc: mathijs@ilse.nl
Cc: pgsql-hackers@postgresql.org
Subject: Re: Re: [SQL] PostgreSQL crashes on me :(
Date: 18 Dec 2000 09:33:40 -0800
Message-ID: <20001218173340.29569.qmail@daffy.airs.com> (raw)
In-Reply-To: <21339.977111275@sss.pgh.pa.us>
References: <20001217215118.D64770@ilse.nl>
	<21339.977111275@sss.pgh.pa.us>

   Date: Sun, 17 Dec 2000 22:47:55 -0500
   From: Tom Lane <tgl@sss.pgh.pa.us>

   BUT: I think there's a race
   condition here, at least on systems where errno is not saved and
   restored around a signal handler.  Consider the following scenario:

	   Postmaster is waiting at the select() --- its normal state.

	   Postmaster receives a SIGCHLD signal due to backend exit, so
	   it goes off and does the reaper() thing.  On return from
	   reaper() the system arranges to return EINTR error from
	   the select().

	   Before control can reach the "if (errno..." test, another
	   SIGCHLD comes in.  reaper() is invoked again and does its
	   thing.

   The normal exit condition from reaper() will be errno == ECHILD,
   because that's what the waitpid() or wait3() call will return after
   all children are dealt with.  If the signal-handling mechanism allows
   that to be returned to the mainline code, we have a failure.

   Can any FreeBSD hackers comment on the plausibility of this theory?

I'm not a FreeBSD hacker, but I do know how the BSD kernel works
unless FreeBSD has changed things.  The important facts are:

1) The kernel only delivers signals when a process moves from kernel
   mode to user mode, after a system call or an interrupt (including a
   timer interrupt).

2) The errno variable is set in user space after the process has
   returned to user mode.

Therefore, the scenario you describe is possible, but only if there
happens to be both a timer interrupt and a SIGCHLD signal within a
couple of instructions after the select returns.

(I suppose that a page fault instead of a timer interrupt could have
the same effect as well, although a page fault here seems quite
unlikely unless the system is extremely overloaded.)

   A quick-and-dirty workaround would be to save and restore errno in
   reaper() and the other postmaster signal handlers.  It might be
   a better idea in the long run to avoid doing system calls in the
   signal handlers --- but that would take a more substantial rewrite.

Ideally, signal handlers should not make system calls.  However, if
this is impossible, then signal handlers must save and restore errno.

Ian



view thread (12+ messages)  latest in thread

Message-ID: <20001218173340.29569.qmail@daffy.airs.com>
Permalink:  ../20001218173340.29569.qmail@daffy.airs.com/
Also on:    postgresql.org/message-id/20001218173340.29569.qmail@daffy.airs.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-sql@postgresql.org
  Cc: ian@airs.com, tgl@sss.pgh.pa.us, mathijs@ilse.nl, pgsql-hackers@postgresql.org
  Subject: Re: [SQL] PostgreSQL crashes on me :(
  In-Reply-To: <20001218173340.29569.qmail@daffy.airs.com>
  References: <20001217215118.D64770@ilse.nl> <21339.977111275@sss.pgh.pa.us> <20001218173340.29569.qmail@daffy.airs.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