RndNoob has asked for the wisdom of the Perl Monks concerning the following question:

Hi there. Perl is completely new for me, anyways I got stuck with this error trying to do a simple script than must keep a connection established with a remote server. When Ping is called for the second time and Mysql server is off segmentation fault occurs.

this is the skeleton working with a local mysql server (same error):

#!/usr/bin/perl use DBI; use warnings; use strict; use Term::ReadKey; my $dsnL = "dbi:mysql:database=test;host=127.0.0.1;port=3306"; my $dbhLocal = DBI->connect($dsnL, 'root',''); $dbhLocal->{mysql_auto_reconnect} = 1; my $exit; ReadMode 4; for ( ; ; ) { if ($dbhLocal->ping()) { print "OK\n"; } else { print "FAILED\n"; } defined( $exit = ReadKey(-1) ) && $exit ne 'Q' ? last : sleep 5; } ReadMode 0; exit 0;

Output: root@debian1:/home/user# perl test.pl
OK
OK
OK
OK
Segmentation fault

Sometimes fails with *** Error in `perl': double free or corruption (!prev): 0x00000000020a89d0 ***

Someone have a idea what is happening or a way to tackle this problem?

perl -v This is perl 5, version 20, subversion 2 (v5.20.2) built for x86_64-linux-gnu-thread-multi

Replies are listed 'Best First'.
Re: DBD::mysql Ping segmentation fault
by Anonymous Monk on Jun 12, 2015 at 06:10 UTC
    increase trace level (DBI->trace(999999), strace), involve gnu debugger (gdb)

      Trace:

      >> ping DISPATCH (DBI::db=HASH(0xce2d58) rc1/1 @1 g2 ima405 + pid#3528) at test.pl line 20 -> ping for DBD::mysql::db (DBI::db=HASH(0xce2d58)~0xcf0178) thr#9 +a1010 <- ping= ( 1 ) [1 items] at test.pl line 20 OK >> ping DISPATCH (DBI::db=HASH(0xce2d58) rc1/1 @1 g2 ima405 + pid#3528) at test.pl line 20 -> ping for DBD::mysql::db (DBI::db=HASH(0xce2d58)~0xcf0178) thr#9 +a1010 <- ping= ( 1 ) [1 items] at test.pl line 20 OK >> ping DISPATCH (DBI::db=HASH(0xce2d58) rc1/1 @1 g2 ima405 + pid#3528) at test.pl line 20 -> ping for DBD::mysql::db (DBI::db=HASH(0xce2d58)~0xcf0178) thr#9 +a1010 <- ping= ( 1 ) [1 items] at test.pl line 20 OK >> ping DISPATCH (DBI::db=HASH(0xce2d58) rc1/1 @1 g2 ima405 + pid#3528) at test.pl line 20 -> ping for DBD::mysql::db (DBI::db=HASH(0xce2d58)~0xcf0178) thr#9 +a1010 imp_dbh->pmysql: d39230 my_login IMPSET but not ACTIVE so connect not skipped imp_dbh->my_login : dbname = test, uid = root, pwd = ,host = 127.0.0.1 +, port = 3306 imp_dbh->mysql_dr_connect: host = |127.0.0.1|, port = 3306, uid = root +, pwd = imp_dbh->bind_type_guessing: 0 imp_dbh->use_server_side_prepare: 0 imp_dbh->mysql_dr_connect: client_flags = 2 imp_dbh->mysql_dr_connect: <- --> do_error Can't connect to MySQL server on '127.0.0.1' (111) error 2003 recorded +: Can't connect to MySQL server on '127.0.0.1' (111) <-- do_error !! ERROR: 2003 'Can't connect to MySQL server on '127.0.0.1' (111) +' (err#0) <- ping= ( '' ) [1 items] at test.pl line 20 DBD::mysql::db ping failed: Can't connect to MySQL server on '127.0.0. +1' (111) at test.pl line 20. FAILED >> ping DISPATCH (DBI::db=HASH(0xce2d58) rc1/1 @1 g2 ima405 + pid#3528) at test.pl line 20 -> ping for DBD::mysql::db (DBI::db=HASH(0xce2d58)~0xcf0178) thr#9 +a1010 imp_dbh->pmysql: d39230 my_login IMPSET but not ACTIVE so connect not skipped imp_dbh->my_login : dbname = test, uid = root, pwd = ,host = 127.0.0.1 +, port = 3306 imp_dbh->mysql_dr_connect: host = |127.0.0.1|, port = 3306, uid = root +, pwd = imp_dbh->bind_type_guessing: 0 imp_dbh->use_server_side_prepare: 0 imp_dbh->mysql_dr_connect: client_flags = 2 imp_dbh->mysql_dr_connect: <-*** Error in `perl': double free or corru +ption (!prev): 0x0000000000d39230 *** Aborted

      Seems that when Ping is called from connection established by itself and this connection failed, memory is not handled properly inside mysql_dr_connect. Maybe i must just check if Ping returned a valid conection before calling it again.

      Anyways using DBI->connect_cached instead of Ping solved my problem.

      although I still curious.