You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Alex Hunsaker <ba...@gmail.com> on 2008/03/13 02:40:38 UTC

[Apache::DBI] [PATCH] use PerlChildExitHandler to properly disconnect

This Patch fixes annoying log messages (notably with postgresql, but
others may apply) because Apache::DBI fails to disconnect properly at
exit time.

(it stops the buggers below from filling my logs...)
LOG:  unexpected EOF on client connection
LOG:  could not receive data from client: Connection reset by peer

Note the first hunk *might* be a bug fix as under MP2
Apache->can('push_handlers') returns false.
It does not look (unless im missing something) like the
PerlCleanupHandler for AutoCommit rollback ever got registered...

(Patch attached as well in case white space damage occurs)

applies cleanly against 1.05/1.06

 diff -x '*~' -ruN Apache-DBI-1.05.orig/lib/Apache/DBI.pm
 Apache-DBI-1.05/lib/Apache/DBI.pm
 --- Apache-DBI-1.05.orig/lib/Apache/DBI.pm  2006-11-03 23:17:44.000000000 -0700
 +++ Apache-DBI-1.05/lib/Apache/DBI.pm   2008-03-12 17:38:37.455592048 -0600
 @@ -137,7 +137,7 @@
     # script has finished if AutoCommit is off.  however, cleanup can only
     # be determined at end of handle life as begin_work may have been called
     # to temporarily turn off AutoCommit.
 -    if (!$Rollback{$Idx} and Apache->can('push_handlers')) {
 +    if (!$Rollback{$Idx} and (Apache->can('push_handlers') || MP2)) {
         debug(2, "$prefix push PerlCleanupHandler");
         if (MP2) {
             my $s = Apache2::ServerUtil->server;
 @@ -151,6 +151,17 @@
         $Rollback{$Idx} = 1;
     }

 +    if (Apache->can('push_handlers') || MP2) {
 +        debug(2, "$prefix push PerlChildExitHandler");
 +        if (MP2) {
 +            my $s = Apache2::ServerUtil->server;
 +            $s->push_handlers("PerlChildExitHandler", sub {
 child_disconnect($Idx) });
 +        }
 +        else {
 +            Apache->push_handlers("PerlChildExitHandler", sub {
 child_disconnect($Idx) });
 +        }
 +    }
 +
     # do we need to ping the database ?
     $PingTimeOut{$dsn}  = 0 unless $PingTimeOut{$dsn};
     $LastPingTime{$dsn} = 0 unless $LastPingTime{$dsn};
 @@ -236,6 +247,26 @@
     1;
  }

 +sub child_disconnect {
 +    my $Idx = shift;
 +
 +    my $prefix = "$$ Apache::DBI            ";
 +    debug(2, "$prefix PerlChildExitHandler");
 +
 +    if ($Connected{$Idx}) {
 +        cleanup($Idx);
 +
 +        {
 +            package Apache::DBI::db;
 +            eval { $Connected{$Idx}->SUPER::disconnect(); };
 +            delete $Connected{$Idx}
 +        }
 +        debug(2, "$prefix PerlChildExitHandler disconnect for '$Idx'");
 +    }
 +
 +    1;
 +}
 +
  # Store the default start state of each dbh in the handle
  # Note: This uses private_Apache_DBI hash ref to store it in the handle itself
  my @attrs = qw(