From af587edea1f687041a5eda4563884b7d52aa19a0 Mon Sep 17 00:00:00 2001 From: Onder Kalaci Date: Wed, 8 Apr 2020 17:39:07 +0200 Subject: [PATCH] Reorder commands as Marco noted: ``` This could throw an OOM. We should do: Create MultiConnection (can error) dlist_push_tail (can error) sharedCounterIncremented = true (cannot error) increment the shared memory counter (cannot error) any error after this will be handled correctly in the abort handler ``` --- .../connection/connection_management.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/backend/distributed/connection/connection_management.c b/src/backend/distributed/connection/connection_management.c index a9a701988..e5080d6d7 100644 --- a/src/backend/distributed/connection/connection_management.c +++ b/src/backend/distributed/connection/connection_management.c @@ -320,6 +320,14 @@ StartNodeUserDatabaseConnection(uint32 flags, const char *hostname, int32 port, } + /* + * Either no caching desired, or no pre-established, non-claimed, + * connection present. Initiate connection establishment. + */ + MultiConnection *connection = MemoryContextAllocZero(ConnectionContext, + sizeof(MultiConnection)); + dlist_push_tail(entry->connections, &connection->connectionNode); + if (flags & WAIT_FOR_CONNECTION) { WaitLoopForSharedConnection(hostname, port); @@ -351,18 +359,13 @@ StartNodeUserDatabaseConnection(uint32 flags, const char *hostname, int32 port, IncrementSharedConnectionCounter(hostname, port); } + /* - * Either no caching desired, or no pre-established, non-claimed, - * connection present. Initiate connection establishment. + * We've already incremented the counter above, so we should decrement + * when we're done with the connection. */ - MultiConnection *connection = MemoryContextAllocZero(ConnectionContext, - sizeof(MultiConnection)); - - /* we've already incremented the counter above */ connection->sharedCounterIncremented = true; - dlist_push_tail(entry->connections, &connection->connectionNode); - StartConnectionEstablishment(connection, &key); ResetShardPlacementAssociation(connection);