Skip to content

Commit 65d318c

Browse files
committed
fix postgresql: avoid memory leak during CleanupConnection
add try-catch to handle exception from Connection::Cleanup
1 parent d0eb633 commit 65d318c

File tree

1 file changed

+19
-20
lines changed
  • postgresql/src/storages/postgres/detail

1 file changed

+19
-20
lines changed

postgresql/src/storages/postgres/detail/pool.cpp

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -563,34 +563,33 @@ void ConnectionPool::Clear() {
563563
}
564564

565565
void ConnectionPool::CleanupConnection(Connection* connection) {
566-
if (cancel_limit_.Obtain()) {
567-
try {
566+
try {
567+
if (cancel_limit_.Obtain()) {
568568
connection->CancelAndCleanup(kCleanupTimeout);
569569
if (connection->IsIdle()) {
570570
LOG_DEBUG() << "Successfully cleaned up a dirty connection";
571571
AccountConnectionStats(connection->GetStatsAndReset());
572572
Push(connection);
573573
return;
574574
}
575-
} catch (const std::exception& e) {
576-
LOG_WARNING() << "Exception while cleaning up a dirty connection: " << e;
577-
connection->MarkAsBroken();
578-
}
579-
} else {
580-
// Too many connections are cancelling ATM, we cannot afford running
581-
// many synchronous calls and/or keep precious connections hanging.
582-
// Assume a router with sane connection management logic is in place.
583-
if (connection->Cleanup(kCleanupTimeout)) {
584-
LOG_DEBUG() << "Successfully finished waiting for a dirty connection "
585-
"to clean up itself";
586-
AccountConnectionStats(connection->GetStatsAndReset());
587-
Push(connection);
588-
return;
589-
}
590-
if (!connection->IsConnected()) {
591-
DeleteBrokenConnection(connection);
592-
return;
575+
} else {
576+
// Too many connections are cancelling ATM, we cannot afford running
577+
// many synchronous calls and/or keep precious connections hanging.
578+
// Assume a router with sane connection management logic is in place.
579+
if (connection->Cleanup(kCleanupTimeout)) {
580+
LOG_DEBUG() << "Successfully finished waiting for a dirty connection "
581+
"to clean up itself";
582+
AccountConnectionStats(connection->GetStatsAndReset());
583+
Push(connection);
584+
return;
585+
}
586+
if (!connection->IsConnected()) {
587+
DeleteBrokenConnection(connection);
588+
return;
589+
}
593590
}
591+
} catch (const std::exception& e) {
592+
LOG_WARNING() << "Exception while cleaning up a dirty connection: " << e;
594593
}
595594
LOG_WARNING() << "Failed to cleanup a dirty connection, deleting...";
596595
++stats_.connection.error_total;

0 commit comments

Comments
 (0)