From 53e1f4ac0027649ae3445898552cc9c8aa2cc8fd Mon Sep 17 00:00:00 2001
From: Sehrope Sarkuni <sehrope@jackdb.com>
Date: Wed, 10 Jun 2026 09:00:44 -0400
Subject: [PATCH v2 4/4] Do not treat a failed wait as a timeout in win32
 pg_usleep

Only WAIT_OBJECT_0 was distinguished, so WAIT_FAILED silently turned
the sleep into an immediate return. Callers using pg_usleep in retry
loops would then spin at full speed if the event handle were ever
bad.
---
 src/backend/port/win32/signal.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/src/backend/port/win32/signal.c b/src/backend/port/win32/signal.c
index 1ef0ca999e1..66a05d966aa 100644
--- a/src/backend/port/win32/signal.c
+++ b/src/backend/port/win32/signal.c
@@ -63,13 +63,18 @@ pg_usleep(long microsec)
 		return;
 	}
 
-	if (WaitForSingleObject(pgwin32_signal_event,
-							(microsec < 500 ? 1 : (microsec + 500) / 1000))
-		== WAIT_OBJECT_0)
+	switch (WaitForSingleObject(pgwin32_signal_event,
+								(microsec < 500 ? 1 : (microsec + 500) / 1000)))
 	{
-		pgwin32_dispatch_queued_signals();
-		errno = EINTR;
-		return;
+		case WAIT_OBJECT_0:
+			pgwin32_dispatch_queued_signals();
+			errno = EINTR;
+			return;
+		case WAIT_TIMEOUT:
+			return;
+		default:
+			elog(FATAL, "could not wait on signal event: error code %lu",
+				 GetLastError());
 	}
 }
 
-- 
2.17.1

