Bug #53251 mysql_library_init fails on second execution with embedded library
Submitted: 28 Apr 2010 13:11 Modified: 20 Oct 2010 2:16
Reporter: Harrison Fisk Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: Embedded Library ( libmysqld ) Severity:S2 (Serious)
Version:5.5.4 OS:Linux (x86_64)
Assigned to: Alexey Botchkov CPU Architecture:Any
Tags: embedded, mysql_library_init, regression

[28 Apr 2010 13:11] Harrison Fisk
Description:
If you are using the embedded library and call mysql_library_init to launch the embedded server for the second time (after stopping it previously with mysql_library_end), then it will cause a segmentation fault while reading the configuration files.

I will attach a test case to the issue, and it works fine with MySQL 5.1.90.

The backtrace looks like:

(gdb) bt
#0  my_stat (path=0x7fff06294ee0 "/etc/mysql/my.cnf", stat_area=0x7fff062952f0, my_flags=0) at my_lib.c:548
#1  0x00000000005439b3 in search_default_file_with_ext (opt_handler=0x543750 <handle_default_option>, handler_ctx=0x7fff062954e0, dir=0x2fbee30 "/etc/mysql/", 
    ext=0x94d2b5 ".cnf", config_file=0x948188 "my", recursion_level=0) at default.c:714
#2  0x000000000054474c in my_search_option_files (conf_file=0x948188 "my", argc=<value optimized out>, argv=<value optimized out>, args_used=0x7fff0629551c, 
    func=0x543750 <handle_default_option>, func_ctx=0x7fff062954e0, default_directories=0x2fbedf0) at default.c:597
#3  0x0000000000544a3d in my_load_defaults (conf_file=0x948188 "my", groups=0x7fff06295648, argc=0x7fff0629556c, argv=0x7fff06295560, default_directories=0xf65b00)
    at default.c:506
#4  0x0000000000569a26 in init_embedded_server (argc=6, argv=0x7fff06295600, groups=0x7fff06295630) at lib_sql.cc:507
#5  0x0000000000502581 in main ()

How to repeat:
Compile the test case against 5.5.4.  Run the test case.  See segfault.

Compile the test case against 5.1. Run the test case.  See it works fine.

Suggested fix:
Don't segfault.
[28 Apr 2010 13:11] Harrison Fisk
test case

Attachment: main.cpp (text/plain), 1.75 KiB.

[28 Apr 2010 13:12] Harrison Fisk
Makefile for test case, run 'make', or 'make 50',

Attachment: Makefile (application/octet-stream, text), 815 bytes.

[1 Jul 2010 13:45] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/112671

3258 Alexey Botchkov	2010-06-30
      Bug#53251      mysql_library_init fails on second execution with embedded library
         we need to clear error messages when closing server.
      
      per-file comments:
        libmysqld/embedded_priv.h
      Bug#53251      mysql_library_init fails on second execution with embedded library
        libmysqld/lib_sql.cc
      Bug#53251      mysql_library_init fails on second execution with embedded library
[7 Jul 2010 14:36] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/113037

3288 Alexey Botchkov	2010-07-06
      Bug#53251      mysql_library_init fails on second execution with embedded library
          thread-specific variables weren't set when we load error message files.
      
      per-file comments:
        libmysqld/lib_sql.cc
      Bug#53251      mysql_library_init fails on second execution with embedded library
            we need to call my_thread_init() once more. Normally it's called at the my_init()
            stage but that doesn't happen on the second my_init() call.
      
        sql/derror.cc
      Bug#53251      mysql_library_init fails on second execution with embedded library
           use default errors for the embedded server.
[8 Jul 2010 14:12] Alexey Botchkov
Don't mind the first patch please.
Was rather an experimental one.
[30 Aug 2010 17:20] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/117153

3195 Alexey Botchkov	2010-08-30
       Bug#53251      mysql_library_init fails on second execution with embedded library
                thread-specific variables weren't set when we load error message files.
            
      per-file comments:
        libmysqld/lib_sql.cc
      Bug#53251      mysql_library_init fails on second execution with embedded library
            we need to call my_thread_init() once more. Normally it's called at the my_init()
            stage but that doesn't happen on the second my_init() call.
      
        sql/derror.cc
      Bug#53251      mysql_library_init fails on second execution with embedded library
           use default errors for the embedded server.
      
        sql/mysqld.cc
      Bug#53251      mysql_library_init fails on second execution with embedded library
              unregister server errors in clean_up(). Without it the error list contains
              that on the second mysql_server_init() which is not good.
[2 Sep 2010 19:43] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/117451

3202 Alexey Botchkov	2010-09-02
      Bug#53251      mysql_library_init fails on second execution with embedded library
                thread-specific variables weren't set when we load error message files.
      
      per-file comments:
        libmysqld/lib_sql.cc
      Bug#53251      mysql_library_init fails on second execution with embedded library
            we need to call my_thread_init() once more. Normally it's called at the my_init()
            stage but that doesn't happen on the second my_init() call.
      
        sql/derror.cc
      Bug#53251      mysql_library_init fails on second execution with embedded library
           use default errors for the embedded server.
      
        sql/mysqld.cc
      Bug#53251      mysql_library_init fails on second execution with embedded library
              unregister server errors in clean_up(). Without it the error list contains
              that on the second mysql_server_init() which is not good.
      
        sql/set_var.cc
      Bug#53251      mysql_library_init fails on second execution with embedded library
              sys_var::cleanup() call instead of the destructor
      
        sql/set_var.h
      Bug#53251      mysql_library_init fails on second execution with embedded library
              sys_var::cleanup() introduced instead of the destructor
              
        sql/sys_vars.h
      Bug#53251      mysql_library_init fails on second execution with embedded library
              Sys_var_charptr::cleanup() implemented
[3 Sep 2010 10:06] Alexander Barkov
Ok to push:

http://bugs.mysql.com/bug.php?id=53251
[3 Sep 2010 10:39] Alexander Barkov
Ok to push:
http://lists.mysql.com/commits/117451
[9 Sep 2010 7:54] Alexey Botchkov
mysql-5.5-bugfixing
[9 Sep 2010 12:37] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/117859

3206 Davi Arnaut	2010-09-09
      Bug#53251: mysql_library_init fails on second execution with embedded library
      
      Add a virtual destructor. Class has virtual functions.
[2 Oct 2010 18:13] Bugs System
Pushed into mysql-trunk 5.6.1-m4 (revid:alexander.nozdrin@oracle.com-20101002180948-852x1cuv7c6i85ea) (version source revid:alexander.nozdrin@oracle.com-20101002180857-an32jpuwzemsp4f2) (merge vers: 5.6.1-m4) (pib:21)
[2 Oct 2010 18:15] Bugs System
Pushed into mysql-next-mr (revid:alexander.nozdrin@oracle.com-20101002181053-6iotvl26uurcoryp) (version source revid:alexander.nozdrin@oracle.com-20101002180917-h0n62akupm3z20nt) (pib:21)
[2 Oct 2010 18:16] Bugs System
Pushed into mysql-5.5 5.5.7-rc (revid:alexander.nozdrin@oracle.com-20101002180831-590ka2tuit9qoxbb) (version source revid:alexander.nozdrin@oracle.com-20101002180831-590ka2tuit9qoxbb) (merge vers: 5.5.7-rc) (pib:21)
[20 Oct 2010 2:16] Paul DuBois
Noted in 5.5.7, 5.6.1 changelogs.

If an application using the embedded server called
mysql_library_init() a second time after calling mysql_library_init()
and mysql_library_end() to start and stop the server, the application
crashed when reading option files.