Avoid the "key conflict attempting to insert a user row" exception in some cases
authorAaron Schulz <aschulz@wikimedia.org>
Wed, 19 Jun 2013 21:13:19 +0000 (14:13 -0700)
committerAaron Schulz <aschulz@wikimedia.org>
Tue, 25 Jun 2013 22:23:42 +0000 (15:23 -0700)
bug: 41609
Change-Id: Iebfcf50e648a43f2970e6d0e4073cb6d84d260df

includes/User.php

index 1c13211..b6d2377 100644 (file)
@@ -3272,6 +3272,7 @@ class User {
                $this->mTouched = self::newTouchedTimestamp();
 
                $dbw = wfGetDB( DB_MASTER );
+               $inWrite = $dbw->writesOrCallbacksPending();
                $seqVal = $dbw->nextSequenceValue( 'user_user_id_seq' );
                $dbw->insert( 'user',
                        array(
@@ -3291,6 +3292,12 @@ class User {
                        array( 'IGNORE' )
                );
                if ( !$dbw->affectedRows() ) {
+                       if ( !$inWrite ) {
+                               // XXX: Get out of REPEATABLE-READ so the SELECT below works.
+                               // Often this case happens early in views before any writes.
+                               // This shows up at least with CentralAuth.
+                               $dbw->commit( __METHOD__, 'flush' );
+                       }
                        $this->mId = $dbw->selectField( 'user', 'user_id',
                                array( 'user_name' => $this->mName ), __METHOD__ );
                        $loaded = false;