Skip Menu |

This queue is for tickets about the Test-postgresql CPAN distribution.

Report information
The Basics
Id: 70835
Status: resolved
Priority: 0/
Queue: Test-postgresql

People
Owner: TJC [...] cpan.org
Requestors: TJC [...] cpan.org
toby.corkindale [...] strategicdata.com.au
Cc:
AdminCc:

Bug Information
Severity: Important
Broken in: 0.09
Fixed in: 1.00



Subject: PATCH - Avoid deadlocks during shutdown
In some cases we have observed a race condition where PostgreSQL still has connections open, and waits for them to finish before exiting.. However because Test::postgresql is waiting for Pg to die, the test never gets to close the connection.. This patch alters things so that it only waits for a few seconds for Pg to die, before SIGKILLing it, then just waiting a little longer, before giving up and exiting anyway. Source is available at: http://gh/tobyc/Test-postgresql/tree/die_harder Patch against version 0.09 is attached.
Subject: 0001-Avoid-deadlocks-during-shutdown-by-SIGKILLing-Pg.patch
From a7dc8aefbb2900c5d3097875f8c172d65d33bcba Mon Sep 17 00:00:00 2001 From: Toby Corkindale <tobyc@strategicdata.com.au> Date: Fri, 9 Sep 2011 17:14:41 +1000 Subject: [PATCH] Avoid deadlocks during shutdown by SIGKILLing Pg In some cases we have observed a race condition where PostgreSQL still has connections open, and waits for them to finish before exiting.. However because Test::postgresql is waiting for Pg to die, the test never gets to close the connection.. This commit only waits for a few seconds for Pg to die, before SIGKILLing it, then just waiting a little longer, before giving up and exiting anyway. --- Changes | 3 +++ Makefile.PL | 1 + lib/Test/postgresql.pm | 29 ++++++++++++++++++++++++----- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/Changes b/Changes index 129e4dc..68686ef 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,8 @@ Revision history for Perl extension Test::postgresql. +0.10 xxxxxxxxxxxxxxxxxxxxxxx + - Avoid deadlocks during Postgres shutdown. + 0.09 Fri Oct 23 16:50:00 2009 - change cwd and directory permissions, modes for better testing diff --git a/Makefile.PL b/Makefile.PL index 54a8141..d9a3d84 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -5,6 +5,7 @@ name 'Test-postgresql'; all_from 'lib/Test/postgresql.pm'; requires 'Class::Accessor::Lite'; +requires 'Time::HiRes'; test_requires 'DBI'; test_requires 'DBD::Pg'; test_requires 'Test::SharedFork' => 0.06; diff --git a/lib/Test/postgresql.pm b/lib/Test/postgresql.pm index f97e8cb..43c0dbb 100644 --- a/lib/Test/postgresql.pm +++ b/lib/Test/postgresql.pm @@ -8,9 +8,10 @@ use Class::Accessor::Lite; use Cwd; use DBI; use File::Temp qw(tempdir); -use POSIX qw(SIGTERM WNOHANG setuid); +use Time::HiRes qw(nanosleep); +use POSIX qw(SIGTERM SIGKILL WNOHANG setuid); -our $VERSION = '0.09'; +our $VERSION = '0.10'; our @SEARCH_PATHS = ( # popular installtion dir? @@ -85,6 +86,7 @@ sub DESTROY { my $self = shift; $self->stop if defined $self->pid && $$ == $self->_owner_pid; + return; } sub dsn { @@ -181,13 +183,30 @@ sub _try_start { sub stop { my ($self, $sig) = @_; - return - unless defined $self->pid; + return unless defined $self->pid; + $sig ||= SIGTERM; + kill $sig, $self->pid; - while (waitpid($self->pid, 0) <= 0) { + my $timeout = 10 * 1000000000; + while ($timeout > 0 and waitpid($self->pid, WNOHANG) <= 0) { + $timeout -= nanosleep(500000000); + } + + if ($timeout <= 0) { + warn "Pg refused to die gracefully; killing it violently.\n"; + kill SIGKILL, $self->pid; + $timeout = 5 * 1000000000; + while ($timeout > 0 and waitpid($self->pid, WNOHANG) <= 0) { + $timeout -= nanosleep(500000000); + } + if ($timeout <= 0) { + warn "Pg really didn't die.. WTF?\n"; + } } + $self->pid(undef); + return; } sub setup { -- 1.7.6
Here's a patch that adds a test, too.
Subject: 0002-Add-test-for-deadlocks.patch
From 6edf8cfc5dacf4e4481277c5d5650a0b93f79aa2 Mon Sep 17 00:00:00 2001 From: Toby Corkindale <tobyc@strategicdata.com.au> Date: Tue, 13 Sep 2011 15:34:36 +1000 Subject: [PATCH 2/2] Add test for deadlocks --- t/deadlock.t | 23 +++++++++++++++++++++++ 1 files changed, 23 insertions(+), 0 deletions(-) create mode 100644 t/deadlock.t diff --git a/t/deadlock.t b/t/deadlock.t new file mode 100644 index 0000000..577d826 --- /dev/null +++ b/t/deadlock.t @@ -0,0 +1,23 @@ +#!perl +use strict; +use warnings; +use Test::postgresql; +use Test::More tests => 3; + +my $pgsql = Test::postgresql->new; + +my $dbh = DBI->connect($pgsql->dsn); +ok($dbh, "Connected to created database."); + +my $t1 = time(); + +$pgsql->stop; + +my $elapsed = time() - $t1; +diag("elapsed: $elapsed"); + +ok(1, "Reached point after calling stop()"); + +ok($elapsed <= 12, "Shutdown took less than 12 seconds."); + +1; -- 1.7.6
Also I must apologise that the previous source repository link went to a private instance that you couldn't access. I've pushed it to Github now, at: https://github.com/TJC/Test-postgresql/tree/die_harder
From: MENDEL [...] cpan.org
On Tue Sep 13 01:39:26 2011, TJC wrote: Show quoted text
> I've pushed it to Github now, at: > > https://github.com/TJC/Test-postgresql/tree/die_harder
I can also confirm that this patch fixes the bug (and that the bug exists). Could you please apply it?
On Thu Jun 21 08:20:38 2012, MENDEL wrote: Show quoted text
> On Tue Sep 13 01:39:26 2011, TJC wrote: >
> > I've pushed it to Github now, at: > > > > https://github.com/TJC/Test-postgresql/tree/die_harder
> > I can also confirm that this patch fixes the bug (and that the bug
exists). Show quoted text
> > Could you please apply it? >
It's been nearly three years since I filed this bug.. I think I'll release my own Test::PostgreSQL module that forks this one and includes the fix.
Hi, I've uploaded Test::PostgreSQL 0.10 to CPAN now. (Note change of case in name) -Toby
From: MENDEL [...] cpan.org
On Fri Jun 22 01:52:46 2012, TJC wrote: Show quoted text
> I've uploaded Test::PostgreSQL 0.10 to CPAN now. > > (Note change of case in name)
Well, the downside is that all the modules built on Test::postgresql still use the old, buggy one.. Also the module author seems to be active, maybe he just does not receive the notifications from RT. I'll try to contact him via email.
On Mon Jun 25 08:08:11 2012, MENDEL wrote: Show quoted text
> On Fri Jun 22 01:52:46 2012, TJC wrote: >
> > I've uploaded Test::PostgreSQL 0.10 to CPAN now. > > > > (Note change of case in name)
> > Well, the downside is that all the modules built on Test::postgresql > still use the old, buggy one.. Also the module author seems to be > active, maybe he just does not receive the notifications from RT. I'll > try to contact him via email. >
Did you have any luck? Two years have gone by. I've seen another person ask for COMAINT access for test-postgresql on github a couple of years back, and no responses were given. I agree, author does appear to be active on github though, so I don't know why they ignore RT and so forth.
Closing as fixed in Test::PostgreSQL (note change in case).