diff --git a/src/backend/replication/walsender.c b/src/backend/replication/walsender.c
index 5b5592aeba7..ba783694553 100644
--- a/src/backend/replication/walsender.c
+++ b/src/backend/replication/walsender.c
@@ -1992,6 +1992,16 @@ WalSndWaitForWal(XLogRecPtr loc)
 		/* die if timeout was reached */
 		WalSndCheckTimeOut();
 
+		/*
+		 * If a shutdown is in progress and wal_sender_shutdown_timeout has
+		 * expired, terminate the walsender immediately rather than waiting
+		 * indefinitely for the standby to catch up.  This is needed because
+		 * when synchronized_standby_slots is configured and the physical
+		 * standby is not responding, the logical walsender can get stuck in
+		 * this loop waiting for NeedToWaitForStandbys() to return false.
+		 */
+		WalSndCheckShutdownTimeout();
+
 		/* Send keepalive if the time has come */
 		WalSndKeepaliveIfNecessary();
 
