diff --git a/src/backend/distributed/connection/remote_commands.c b/src/backend/distributed/connection/remote_commands.c index a09aa877c..00bd0de81 100644 --- a/src/backend/distributed/connection/remote_commands.c +++ b/src/backend/distributed/connection/remote_commands.c @@ -781,6 +781,7 @@ WaitForAllConnections(List *connectionList, bool raiseInterrupts) while (pendingConnectionsStartIndex < totalConnectionCount) { + bool cancellationReceived = false; int eventIndex = 0; int eventCount = 0; long timeout = -1; @@ -837,9 +838,13 @@ WaitForAllConnections(List *connectionList, bool raiseInterrupts) if (InterruptHoldoffCount > 0 && (QueryCancelPending || ProcDiePending)) { - /* return immediately in case of cancellation */ - FreeWaitEventSet(waitEventSet); - return; + /* + * Break out of event loop immediately in case of cancellation. + * We cannot use "return" here inside a PG_TRY() block since + * then the exception stack won't be reset. + */ + cancellationReceived = true; + break; } continue; @@ -905,6 +910,11 @@ WaitForAllConnections(List *connectionList, bool raiseInterrupts) } } + if (cancellationReceived) + { + break; + } + /* move non-ready connections to the back of the array */ for (connectionIndex = pendingConnectionsStartIndex; connectionIndex < totalConnectionCount; connectionIndex++)