diff --git a/src/backend/port/unix_latch.c b/src/backend/port/unix_latch.c
index 147e22cee4eee16e9b51b39476d8128e14927365..90ec4f81d9e2bddb5670447b7eed553108c14d3b 100644
--- a/src/backend/port/unix_latch.c
+++ b/src/backend/port/unix_latch.c
@@ -460,7 +460,8 @@ WaitLatchOrSocket(volatile Latch *latch, int wakeEvents, pgsocket sock,
 				result |= WL_SOCKET_WRITEABLE;
 			}
 			if ((wakeEvents & WL_POSTMASTER_DEATH) &&
-			FD_ISSET(postmaster_alive_fds[POSTMASTER_FD_WATCH], &input_mask))
+				FD_ISSET(postmaster_alive_fds[POSTMASTER_FD_WATCH],
+						 &input_mask))
 			{
 				/*
 				 * According to the select(2) man page on Linux, select(2) may
@@ -479,17 +480,22 @@ WaitLatchOrSocket(volatile Latch *latch, int wakeEvents, pgsocket sock,
 #endif   /* HAVE_POLL */
 
 		/* If we're not done, update cur_timeout for next iteration */
-		if (result == 0 && cur_timeout >= 0)
+		if (result == 0 && (wakeEvents & WL_TIMEOUT))
 		{
 			INSTR_TIME_SET_CURRENT(cur_time);
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
-			if (cur_timeout < 0)
-				cur_timeout = 0;
-
+			if (cur_timeout <= 0)
+			{
+				/* Timeout has expired, no need to continue looping */
+				result |= WL_TIMEOUT;
+			}
 #ifndef HAVE_POLL
-			tv.tv_sec = cur_timeout / 1000L;
-			tv.tv_usec = (cur_timeout % 1000L) * 1000L;
+			else
+			{
+				tv.tv_sec = cur_timeout / 1000L;
+				tv.tv_usec = (cur_timeout % 1000L) * 1000L;
+			}
 #endif
 		}
 	} while (result == 0);
diff --git a/src/backend/port/win32_latch.c b/src/backend/port/win32_latch.c
index ee9526245fd48bcc29ce871bb4c37cd8e2a5884a..0e3aaeec69eae9909ac115d8442c83d4c87cc390 100644
--- a/src/backend/port/win32_latch.c
+++ b/src/backend/port/win32_latch.c
@@ -265,13 +265,16 @@ WaitLatchOrSocket(volatile Latch *latch, int wakeEvents, pgsocket sock,
 			elog(ERROR, "unexpected return code from WaitForMultipleObjects(): %lu", rc);
 
 		/* If we're not done, update cur_timeout for next iteration */
-		if (result == 0 && cur_timeout != INFINITE)
+		if (result == 0 && (wakeEvents & WL_TIMEOUT))
 		{
 			INSTR_TIME_SET_CURRENT(cur_time);
 			INSTR_TIME_SUBTRACT(cur_time, start_time);
 			cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
-			if (cur_timeout < 0)
-				cur_timeout = 0;
+			if (cur_timeout <= 0)
+			{
+				/* Timeout has expired, no need to continue looping */
+				result |= WL_TIMEOUT;
+			}
 		}
 	} while (result == 0);