From: Dylan William Hardison Date: Tue, 16 Oct 2018 17:51:33 +0000 (-0400) Subject: Bug 1499262 - Bugzilla::DB should gracefully handle disconnection events that happen... X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=577c19aad3404c0caed9236712cf18aa338b7e0b;p=thirdparty%2Fbugzilla.git Bug 1499262 - Bugzilla::DB should gracefully handle disconnection events that happen during transactions --- diff --git a/Bugzilla/DB.pm b/Bugzilla/DB.pm index 87110aaaa..bcd455bae 100644 --- a/Bugzilla/DB.pm +++ b/Bugzilla/DB.pm @@ -1280,8 +1280,9 @@ sub bz_rollback_transaction { if (!$self->bz_in_transaction) { ThrowCodeError("not_in_transaction"); } else { - $self->rollback(); $self->{private_bz_transaction_count} = 0; + # the next line may fail if somehow we're connected but not in a txn + $self->rollback() if $self->connector->connected; } } diff --git a/Bugzilla/Test/MockDB.pm b/Bugzilla/Test/MockDB.pm index fb7873ccf..84e4a3bf3 100644 --- a/Bugzilla/Test/MockDB.pm +++ b/Bugzilla/Test/MockDB.pm @@ -13,7 +13,7 @@ use Capture::Tiny qw(capture_merged); use Bugzilla::Test::MockLocalconfig ( db_driver => 'sqlite', - db_name => ':memory:', + db_name => $ENV{test_db_name} // ':memory:', ); use Bugzilla; BEGIN { Bugzilla->extensions }; diff --git a/t/db-error-cleanup.pl b/t/db-error-cleanup.pl new file mode 100644 index 000000000..7db336617 --- /dev/null +++ b/t/db-error-cleanup.pl @@ -0,0 +1,71 @@ +#!/usr/bin/perl +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. +# +# This Source Code Form is "Incompatible With Secondary Licenses", as +# defined by the Mozilla Public License, v. 2.0. +use strict; +use warnings; +use 5.10.1; +use lib qw( . lib local/lib/perl5 ); + +BEGIN { + $ENV{LOG4PERL_CONFIG_FILE} = 'log4perl-t.conf'; + $ENV{test_db_name} = 'db_errors'; +} + +END { unlink('data/db/db_errors') } + +use Bugzilla::Test::MockLocalconfig (urlbase => 'http://bmo-web.vm' ); +use Bugzilla::Test::MockDB; +use Test2::V0; +use Test2::Tools::Exception qw(dies lives); +use Try::Tiny; +use Bugzilla::Test::Util qw(create_user); + +ok( + dies { + my $dbh = Bugzilla->dbh; + $dbh->bz_start_transaction; + create_user('example@user.org', '*'); + $dbh->dbh->disconnect; + $dbh->dbh; + }, + "connecting after a disconnect in a bz transaction is fatal" +) and note($@); + +ok(lives { Bugzilla->_cleanup }, "_cleanup") or note($@); + +ok(!Bugzilla->request_cache->{dbh}, "dbh should be gone after cleanup"); + +my $user = Bugzilla::User->new({name => 'example@user.org'}); +ok(!$user, 'user was not created'); + +ok( + dies { + my $dbh = Bugzilla->dbh; + $dbh->bz_start_transaction; + create_user('example@user.org', '*'); + $dbh->dbh->disconnect; + $dbh->bz_commit_transaction; + }, + "commit after a disconnect is fatal" +) and note($@); + +ok(lives { Bugzilla->_cleanup }, "_cleanup") or note($@); + +ok( + lives { + my $dbh = Bugzilla->dbh; + $dbh->bz_start_transaction; + create_user('example@user.org', '*'); + $dbh->dbh->disconnect; + Bugzilla->_cleanup; + $dbh->dbh; + }, + "calling _cleanup after bz_start_transaction results in a working connection", +) or note($@); + + +done_testing;