diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c index b7e5129..8d14e6b 100644 --- a/src/backend/storage/ipc/latch.c +++ b/src/backend/storage/ipc/latch.c @@ -643,6 +643,7 @@ AddWaitEventToSet(WaitEventSet *set, uint32 events, pgsocket fd, Latch *latch, event->fd = fd; event->events = events; event->user_data = user_data; + event->reset = false; if (events == WL_LATCH_SET) { @@ -689,13 +690,14 @@ ModifyWaitEvent(WaitEventSet *set, int pos, uint32 events, Latch *latch) event = &set->events[pos]; /* - * If neither the event mask nor the associated latch changes, return - * early. That's an important optimization for some sockets, where - * ModifyWaitEvent is frequently used to switch from waiting for reads to - * waiting on writes. + * If one of the event mask or the associated latch changes or event reset + * is requested, then modify the event, otherwise return early. That's an + * important optimization for some sockets, where ModifyWaitEvent is + * frequently used to switch from waiting for reads to waiting on writes. */ if (events == event->events && - (!(event->events & WL_LATCH_SET) || set->latch == latch)) + (!(event->events & WL_LATCH_SET) || set->latch == latch) && + !event->reset) return; if (event->events & WL_LATCH_SET && @@ -727,6 +729,8 @@ ModifyWaitEvent(WaitEventSet *set, int pos, uint32 events, Latch *latch) #elif defined(WAIT_USE_WIN32) WaitEventAdjustWin32(set, event); #endif + + event->reset = false; } #if defined(WAIT_USE_EPOLL) @@ -1472,6 +1476,16 @@ WaitEventSetWaitBlock(WaitEventSet *set, int cur_timeout, { /* data available in socket */ occurred_events->events |= WL_SOCKET_READABLE; + /* + * We need to re-enable the setting of the event to ensure that it + * will be notified on next call. This is required as per msdn + * https://msdn.microsoft.com/en-us/library/windows/desktop/ms741576(v=vs.85).aspx. + * It is not clear whether we need this for any other events, + * however for FD_READ it is required, otherwise it won't recognize + * it and keeps on waiting indefinitely if the first event + * triggered is other than FD_READ. + */ + cur_event->reset = true; } if ((cur_event->events & WL_SOCKET_WRITEABLE) && (resEvents.lNetworkEvents & FD_WRITE)) diff --git a/src/include/storage/latch.h b/src/include/storage/latch.h index e96e88f..971b646 100644 --- a/src/include/storage/latch.h +++ b/src/include/storage/latch.h @@ -133,6 +133,7 @@ typedef struct WaitEvent uint32 events; /* triggered events */ pgsocket fd; /* socket fd associated with event */ void *user_data; /* pointer provided in AddWaitEventToSet */ + bool reset; /* is reset of event required */ } WaitEvent; /* forward declaration to avoid exposing latch.c implementation details */