Commit e086b553 authored by Thomas Munro's avatar Thomas Munro

Don't trust signalfd() on illumos.

Since commit 6a2a70a0, we've used signalfd() to receive latch wakeups
when building with WAIT_USE_EPOLL (default for Linux and illumos), and
our traditional self-pipe when falling back to WAIT_USE_POLL (default
for other Unixes with neither epoll() nor kqueue()).

Unexplained hangs and kernel panics have been reported on illumos
systems, apparently linked to this use of signalfd(), leading illumos
users and build farm members to have to define WAIT_USE_POLL explicitly
as a work-around.  A bug report exists at
https://www.illumos.org/issues/13700 but no fix is available yet.

Let's provide a way for illumos users to go back to self-pipes with
epoll(), like releases before 14, and choose that by default.  No change
for Linux users.  To help with development/debugging, macros
WAIT_USE_{EPOLL,POLL} and WAIT_USE_{SIGNALFD,SELF_PIPE} can be defined
explicitly to override the defaults.

Back-patch to 14, where we started using signalfd().
Reported-by: default avatarJapin Li <japinli@hotmail.com>
Reported-by: Olaf Bohlen <olbohlen@eenfach.de> (off-list)
Reviewed-by: default avatarJapin Li <japinli@hotmail.com>
Discussion: https://postgr.es/m/MEYP282MB1669C8D88F0997354C2313C1B6CA9%40MEYP282MB1669.AUSP282.PROD.OUTLOOK.COM
parent 4b0d21b0
...@@ -72,7 +72,7 @@ ...@@ -72,7 +72,7 @@
#if defined(WAIT_USE_EPOLL) || defined(WAIT_USE_POLL) || \ #if defined(WAIT_USE_EPOLL) || defined(WAIT_USE_POLL) || \
defined(WAIT_USE_KQUEUE) || defined(WAIT_USE_WIN32) defined(WAIT_USE_KQUEUE) || defined(WAIT_USE_WIN32)
/* don't overwrite manual choice */ /* don't overwrite manual choice */
#elif defined(HAVE_SYS_EPOLL_H) && defined(HAVE_SYS_SIGNALFD_H) #elif defined(HAVE_SYS_EPOLL_H)
#define WAIT_USE_EPOLL #define WAIT_USE_EPOLL
#elif defined(HAVE_KQUEUE) #elif defined(HAVE_KQUEUE)
#define WAIT_USE_KQUEUE #define WAIT_USE_KQUEUE
...@@ -84,6 +84,22 @@ ...@@ -84,6 +84,22 @@
#error "no wait set implementation available" #error "no wait set implementation available"
#endif #endif
/*
* By default, we use a self-pipe with poll() and a signalfd with epoll(), if
* available. We avoid signalfd on illumos for now based on problem reports.
* For testing the choice can also be manually specified.
*/
#if defined(WAIT_USE_POLL) || defined(WAIT_USE_EPOLL)
#if defined(WAIT_USE_SELF_PIPE) || defined(WAIT_USE_SIGNALFD)
/* don't overwrite manual choice */
#elif defined(WAIT_USE_EPOLL) && defined(HAVE_SYS_SIGNALFD_H) && \
!defined(__illumos__)
#define WAIT_USE_SIGNALFD
#else
#define WAIT_USE_SELF_PIPE
#endif
#endif
/* typedef in latch.h */ /* typedef in latch.h */
struct WaitEventSet struct WaitEventSet
{ {
...@@ -146,12 +162,12 @@ static WaitEventSet *LatchWaitSet; ...@@ -146,12 +162,12 @@ static WaitEventSet *LatchWaitSet;
static volatile sig_atomic_t waiting = false; static volatile sig_atomic_t waiting = false;
#endif #endif
#ifdef WAIT_USE_EPOLL #ifdef WAIT_USE_SIGNALFD
/* On Linux, we'll receive SIGURG via a signalfd file descriptor. */ /* On Linux, we'll receive SIGURG via a signalfd file descriptor. */
static int signal_fd = -1; static int signal_fd = -1;
#endif #endif
#if defined(WAIT_USE_POLL) #ifdef WAIT_USE_SELF_PIPE
/* Read and write ends of the self-pipe */ /* Read and write ends of the self-pipe */
static int selfpipe_readfd = -1; static int selfpipe_readfd = -1;
static int selfpipe_writefd = -1; static int selfpipe_writefd = -1;
...@@ -164,7 +180,7 @@ static void latch_sigurg_handler(SIGNAL_ARGS); ...@@ -164,7 +180,7 @@ static void latch_sigurg_handler(SIGNAL_ARGS);
static void sendSelfPipeByte(void); static void sendSelfPipeByte(void);
#endif #endif
#if defined(WAIT_USE_POLL) || defined(WAIT_USE_EPOLL) #if defined(WAIT_USE_SELF_PIPE) || defined(WAIT_USE_SIGNALFD)
static void drain(void); static void drain(void);
#endif #endif
...@@ -190,7 +206,7 @@ static inline int WaitEventSetWaitBlock(WaitEventSet *set, int cur_timeout, ...@@ -190,7 +206,7 @@ static inline int WaitEventSetWaitBlock(WaitEventSet *set, int cur_timeout,
void void
InitializeLatchSupport(void) InitializeLatchSupport(void)
{ {
#if defined(WAIT_USE_POLL) #if defined(WAIT_USE_SELF_PIPE)
int pipefd[2]; int pipefd[2];
if (IsUnderPostmaster) if (IsUnderPostmaster)
...@@ -264,7 +280,7 @@ InitializeLatchSupport(void) ...@@ -264,7 +280,7 @@ InitializeLatchSupport(void)
pqsignal(SIGURG, latch_sigurg_handler); pqsignal(SIGURG, latch_sigurg_handler);
#endif #endif
#ifdef WAIT_USE_EPOLL #ifdef WAIT_USE_SIGNALFD
sigset_t signalfd_mask; sigset_t signalfd_mask;
/* Block SIGURG, because we'll receive it through a signalfd. */ /* Block SIGURG, because we'll receive it through a signalfd. */
...@@ -316,7 +332,7 @@ ShutdownLatchSupport(void) ...@@ -316,7 +332,7 @@ ShutdownLatchSupport(void)
LatchWaitSet = NULL; LatchWaitSet = NULL;
} }
#if defined(WAIT_USE_POLL) #if defined(WAIT_USE_SELF_PIPE)
close(selfpipe_readfd); close(selfpipe_readfd);
close(selfpipe_writefd); close(selfpipe_writefd);
selfpipe_readfd = -1; selfpipe_readfd = -1;
...@@ -324,7 +340,7 @@ ShutdownLatchSupport(void) ...@@ -324,7 +340,7 @@ ShutdownLatchSupport(void)
selfpipe_owner_pid = InvalidPid; selfpipe_owner_pid = InvalidPid;
#endif #endif
#if defined(WAIT_USE_EPOLL) #if defined(WAIT_USE_SIGNALFD)
close(signal_fd); close(signal_fd);
signal_fd = -1; signal_fd = -1;
#endif #endif
...@@ -341,9 +357,12 @@ InitLatch(Latch *latch) ...@@ -341,9 +357,12 @@ InitLatch(Latch *latch)
latch->owner_pid = MyProcPid; latch->owner_pid = MyProcPid;
latch->is_shared = false; latch->is_shared = false;
#if defined(WAIT_USE_POLL) #if defined(WAIT_USE_SELF_PIPE)
/* Assert InitializeLatchSupport has been called in this process */ /* Assert InitializeLatchSupport has been called in this process */
Assert(selfpipe_readfd >= 0 && selfpipe_owner_pid == MyProcPid); Assert(selfpipe_readfd >= 0 && selfpipe_owner_pid == MyProcPid);
#elif defined(WAIT_USE_SIGNALFD)
/* Assert InitializeLatchSupport has been called in this process */
Assert(signal_fd >= 0);
#elif defined(WAIT_USE_WIN32) #elif defined(WAIT_USE_WIN32)
latch->event = CreateEvent(NULL, TRUE, FALSE, NULL); latch->event = CreateEvent(NULL, TRUE, FALSE, NULL);
if (latch->event == NULL) if (latch->event == NULL)
...@@ -405,9 +424,12 @@ OwnLatch(Latch *latch) ...@@ -405,9 +424,12 @@ OwnLatch(Latch *latch)
/* Sanity checks */ /* Sanity checks */
Assert(latch->is_shared); Assert(latch->is_shared);
#if defined(WAIT_USE_POLL) #if defined(WAIT_USE_SELF_PIPE)
/* Assert InitializeLatchSupport has been called in this process */ /* Assert InitializeLatchSupport has been called in this process */
Assert(selfpipe_readfd >= 0 && selfpipe_owner_pid == MyProcPid); Assert(selfpipe_readfd >= 0 && selfpipe_owner_pid == MyProcPid);
#elif defined(WAIT_USE_SIGNALFD)
/* Assert InitializeLatchSupport has been called in this process */
Assert(signal_fd >= 0);
#endif #endif
if (latch->owner_pid != 0) if (latch->owner_pid != 0)
...@@ -617,7 +639,7 @@ SetLatch(Latch *latch) ...@@ -617,7 +639,7 @@ SetLatch(Latch *latch)
return; return;
else if (owner_pid == MyProcPid) else if (owner_pid == MyProcPid)
{ {
#if defined(WAIT_USE_POLL) #if defined(WAIT_USE_SELF_PIPE)
if (waiting) if (waiting)
sendSelfPipeByte(); sendSelfPipeByte();
#else #else
...@@ -904,9 +926,9 @@ AddWaitEventToSet(WaitEventSet *set, uint32 events, pgsocket fd, Latch *latch, ...@@ -904,9 +926,9 @@ AddWaitEventToSet(WaitEventSet *set, uint32 events, pgsocket fd, Latch *latch,
{ {
set->latch = latch; set->latch = latch;
set->latch_pos = event->pos; set->latch_pos = event->pos;
#if defined(WAIT_USE_POLL) #if defined(WAIT_USE_SELF_PIPE)
event->fd = selfpipe_readfd; event->fd = selfpipe_readfd;
#elif defined(WAIT_USE_EPOLL) #elif defined(WAIT_USE_SIGNALFD)
event->fd = signal_fd; event->fd = signal_fd;
#else #else
event->fd = PGINVALID_SOCKET; event->fd = PGINVALID_SOCKET;
...@@ -2023,7 +2045,7 @@ GetNumRegisteredWaitEvents(WaitEventSet *set) ...@@ -2023,7 +2045,7 @@ GetNumRegisteredWaitEvents(WaitEventSet *set)
return set->nevents; return set->nevents;
} }
#if defined(WAIT_USE_POLL) #if defined(WAIT_USE_SELF_PIPE)
/* /*
* SetLatch uses SIGURG to wake up the process waiting on the latch. * SetLatch uses SIGURG to wake up the process waiting on the latch.
...@@ -2074,7 +2096,7 @@ retry: ...@@ -2074,7 +2096,7 @@ retry:
#endif #endif
#if defined(WAIT_USE_POLL) || defined(WAIT_USE_EPOLL) #if defined(WAIT_USE_SELF_PIPE) || defined(WAIT_USE_SIGNALFD)
/* /*
* Read all available data from self-pipe or signalfd. * Read all available data from self-pipe or signalfd.
...@@ -2090,7 +2112,7 @@ drain(void) ...@@ -2090,7 +2112,7 @@ drain(void)
int rc; int rc;
int fd; int fd;
#ifdef WAIT_USE_POLL #ifdef WAIT_USE_SELF_PIPE
fd = selfpipe_readfd; fd = selfpipe_readfd;
#else #else
fd = signal_fd; fd = signal_fd;
...@@ -2108,7 +2130,7 @@ drain(void) ...@@ -2108,7 +2130,7 @@ drain(void)
else else
{ {
waiting = false; waiting = false;
#ifdef WAIT_USE_POLL #ifdef WAIT_USE_SELF_PIPE
elog(ERROR, "read() on self-pipe failed: %m"); elog(ERROR, "read() on self-pipe failed: %m");
#else #else
elog(ERROR, "read() on signalfd failed: %m"); elog(ERROR, "read() on signalfd failed: %m");
...@@ -2118,7 +2140,7 @@ drain(void) ...@@ -2118,7 +2140,7 @@ drain(void)
else if (rc == 0) else if (rc == 0)
{ {
waiting = false; waiting = false;
#ifdef WAIT_USE_POLL #ifdef WAIT_USE_SELF_PIPE
elog(ERROR, "unexpected EOF on self-pipe"); elog(ERROR, "unexpected EOF on self-pipe");
#else #else
elog(ERROR, "unexpected EOF on signalfd"); elog(ERROR, "unexpected EOF on signalfd");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment