mojolicious / mojo-pg

Mojolicious :heart: PostgreSQL
https://metacpan.org/release/Mojo-Pg
Artistic License 2.0
101 stars 46 forks source link

Reusing dbh when sth is still exists #36

Closed kak-tus closed 7 years ago

kak-tus commented 7 years ago

Steps to reproduce the behavior

When we using async operations, and got db and results object - we can pass result object to subfunction. After that result object continues to exists, and db - not, so DESTROY on db called and dbh returns to queue. See example code, that reproduce this issue. Code not perfect (normally it's not a good idea to pass results object to subfunction, but it is exists).

my $cv = AE::cv;

$pg->db->query(
  'SELECT 1;',
  sub {
    my ( $db, $error, $results ) = @_;

    Mojo::IOLoop->timer(
      1 => sub {
        ## Here sth still exists
        _query2( $results->array->[0] );
      }
    );

    ## After this $db not exists already, but sth in results still exists
    ## Called DESTROY to $db and dbh pushed to queue
  }
);

sub _query2 {
  ## Here sth still exists and we get dbh from previous query
  $pg->db->query(
    'SELECT 2;',
    sub {
      my ( $db, $error, $results ) = @_;
      say $results->array->[0];
      $cv->send;
    }
  );
}

$cv->recv;

Expected behavior

We coud got normal result from second query.

Actual behavior

We got error

DBD::Pg::st fetchrow_arrayref failed: no statement executing at /usr/share/perl5/Mojo/Pg/Results.pm line 16.
Mojo::Reactor::EV: I/O watcher failed: DBD::Pg::st fetchrow_arrayref failed: no statement executing at /usr/share/perl5/Mojo/Pg/Results.pm line 16.
jberger commented 7 years ago

I guess I don't fully understand your issue (I don't have time to reproduce at the moment) but it sounds like perhaps you should look at https://metacpan.org/pod/Mojo::Pg::Results#finish .

kak-tus commented 7 years ago

Problem here is not in non used usual functions (yes, finish will help here). Problem is in using results object in closure function, which is not a good idea, but user can do this. In this case - we have got dbh marked as free, and not finished sth.

kraih commented 7 years ago

Afraid i don't understand the problem either, and i don't think we care too much about AnyEvent specific problems.