Mojo::Pg and migrations (strage behavior)

112 views
Skip to first unread message

Andrey Khozov

unread,
Dec 20, 2014, 9:13:42 AM12/20/14
to mojol...@googlegroups.com
I have the following code:

use Mojo::Pg;
use Mojolicious::Lite;
use Test::Mojo;
use Test::More;

helper pg => sub { state $pg = Mojo::Pg->new('postgresql://user:te...@127.0.0.1/test') };

app->pg->migrations->migrate;
get '/test' => sub {
  my $c = shift;
  $c->render_later;

  $c->delay(
    sub {
      my $delay = shift;
      $c->pg->db->query('insert into test (ts) values (now())' => $delay->begin);
    },
    sub {
      my ($delay, $e, $res) = @_;
      return $c->render_exception("Error while insert: $e") if $e;
      $c->render(text => "Ok\n");
    }
  );
};
app->start;

my $t = Test::Mojo->new;
$t->get_ok('/test')->content_is("Ok\n");
$t->get_ok('/test')->content_is("Ok\n");
done_testing();

And I have strange behavior (timeout for curl and blocked ioloop for morbo) with second query to server.
If I remove the migration line, everything works well.​ Tests also work well.

I tried to use different postgres database (9.3 and 9.4RC), perl (5.18 and 5.20) and last version of Mojo::Pg.

Can someone confirm this behavior?

--
Andrey Khozov

sri

unread,
Dec 20, 2014, 12:26:52 PM12/20/14
to mojol...@googlegroups.com
Can someone confirm this behavior?

Andrey Khozov

unread,
Dec 20, 2014, 12:48:12 PM12/20/14
to mojol...@googlegroups.com
I think that for reproduce a bug you need to make a new connection to the server and perform at least two request.


--
Andrey Khozov

sri

unread,
Dec 20, 2014, 7:33:39 PM12/20/14
to mojol...@googlegroups.com
I think that for reproduce a bug you need to make a new connection to the server and perform at least two request.


I can confirm the problem, and i suspect it might be a DBD::Pg bug. I have traced the problem to a $dbh->ping call, which appears to just block forever.


--
sebastian 

sri

unread,
Dec 20, 2014, 8:08:19 PM12/20/14
to mojol...@googlegroups.com
I have traced the problem to a $dbh->ping call, which appears to just block forever.


There appears to be no connection to migrations though, i can replicate the problem with and without them. I have no idea why $ua->max_connections(0) matters though, that part is very strange.

--
sebastian 

sri

unread,
Dec 21, 2014, 1:15:15 AM12/21/14
to mojol...@googlegroups.com
That was a really fun bug to hunt! And it should be fixed now.


Turns out DBD::Pg is innocent after all, and it was just a little bit of Perl magic. We wrap the DBD::Pg file descriptor into a IO::Handle object, to be able to use it with Mojo::IOLoop.


But when the last reference to this wrapper is lost, Perl automatically closes the underlying file descriptor. (Although $dbh->ping blocking forever might still be a bug, i would expect for it to be able to detect the closed file descriptor)

--
sebastian

Andrey Khozov

unread,
Dec 21, 2014, 4:58:35 AM12/21/14
to mojol...@googlegroups.com
Great! Thanks!
Reply all
Reply to author
Forward
0 new messages