Skip to content

Commit d8548b0

Browse files
committed
Add _finish_when_safe to work around DBD::Pg race condition
1 parent 0cc90ab commit d8548b0

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

lib/Mojo/Pg/Database.pm

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ sub query {
107107

108108
# Non-blocking
109109
$self->{waiting} = {cb => $cb, sth => $sth};
110+
$self->{finish} = [];
110111
$self->_watch;
111112
}
112113

@@ -134,6 +135,12 @@ sub unlisten {
134135
return $self;
135136
}
136137

138+
sub _finish_when_safe {
139+
my $self = shift;
140+
if ($self->{finish}) { push @{$self->{finish}}, @_ }
141+
else { $_->finish for @_ }
142+
}
143+
137144
sub _notifications {
138145
my $self = shift;
139146

@@ -178,6 +185,7 @@ sub _watch {
178185
my $err = defined $result ? undef : $dbh->errstr;
179186

180187
$self->$cb($err, $self->results_class->new(db => $self, sth => $sth));
188+
$self->_finish_when_safe(@{delete $self->{finish}}) if $self->{finish};
181189
$self->_unwatch unless $self->{waiting} || $self->is_listening;
182190
}
183191
)->watch($self->{handle}, 1, 0);

lib/Mojo/Pg/Results.pm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ has [qw(db sth)];
1010
sub DESTROY {
1111
my $self = shift;
1212
return unless my $sth = $self->{sth};
13-
$sth->finish unless --$sth->{private_mojo_results};
13+
$self->finish unless --$sth->{private_mojo_results};
1414
}
1515

1616
sub array { ($_[0]->_expand($_[0]->sth->fetchrow_arrayref))[0] }
@@ -23,7 +23,7 @@ sub hash { ($_[0]->_expand($_[0]->sth->fetchrow_hashref))[0] }
2323

2424
sub expand { ++$_[0]{expand} and return $_[0] }
2525

26-
sub finish { shift->sth->finish }
26+
sub finish { $_[0]->db->_finish_when_safe($_[0]->sth) }
2727

2828
sub hashes { _collect($_[0]->_expand(@{$_[0]->sth->fetchall_arrayref({})})) }
2929

0 commit comments

Comments
 (0)