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

Hello Monks, I have a (multi-threaded) perl script that is occassionally seg faulting. I seek your wisdom in nailing the offending part of the script.
Environment: OS: CentOS 6.4 Perl: v5.10.1 (*) built for x86_64-linux-thread-multi DBI: 1.623 DBD::mysql: 4.022 Threads: 1.72

Script overview: This is the master script that is terminating with a seg fault. It spawns a number of threads each invoking an external shell script using system() call (and these shell scripts in-turn invoke external programs).It also creates an extra detached thread that periodically connects to a remote database to monitor the status of a few external programs (that report to the DB).

When this script terminates, I see all the shells scripts and the programs they spawned (all all their offsprings) running ok. Its only this master script and the monitor thread that it creates that don't exist anymore (expected after a seg fault).

The issue gets complicated because this failure is not consistent across runs. It only happens occasionally. Here is a gdb backtrace of the core dump generated by the failure (I've changed the credential info for obvious reasons, but the trace is left as is).

Any pointers in the right direction?

Core was generated by `perl -d problem_script.pl 970 10.30.30.42 50,51 +,52,53,54,55,56,57,58,59'. Program terminated with signal 6, Aborted. #0 0x00000032fe6306f7 in kill () from /lib64/libc.so.6 (gdb) bt #0 0x00000032fe6306f7 in kill () from /lib64/libc.so.6 #1 0x00000000004d8367 in Perl_apply () #2 0x00000000004c99c9 in Perl_pp_chown () #3 0x000000000048765e in Perl_runops_standard () #4 0x0000000000430cb4 in Perl_call_sv () #5 0x0000000000478166 in Perl_sighandler () #6 <signal handler called> #7 my_stat (path=0x7fff24f5db80 "/usr/share/mysql/charsets/Index.xml" +, stat_area=0x7fff24f5da50, my_flags=0) at /export/home/pb2/build/sb_0-10180689-1378750483.08/rpm/BUILD/my +sql-5.5.34/mysql-5.5.34/mysys/my_lib.c:415 #8 0x00002ae284d8cd74 in my_read_charset_file (filename=0x2 <Address +0x2 out of bounds>, myflags=0) at /export/home/pb2/build/sb_0-10180689-1378750483.08/rpm/BUILD/my +sql-5.5.34/mysql-5.5.34/mysys/charset.c:364 #9 0x00002ae284d8d615 in init_available_charsets () at /export/home/p +b2/build/sb_0-10180689-1378750483.08/rpm/BUILD/mysql-5.5.34/mysql-5.5 +.34/mysys/charset.c:458 #10 0x00000032ff60c1d3 in pthread_once () from /lib64/libpthread.so.0 #11 0x00002ae284d8de48 in get_charset_by_csname (cs_name=0x199e2bf0 "l +atin1", cs_flags=32, flags=2034312912) at /export/home/pb2/build/sb_0-10180689-1378750483.08/rpm/BUILD/my +sql-5.5.34/mysql-5.5.34/mysys/charset.c:644 #12 0x00002ae284d7383f in mysql_set_character_set_with_default_collati +on (mysql=0x19a48f00) at /export/home/pb2/build/sb_0-10180689-1378750483.08/rpm/BUILD/my +sql-5.5.34/mysql-5.5.34/sql-common/client.c:2233 #13 mysql_init_character_set (mysql=0x19a48f00) at /export/home/pb2/bu +ild/sb_0-10180689-1378750483.08/rpm/BUILD/mysql-5.5.34/mysql-5.5.34/s +ql-common/client.c:2272 #14 0x00002ae284d7632d in mysql_real_connect (mysql=0x19a48f00, host=0 +x199e6b30 "10.30.30.73", user=0x19670e10 "<foo>", passwd=0x19670e30 " +<foobar>", db=0x199e6af0 "<bar>", port=3306, unix_socket=0x0, client_flag=131074) at /export/home/pb2/build/sb_ +0-10180689-1378750483.08/rpm/BUILD/mysql-5.5.34/mysql-5.5.34/sql-comm +on/client.c:3436 #15 0x00002ae284b2bfd5 in mysql_dr_connect (dbh=<value optimized out>, + sock=0x19a48f00, mysql_socket=0x0, host=0x199e6b30 "10.30.30.73", po +rt=0x14edc368 "\b\243\016\210\342*", user=<value optimized out>, password=<value optimized out>, dbname +=0x199e6af0 "<bar>", imp_dbh=0x193e98b0) at dbdimp.c:1892 #16 0x00002ae284b2d188 in my_login (my_perl=0x14772010, dbh=0x19696110 +, imp_dbh=0x193e98b0) at dbdimp.c:2004 #17 0x00002ae284b2e372 in mysql_db_login (dbh=0x19696110, imp_dbh=0x19 +3e98b0, dbname=0x2ae288132220 "<bar>:10.30.30.73:3306", user=0x2ae288 +132240 "<foo>", password=0x2ae288132260 "<foobar>") at dbdimp.c:2055 #18 0x00002ae284b35a77 in XS_DBD__mysql__db__login (my_perl=0x14772010 +, cv=<value optimized out>) at mysql.xsi:104 #19 0x000000000048906f in Perl_pp_entersub () #20 0x000000000048765e in Perl_runops_standard () #21 0x000000000043097f in Perl_call_sv () #22 0x00002ae27d64b5f1 in XS_DBI_dispatch () from /usr/local/lib/perl5 +/site_perl/5.10.1/x86_64-linux-thread-multi/auto/DBI/DBI.so #23 0x000000000048906f in Perl_pp_entersub () #24 0x000000000048765e in Perl_runops_standard () #25 0x00000000004314bc in perl_run () #26 0x000000000041d4ad in main () (gdb)

Of the above, line 8 and 15 seem suspicious to me, but I can use some wisdom here.

Replies are listed 'Best First'.
Re: Help debugging a random seg fault
by dave_the_m (Monsignor) on Jan 22, 2014 at 22:24 UTC
    Perl 5.10.1 is over 4 years old. I'd strongly advise you to try your code with a newer release; 5.18.2 is the latest maintenance release. (Along with the newest versions of DBI, DBD::MySQL).

    Dave.

      I, unfortunately, do not have much control over installing new versions, unless I can show that a bug exists in this version which was fixed later. This version was the default one provided by the OS. However, thank you for the suggestion. I'll try to get the message through.

        As a recommendation, I would suggest having your own, non-OS version of Perl as a dependency for your application. OS vendors do update perl occasionally, or you may find that you need to update something that the OS collides with. Having your own version of perl allows you to make the best decisions for your application.

        --MidLifeXis

Re: Help debugging a random seg fault
by sved (Novice) on Jan 22, 2014 at 22:10 UTC
    Here is the backtrace from another run (a different reason too, but similar backtrace).
    Core was generated by `/usr/bin/perl problem_script.pl 972 10.30.'. Program terminated with signal 11, Segmentation fault. #0 my_stat (path=0x7fffbbc78110 "/usr/share/mysql/charsets/Index.xml" +, stat_area=0x7fffbbc77fe0, my_flags=0) at /export/home/pb2/build/sb_0-10180689-1378750483.08/rpm/BUILD/my +sql-5.5.34/mysql-5.5.34/mysys/my_lib.c:415 415 /export/home/pb2/build/sb_0-10180689-1378750483.08/rpm/BUILD/m +ysql-5.5.34/mysql-5.5.34/mysys/my_lib.c: No such file or directory. in /export/home/pb2/build/sb_0-10180689-1378750483.08/rpm/BUIL +D/mysql-5.5.34/mysql-5.5.34/mysys/my_lib.c (gdb) bt #0 my_stat (path=0x7fffbbc78110 "/usr/share/mysql/charsets/Index.xml" +, stat_area=0x7fffbbc77fe0, my_flags=0) at /export/home/pb2/build/sb_0-10180689-1378750483.08/rpm/BUILD/my +sql-5.5.34/mysql-5.5.34/mysys/my_lib.c:415 #1 0x00002ae583491d74 in my_read_charset_file (filename=0x2 <Address +0x2 out of bounds>, myflags=0) at /export/home/pb2/build/sb_0-10180689-1378750483.08/rpm/BUILD/my +sql-5.5.34/mysql-5.5.34/mysys/charset.c:364 #2 0x00002ae583492615 in init_available_charsets () at /export/home/p +b2/build/sb_0-10180689-1378750483.08/rpm/BUILD/mysql-5.5.34/mysql-5.5 +.34/mysys/charset.c:458 #3 0x00000032ff60c1d3 in pthread_once () from /lib64/libpthread.so.0 #4 0x00002ae583492e48 in get_charset_by_csname (cs_name=0x177201e0 "l +atin1", cs_flags=32, flags=1571489488) at /export/home/pb2/build/sb_0-10180689-1378750483.08/rpm/BUILD/my +sql-5.5.34/mysql-5.5.34/mysys/charset.c:644 #5 0x00002ae58347883f in mysql_set_character_set_with_default_collati +on (mysql=0x1771fce0) at /export/home/pb2/build/sb_0-10180689-1378750483.08/rpm/BUILD/my +sql-5.5.34/mysql-5.5.34/sql-common/client.c:2233 #6 mysql_init_character_set (mysql=0x1771fce0) at /export/home/pb2/bu +ild/sb_0-10180689-1378750483.08/rpm/BUILD/mysql-5.5.34/mysql-5.5.34/s +ql-common/client.c:2272 #7 0x00002ae58347b32d in mysql_real_connect (mysql=0x1771fce0, host=0 +x1771b5a0 "10.30.30.73", user=0x178cd690 "<foo>", passwd=0x178cd6e0 " +<foobar>", db=0x1771b4d0 "<bar>", port=3306, unix_socket=0x0, client_flag=131074) at /export/home/pb2/build/sb_ +0-10180689-1378750483.08/rpm/BUILD/mysql-5.5.34/mysql-5.5.34/sql-comm +on/client.c:3436 #8 0x00002ae583230fd5 in mysql_dr_connect (dbh=<value optimized out>, + sock=0x1771fce0, mysql_socket=0x0, host=0x1771b5a0 "10.30.30.73", po +rt=0xe689e98 "x\265`\016", user=<value optimized out>, password=<value optimized out>, dbname=0x1771b4d0 "<bar>", imp_dbh +=0x1771f700) at dbdimp.c:1892 #9 0x00002ae583232188 in my_login (my_perl=0xe301010, dbh=0xe687c48, +imp_dbh=0x1771f700) at dbdimp.c:2004 #10 0x00002ae583233372 in mysql_db_login (dbh=0xe687c48, imp_dbh=0x177 +1f700, dbname=0x17720ae0 "<bar>:10.30.30.73:3306", user=0x1771b2f0 "< +foo>", password=0x178cd450 "<foobar>") at dbdimp.c:2055 #11 0x00002ae58323aa77 in XS_DBD__mysql__db__login (my_perl=0xe301010, + cv=<value optimized out>) at mysql.xsi:104 #12 0x000000000048906f in Perl_pp_entersub () #13 0x000000000048765e in Perl_runops_standard () #14 0x000000000043097f in Perl_call_sv () #15 0x00002ae55e4f15f1 in XS_DBI_dispatch () from /usr/local/lib/perl5 +/site_perl/5.10.1/x86_64-linux-thread-multi/auto/DBI/DBI.so #16 0x000000000048906f in Perl_pp_entersub () #17 0x000000000048765e in Perl_runops_standard () #18 0x00000000004316a5 in perl_run () #19 0x000000000041d4ad in main () (gdb)