jenkins-bot has submitted this change and it was merged.

Change subject: Improve Database::__destruct() and add a __clone() method too
......................................................................


Improve Database::__destruct() and add a __clone() method too

* Close dangling connections in LoadBalancer/Database destructors.
* When DBs are cloned, create new connection resources for the clone
  so the two do Database objects don't clobber each other.

Change-Id: I3adb57cbb1fdc2a17e6d95389d0562ef22701576
---
M includes/libs/rdbms/database/Database.php
M includes/libs/rdbms/loadbalancer/LoadBalancer.php
2 files changed, 33 insertions(+), 1 deletion(-)

Approvals:
  Gergő Tisza: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/includes/libs/rdbms/database/Database.php 
b/includes/libs/rdbms/database/Database.php
index 9b4e4ac..33e36b5 100644
--- a/includes/libs/rdbms/database/Database.php
+++ b/includes/libs/rdbms/database/Database.php
@@ -3488,6 +3488,26 @@
        }
 
        /**
+        * Make sure that copies do not share the same client binding handle
+        * @throws DBConnectionError
+        */
+       public function __clone() {
+               $this->connLogger->debug(
+                       "Cloning " . get_class( $this ) . " is not recomended; 
forking connection:\n" .
+                       ( new RuntimeException() )->getTraceAsString()
+               );
+
+               if ( $this->isOpen() ) {
+                       // Open a new connection resource without messing with 
the old one
+                       $this->mOpened = false;
+                       $this->mConn = false;
+                       $this->mTrxLevel = 0; // no trx anymore
+                       $this->open( $this->mServer, $this->mUser, 
$this->mPassword, $this->mDBname );
+                       $this->lastPing = microtime( true );
+               }
+       }
+
+       /**
         * Called by serialize. Throw an exception when DB connection is 
serialized.
         * This causes problems on some database engines because the connection 
is
         * not restored on unserialize.
@@ -3498,7 +3518,7 @@
        }
 
        /**
-        * Run a few simple sanity checks
+        * Run a few simple sanity checks and close dangling connections
         */
        public function __destruct() {
                if ( $this->mTrxLevel && $this->mTrxDoneWrites ) {
@@ -3510,5 +3530,12 @@
                        $fnames = implode( ', ', $danglingWriters );
                        trigger_error( "DB transaction writes or callbacks 
still pending ($fnames)." );
                }
+
+               if ( $this->mConn ) {
+                       // Avoid connection leaks for sanity
+                       $this->closeConnection();
+                       $this->mConn = false;
+                       $this->mOpened = false;
+               }
        }
 }
diff --git a/includes/libs/rdbms/loadbalancer/LoadBalancer.php 
b/includes/libs/rdbms/loadbalancer/LoadBalancer.php
index 791e5ad..c78f0ae 100644
--- a/includes/libs/rdbms/loadbalancer/LoadBalancer.php
+++ b/includes/libs/rdbms/loadbalancer/LoadBalancer.php
@@ -1460,4 +1460,9 @@
                        $db->tablePrefix( $prefix );
                } );
        }
+
+       function __destruct() {
+               // Avoid connection leaks for sanity
+               $this->closeAll();
+       }
 }

-- 
To view, visit https://gerrit.wikimedia.org/r/311734
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I3adb57cbb1fdc2a17e6d95389d0562ef22701576
Gerrit-PatchSet: 9
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Aaron Schulz <asch...@wikimedia.org>
Gerrit-Reviewer: Gergő Tisza <gti...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to