[unixODBC-dev] A multi-thread contention issue when using lt_dlinit function

xiaonan xn212516 at 163.com
Thu Apr 11 07:24:47 BST 2013


 Hi, Nick:

    Sorry for interrupting you again!
    
    Because libltdl functions are not thread-safe (http://www.gnu.org/software/libtool/manual/html_node/Thread-Safety-in-libltdl.html), we should add lock before using them.
    
    In __connect_part_one function:
    {
        ......
        /*
         * initialize libtool
         */
    
        lt_dlinit();
        ......
        
        if ( !(connection -> dl_handle = odbc_dlopen( driver_lib )))
        {
           ......
        }
     }
    
     int lt_dlinit (void)
     {
         ......
         if (++initialized == 1)
         {
            ......
         }
    
     }
    
     In multi-thread environment, there will be a scenario: Thread 1 calls lt_dlinit(), and the initialized's value is 1. Then Thread 2 calls lt_dlinit(), and the initialized's value is changed to 2. Thread 2 thinks the initializtion is OK, so it will continue executing and calls odbc_dlopen(). But in fact, the initialization isn't done! So odbc_dlopen() will return NULL.
    
     So I think we should add lock before using lt_dlinit(), such as:
    
     mutex_lib_entry();
     lt_dlinit();
     mutex_lib_exit();
         
     Is it OK? Thanks very much!
 
 Best Regards
 Nan Xiao
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.unixodbc.org/pipermail/unixodbc-dev/attachments/20130411/7f6ef6dc/attachment.html>


More information about the unixODBC-dev mailing list